1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.objmap.graph.annotations.factories;
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Method;
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;
34 * Rule factory for mapped value using Getter/Setter-methods.
36 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
39 public class CompoundRelatedGetSetValueRuleFactory<Range> implements IGetSetRuleFactory<Resource,Range> {
42 public IBidirectionalMappingRule<Resource, Range> create(ReadGraph g, Annotation annotation,
43 Method getter, Method setter)
44 throws DatabaseException {
45 CompoundRelatedGetValue getterAnn = (CompoundRelatedGetValue)annotation;
47 // Class<? extends ValueAdapter> adapterClass = getterAnn.adapter();
48 IRangeAccessor<Range,Object> rangeAccessor = new CompoundGetSetValueAccessor<Range,Object>(getter, setter);
49 Binding valueBinding = null;
51 valueBinding = Bindings.getBinding(BindingRequest.create(getter));
52 } catch (BindingConstructionException e) {
55 // Resource valueType;
56 // if (adapterClass == IdentityAdapter.class) {
57 // valueType = dataTypeOfClass(g, getter.getReturnType());
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);
69 return new ValueRule<Resource,Range>(new CompoundValueAccessor(g.getResource(getterAnn.objRelation()),
70 g.getResource(getterAnn.objType()),
71 g.getResource(getterAnn.valRelation()),
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());