]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/factories/CompoundRelatedGetSetValueRuleFactory.java
Fixed some issues in objmap2
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / annotations / factories / CompoundRelatedGetSetValueRuleFactory.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.annotations.factories;
13
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Method;
16
17 import org.simantics.databoard.Bindings;
18 import org.simantics.databoard.binding.Binding;
19 import org.simantics.databoard.binding.error.BindingConstructionException;
20 import org.simantics.databoard.binding.reflection.BindingRequest;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;
25 import org.simantics.objmap.graph.annotations.CompoundRelatedGetValue;
26 import org.simantics.objmap.graph.annotations.CompoundRelatedSetValue;
27 import org.simantics.objmap.graph.rules.ValueRule;
28 import org.simantics.objmap.graph.rules.domain.CompoundValueAccessor;
29 import org.simantics.objmap.graph.rules.factory.IGetSetRuleFactory;
30 import org.simantics.objmap.graph.rules.range.CompoundGetSetValueAccessor;
31 import org.simantics.objmap.graph.rules.range.IRangeAccessor;
32
33 /**
34  * Rule factory for mapped value using Getter/Setter-methods.
35  * 
36  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
37  *
38  */
39 public class CompoundRelatedGetSetValueRuleFactory<Range> implements IGetSetRuleFactory<Resource,Range> {
40         
41         @Override
42         public IBidirectionalMappingRule<Resource, Range> create(ReadGraph g, Annotation annotation,
43                         Method getter, Method setter)
44                         throws DatabaseException {
45                 CompoundRelatedGetValue getterAnn = (CompoundRelatedGetValue)annotation;
46                 
47 //              Class<? extends ValueAdapter> adapterClass = getterAnn.adapter();
48                 IRangeAccessor<Range,Object> rangeAccessor = new CompoundGetSetValueAccessor<Range,Object>(getter, setter);
49                 Binding valueBinding = null;
50                 try {
51                         valueBinding = Bindings.getBinding(BindingRequest.create(getter));
52                 } catch (BindingConstructionException e) {
53                 }
54                 
55 //        Resource valueType;
56 //        if (adapterClass == IdentityAdapter.class) {
57 //            valueType = dataTypeOfClass(g, getter.getReturnType());
58 //        } else {
59 //              try{
60 //                       ValueAdapter adapter = adapterClass.newInstance();
61 //                 rangeAccessor = new AdaptedRangeAccessor<Range>(rangeAccessor, adapter);
62 //                 valueType = adapter.rangeTypeToDomainType(g, getter.getReturnType());
63 //             } catch (InstantiationException e) {
64 //                 throw new RuntimeException(e);
65 //             } catch (IllegalAccessException e) {
66 //                 throw new RuntimeException(e);
67 //             }
68 //        }
69                 return new ValueRule<Resource,Range>(new CompoundValueAccessor(g.getResource(getterAnn.objRelation()),
70                                                                                                                                            g.getResource(getterAnn.objType()),
71                                                                                                                                            g.getResource(getterAnn.valRelation()),
72                                                                                                                                            valueBinding),
73                                         rangeAccessor);
74         }
75         
76         @Override
77         public boolean isSetter(Annotation getterAnnotation, Annotation annotation) {
78                 CompoundRelatedGetValue getterAnn = (CompoundRelatedGetValue)getterAnnotation;
79                 CompoundRelatedSetValue setterAnn = (CompoundRelatedSetValue)annotation;
80                 return getterAnn.objRelation().equals(setterAnn.value());
81         }
82
83 }