1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2013 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.objmap.structural.annotations.factories;
\r
14 import java.lang.annotation.Annotation;
\r
15 import java.lang.reflect.Field;
\r
17 import org.simantics.db.ReadGraph;
\r
18 import org.simantics.db.Resource;
\r
19 import org.simantics.db.exception.ResourceNotFoundException;
\r
20 import org.simantics.db.exception.ServiceException;
\r
21 import org.simantics.db.exception.ValidationException;
\r
23 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;
\r
24 import org.simantics.objmap.graph.annotations.RelatedValue;
\r
25 import org.simantics.objmap.graph.rules.ValueRule;
\r
26 import org.simantics.objmap.graph.rules.adapters.IdentityAdapter;
\r
27 import org.simantics.objmap.graph.rules.adapters.ValueAdapter;
\r
28 import org.simantics.objmap.structural.rules.domain.RelatedValueAccessor;
\r
29 import org.simantics.objmap.graph.rules.factory.IFieldRuleFactory;
\r
30 import org.simantics.objmap.graph.rules.range.AdaptedRangeAccessor;
\r
31 import org.simantics.objmap.graph.rules.range.FieldAccessor;
\r
32 import org.simantics.objmap.graph.rules.range.IRangeAccessor;
\r
33 import org.simantics.objmap.structural.StructuralResource;
\r
36 public class RelatedValueRuleFactory<Range> implements IFieldRuleFactory<StructuralResource, Range> {
\r
39 public IBidirectionalMappingRule<StructuralResource, Range> create(ReadGraph g, Annotation _annotation, Field field) throws ResourceNotFoundException,
\r
40 ValidationException, ServiceException {
\r
41 RelatedValue annotation = (RelatedValue) _annotation;
\r
42 Class<? extends ValueAdapter> adapterClass = annotation.adapter();
\r
43 IRangeAccessor<Range,Object> rangeAccessor = new FieldAccessor<Range,Object>(field);
\r
45 if (adapterClass == IdentityAdapter.class) {
\r
46 valueType = DataTypeUtils.dataTypeOfClass(g, field.getType());
\r
49 ValueAdapter adapter = adapterClass.newInstance();
\r
50 rangeAccessor = new AdaptedRangeAccessor<Range>(rangeAccessor, adapter);
\r
51 valueType = adapter.rangeTypeToDomainType(g, field.getType());
\r
52 } catch (InstantiationException e) {
\r
53 throw new RuntimeException(e);
\r
54 } catch (IllegalAccessException e) {
\r
55 throw new RuntimeException(e);
\r
58 return new ValueRule<StructuralResource,Range>(new RelatedValueAccessor(g.getResource(annotation.value()), valueType,false,true,false), rangeAccessor);
\r