-/*******************************************************************************\r
- * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.objmap.graph.schema;\r
-\r
-import java.lang.reflect.InvocationTargetException;\r
-import java.lang.reflect.Method;\r
-import java.util.ArrayList;\r
-\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;\r
-import org.simantics.objmap.exceptions.MappingException;\r
-import org.simantics.objmap.graph.annotations.GetType;\r
-import org.simantics.objmap.graph.annotations.SetType;\r
-\r
-public class DynamicSimpleLinkType<Range> extends SimpleLinkType<Range>{\r
-\r
- protected Method typeGetter;\r
- protected Method typeSetter;\r
- \r
- public DynamicSimpleLinkType(Resource domainType, Class<?> rangeType, ArrayList<IBidirectionalMappingRule<Resource, Range>> rules) {\r
- super(domainType, rangeType, rules);\r
- findTypeGetter(rangeType);\r
- }\r
-\r
- public DynamicSimpleLinkType(Resource domainType, Class<?> rangeType) {\r
- super(domainType, rangeType);\r
- findTypeGetter(rangeType);\r
- }\r
- \r
- private void findTypeGetter(Class<?> clazz) {\r
- for (Method m : clazz.getDeclaredMethods()) {\r
- m.setAccessible(true);\r
- GetType t = m.getAnnotation(GetType.class);\r
- if (t != null) {\r
- typeGetter = m;\r
- }\r
- SetType t2 = m.getAnnotation(SetType.class);\r
- if (t2 != null) {\r
- typeSetter = m;\r
- }\r
- }\r
- if (typeGetter == null || typeSetter == null) {\r
- Class<?> superClazz = clazz.getSuperclass();\r
- if (superClazz != Object.class)\r
- findTypeGetter(superClazz);\r
- if (typeGetter == null || typeSetter == null) {\r
- throw new RuntimeException("Cannot find dynamic type methods for class " + clazz.getSimpleName()); \r
- }\r
- }\r
- \r
- \r
- }\r
- \r
- @Override\r
- public Resource createDomainElement(WriteGraph g, Range rangeElement)\r
- throws MappingException {\r
- try {\r
- String typeUri = (String)typeGetter.invoke(rangeElement, (Object[]) null);\r
- if(LOGGER.isInfoEnabled())\r
- LOGGER.info("SimpleLinkType.createDomainElement " +\r
- rangeElement.toString()\r
- );\r
- Resource actualDomainType = g.getResource(typeUri);\r
- Resource result = g.newResource();\r
- //g.claim(result, Layer0.getInstance(g).InstanceOf, null, domainType);\r
- g.claim(result, Layer0.getInstance(g).InstanceOf, null, actualDomainType);\r
- return result;\r
- } catch(DatabaseException e) {\r
- throw new MappingException(e);\r
- } catch (IllegalArgumentException e) {\r
- throw new MappingException(e);\r
- } catch (IllegalAccessException e) {\r
- throw new MappingException(e);\r
- } catch (InvocationTargetException e) {\r
- throw new MappingException(e.getCause());\r
- }\r
- }\r
- \r
- @SuppressWarnings("unchecked")\r
- @Override\r
- public Range createRangeElement(ReadGraph g, Resource domainElement)\r
- throws MappingException {\r
- try {\r
- if(LOGGER.isInfoEnabled())\r
- try { \r
- LOGGER.info("SimpleLinkType.createRangeElement " +\r
- NameUtils.getSafeName(g, domainElement)\r
- );\r
- } catch(DatabaseException e) {\r
- throw new MappingException(e);\r
- }\r
- Range r = (Range)rangeType.newInstance();\r
- Resource type = g.getSingleType(domainElement, domainType);\r
- String uri = g.getURI(type);\r
- typeSetter.invoke(r, uri);\r
- return r;\r
- } catch (InstantiationException e) {\r
- throw new MappingException(e);\r
- } catch (IllegalAccessException e) {\r
- throw new MappingException(e);\r
- } catch (DatabaseException e) {\r
- throw new MappingException(e);\r
- } catch (IllegalArgumentException e) {\r
- throw new MappingException(e);\r
- } catch (InvocationTargetException e) {\r
- throw new MappingException(e.getCause());\r
- } \r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.objmap.graph.schema;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.layer0.Layer0;
+import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;
+import org.simantics.objmap.exceptions.MappingException;
+import org.simantics.objmap.graph.annotations.GetType;
+import org.simantics.objmap.graph.annotations.SetType;
+
+public class DynamicSimpleLinkType<Range> extends SimpleLinkType<Range>{
+
+ protected Method typeGetter;
+ protected Method typeSetter;
+
+ public DynamicSimpleLinkType(Resource domainType, Class<?> rangeType, ArrayList<IBidirectionalMappingRule<Resource, Range>> rules) {
+ super(domainType, rangeType, rules);
+ findTypeGetter(rangeType);
+ }
+
+ public DynamicSimpleLinkType(Resource domainType, Class<?> rangeType) {
+ super(domainType, rangeType);
+ findTypeGetter(rangeType);
+ }
+
+ private void findTypeGetter(Class<?> clazz) {
+ for (Method m : clazz.getDeclaredMethods()) {
+ m.setAccessible(true);
+ GetType t = m.getAnnotation(GetType.class);
+ if (t != null) {
+ typeGetter = m;
+ }
+ SetType t2 = m.getAnnotation(SetType.class);
+ if (t2 != null) {
+ typeSetter = m;
+ }
+ }
+ if (typeGetter == null || typeSetter == null) {
+ Class<?> superClazz = clazz.getSuperclass();
+ if (superClazz != Object.class)
+ findTypeGetter(superClazz);
+ if (typeGetter == null || typeSetter == null) {
+ throw new RuntimeException("Cannot find dynamic type methods for class " + clazz.getSimpleName());
+ }
+ }
+
+
+ }
+
+ @Override
+ public Resource createDomainElement(WriteGraph g, Range rangeElement)
+ throws MappingException {
+ try {
+ String typeUri = (String)typeGetter.invoke(rangeElement, (Object[]) null);
+ if(LOGGER.isInfoEnabled())
+ LOGGER.info("SimpleLinkType.createDomainElement " +
+ rangeElement.toString()
+ );
+ Resource actualDomainType = g.getResource(typeUri);
+ Resource result = g.newResource();
+ //g.claim(result, Layer0.getInstance(g).InstanceOf, null, domainType);
+ g.claim(result, Layer0.getInstance(g).InstanceOf, null, actualDomainType);
+ return result;
+ } catch(DatabaseException e) {
+ throw new MappingException(e);
+ } catch (IllegalArgumentException e) {
+ throw new MappingException(e);
+ } catch (IllegalAccessException e) {
+ throw new MappingException(e);
+ } catch (InvocationTargetException e) {
+ throw new MappingException(e.getCause());
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public Range createRangeElement(ReadGraph g, Resource domainElement)
+ throws MappingException {
+ try {
+ if(LOGGER.isInfoEnabled())
+ try {
+ LOGGER.info("SimpleLinkType.createRangeElement " +
+ NameUtils.getSafeName(g, domainElement)
+ );
+ } catch(DatabaseException e) {
+ throw new MappingException(e);
+ }
+ Range r = (Range)rangeType.newInstance();
+ Resource type = g.getSingleType(domainElement, domainType);
+ String uri = g.getURI(type);
+ typeSetter.invoke(r, uri);
+ return r;
+ } catch (InstantiationException e) {
+ throw new MappingException(e);
+ } catch (IllegalAccessException e) {
+ throw new MappingException(e);
+ } catch (DatabaseException e) {
+ throw new MappingException(e);
+ } catch (IllegalArgumentException e) {
+ throw new MappingException(e);
+ } catch (InvocationTargetException e) {
+ throw new MappingException(e.getCause());
+ }
+ }
+
+}