/******************************************************************************* * Copyright (c) 2012, 2013 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.objmap.graph.annotations.factories; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import org.simantics.databoard.binding.Binding; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.request.PropertyInfo; import org.simantics.db.layer0.request.PropertyInfoRequest; import org.simantics.objmap.bidirectional.IBidirectionalMappingRule; import org.simantics.objmap.graph.annotations.CompoundRelatedGetValue; import org.simantics.objmap.graph.annotations.CompoundRelatedSetValue; import org.simantics.objmap.graph.rules.ValueRule; import org.simantics.objmap.graph.rules.domain.CompoundValueAccessor; import org.simantics.objmap.graph.rules.factory.IGetSetRuleFactory; import org.simantics.objmap.graph.rules.range.CompoundGetSetValueAccessor; import org.simantics.objmap.graph.rules.range.IRangeAccessor; /** * Rule factory for mapped value using Getter/Setter-methods. * * @author Marko Luukkainen * */ public class CompoundRelatedGetSetValueRuleFactory implements IGetSetRuleFactory { @Override public IBidirectionalMappingRule create(ReadGraph g, Annotation annotation, Method getter, Method setter) throws DatabaseException { CompoundRelatedGetValue getterAnn = (CompoundRelatedGetValue)annotation; // Class adapterClass = getterAnn.adapter(); IRangeAccessor rangeAccessor = new CompoundGetSetValueAccessor(getter, setter); PropertyInfo propInfo = g.syncRequest(new PropertyInfoRequest(g.getResource(getterAnn.valRelation()))); Binding valueBinding = propInfo.defaultBinding; // Resource valueType; // if (adapterClass == IdentityAdapter.class) { // valueType = dataTypeOfClass(g, getter.getReturnType()); // } else { // try{ // ValueAdapter adapter = adapterClass.newInstance(); // rangeAccessor = new AdaptedRangeAccessor(rangeAccessor, adapter); // valueType = adapter.rangeTypeToDomainType(g, getter.getReturnType()); // } catch (InstantiationException e) { // throw new RuntimeException(e); // } catch (IllegalAccessException e) { // throw new RuntimeException(e); // } // } return new ValueRule(new CompoundValueAccessor(g.getResource(getterAnn.objRelation()), g.getResource(getterAnn.objType()), g.getResource(getterAnn.valRelation()), valueBinding), rangeAccessor); } @Override public boolean isSetter(Annotation getterAnnotation, Annotation annotation) { CompoundRelatedGetValue getterAnn = (CompoundRelatedGetValue)getterAnnotation; CompoundRelatedSetValue setterAnn = (CompoundRelatedSetValue)annotation; return getterAnn.objRelation().equals(setterAnn.value()); } }