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