]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/DynamicSimpleLinkType.java
Use trace level debug messages with ObjMap
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / schema / DynamicSimpleLinkType.java
1 /*******************************************************************************
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.objmap.graph.schema;
13
14 import java.lang.reflect.InvocationTargetException;
15 import java.lang.reflect.Method;
16 import java.util.ArrayList;
17
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.common.utils.NameUtils;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;
25 import org.simantics.objmap.exceptions.MappingException;
26 import org.simantics.objmap.graph.annotations.GetType;
27 import org.simantics.objmap.graph.annotations.SetType;
28
29 public class DynamicSimpleLinkType<Range> extends SimpleLinkType<Range>{
30
31         protected Method typeGetter;
32         protected Method typeSetter;
33         
34         public DynamicSimpleLinkType(Resource domainType, Class<?> rangeType, ArrayList<IBidirectionalMappingRule<Resource, Range>> rules) {
35                 super(domainType, rangeType, rules);
36                 findTypeGetter(rangeType);
37         }
38
39         public DynamicSimpleLinkType(Resource domainType, Class<?> rangeType) {
40                 super(domainType, rangeType);
41                 findTypeGetter(rangeType);
42         }
43         
44         private void findTypeGetter(Class<?> clazz) {
45                 for (Method m : clazz.getDeclaredMethods()) {
46                          m.setAccessible(true);
47                          GetType t = m.getAnnotation(GetType.class);
48                          if (t != null) {
49                                  typeGetter = m;
50                          }
51                          SetType t2 = m.getAnnotation(SetType.class);
52                          if (t2 != null) {
53                                  typeSetter = m;
54                          }
55                 }
56                 if (typeGetter == null || typeSetter == null) {
57                         Class<?> superClazz = clazz.getSuperclass();
58                         if (superClazz != Object.class)
59                                 findTypeGetter(superClazz);
60                         if (typeGetter == null || typeSetter == null) {
61                                 throw new RuntimeException("Cannot find dynamic type methods for class " + clazz.getSimpleName());      
62                         }
63                 }
64                 
65                         
66         }
67         
68         @Override
69     public Resource createDomainElement(WriteGraph g, Range rangeElement)
70             throws MappingException {
71         try {
72                 String typeUri = (String)typeGetter.invoke(rangeElement, (Object[]) null);
73             if(LOGGER.isTraceEnabled())
74                 LOGGER.trace("SimpleLinkType.createDomainElement " +
75                         rangeElement.toString()
76                 );
77             Resource actualDomainType = g.getResource(typeUri);
78             Resource result = g.newResource();
79             //g.claim(result, Layer0.getInstance(g).InstanceOf, null, domainType);
80             g.claim(result, Layer0.getInstance(g).InstanceOf, null, actualDomainType);
81             return result;
82         } catch(DatabaseException e) {
83             throw new MappingException(e);
84         } catch (IllegalArgumentException e) {
85                  throw new MappingException(e);
86                 } catch (IllegalAccessException e) {
87                          throw new MappingException(e);
88                 } catch (InvocationTargetException e) {
89                          throw new MappingException(e.getCause());
90                 }
91     }
92         
93          @SuppressWarnings("unchecked")
94         @Override
95             public Range createRangeElement(ReadGraph g, Resource domainElement)
96                     throws MappingException {
97                 try {
98                     if(LOGGER.isTraceEnabled())
99                         try { 
100                             LOGGER.trace("SimpleLinkType.createRangeElement " +
101                                         NameUtils.getSafeName(g, domainElement)
102                                     );
103                         } catch(DatabaseException e) {
104                             throw new MappingException(e);
105                         }
106                    Range r =  (Range)rangeType.newInstance();
107                    Resource type = g.getSingleType(domainElement, domainType);
108                    String uri = g.getURI(type);
109                    typeSetter.invoke(r, uri);
110                    return r;
111                 } catch (InstantiationException e) {
112                     throw new MappingException(e);
113                 } catch (IllegalAccessException e) {
114                     throw new MappingException(e);
115                 } catch (DatabaseException e) {
116                         throw new MappingException(e);
117                         } catch (IllegalArgumentException e) {
118                                 throw new MappingException(e);
119                         } catch (InvocationTargetException e) {
120                                 throw new MappingException(e.getCause());
121                         } 
122             }
123
124 }