1 package org.simantics.objmap.graph.schema;
\r
3 import java.lang.reflect.InvocationTargetException;
\r
4 import java.lang.reflect.Method;
\r
5 import java.util.ArrayList;
\r
7 import org.simantics.db.ReadGraph;
\r
8 import org.simantics.db.Resource;
\r
9 import org.simantics.db.WriteGraph;
\r
10 import org.simantics.db.common.utils.NameUtils;
\r
11 import org.simantics.db.exception.DatabaseException;
\r
12 import org.simantics.layer0.Layer0;
\r
13 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;
\r
14 import org.simantics.objmap.exceptions.MappingException;
\r
15 import org.simantics.objmap.graph.annotations.GetType;
\r
16 import org.simantics.objmap.graph.annotations.SetType;
\r
18 public class DynamicSimpleLinkType<Range> extends SimpleLinkType<Range>{
\r
20 protected Method typeGetter;
\r
21 protected Method typeSetter;
\r
23 public DynamicSimpleLinkType(Resource domainType, Class<?> rangeType, ArrayList<IBidirectionalMappingRule<Resource, Range>> rules) {
\r
24 super(domainType, rangeType, rules);
\r
25 findTypeGetter(rangeType);
\r
28 public DynamicSimpleLinkType(Resource domainType, Class<?> rangeType) {
\r
29 super(domainType, rangeType);
\r
30 findTypeGetter(rangeType);
\r
33 private void findTypeGetter(Class<?> clazz) {
\r
34 for (Method m : clazz.getDeclaredMethods()) {
\r
35 m.setAccessible(true);
\r
36 GetType t = m.getAnnotation(GetType.class);
\r
40 SetType t2 = m.getAnnotation(SetType.class);
\r
45 if (typeGetter == null || typeSetter == null) {
\r
46 Class<?> superClazz = clazz.getSuperclass();
\r
47 if (superClazz != Object.class)
\r
48 findTypeGetter(superClazz);
\r
49 if (typeGetter == null || typeSetter == null) {
\r
50 throw new RuntimeException("Cannot find dynamic type methods for class " + clazz.getSimpleName());
\r
58 public Resource createDomainElement(WriteGraph g, Range rangeElement)
\r
59 throws MappingException {
\r
61 String typeUri = (String)typeGetter.invoke(rangeElement, null);
\r
62 if(LOGGER.isInfoEnabled())
\r
63 LOGGER.info("SimpleLinkType.createDomainElement " +
\r
64 rangeElement.toString()
\r
66 Resource actualDomainType = g.getResource(typeUri);
\r
67 Resource result = g.newResource();
\r
68 //g.claim(result, Layer0.getInstance(g).InstanceOf, null, domainType);
\r
69 g.claim(result, Layer0.getInstance(g).InstanceOf, null, actualDomainType);
\r
71 } catch(DatabaseException e) {
\r
72 throw new MappingException(e);
\r
73 } catch (IllegalArgumentException e) {
\r
74 throw new MappingException(e);
\r
75 } catch (IllegalAccessException e) {
\r
76 throw new MappingException(e);
\r
77 } catch (InvocationTargetException e) {
\r
78 throw new MappingException(e);
\r
83 public Range createRangeElement(ReadGraph g, Resource domainElement)
\r
84 throws MappingException {
\r
86 if(LOGGER.isInfoEnabled())
\r
88 LOGGER.info("SimpleLinkType.createRangeElement " +
\r
89 NameUtils.getSafeName(g, domainElement)
\r
91 } catch(DatabaseException e) {
\r
92 throw new MappingException(e);
\r
94 Range r = (Range)rangeType.newInstance();
\r
95 Resource type = g.getSingleType(domainElement, domainType);
\r
96 String uri = g.getURI(type);
\r
97 typeSetter.invoke(r, uri);
\r
99 } catch (InstantiationException e) {
\r
100 throw new MappingException(e);
\r
101 } catch (IllegalAccessException e) {
\r
102 throw new MappingException(e);
\r
103 } catch (DatabaseException e) {
\r
104 throw new MappingException(e);
\r
105 } catch (IllegalArgumentException e) {
\r
106 throw new MappingException(e);
\r
107 } catch (InvocationTargetException e) {
\r
108 throw new MappingException(e);
\r