1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 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.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
22 import org.simantics.objmap.IMappingRule;
\r
23 import org.simantics.objmap.annotations.RelatedValue;
\r
24 import org.simantics.objmap.rules.ValueRule;
\r
25 import org.simantics.objmap.rules.adapters.IdentityAdapter;
\r
26 import org.simantics.objmap.rules.adapters.ValueAdapter;
\r
27 import org.simantics.objmap.rules.domain.RelatedValueAccessor;
\r
28 import org.simantics.objmap.rules.factory.IFieldRuleFactory;
\r
29 import org.simantics.objmap.rules.range.AdaptedRangeAccessor;
\r
30 import org.simantics.objmap.rules.range.FieldAccessor;
\r
31 import org.simantics.objmap.rules.range.IRangeAccessor;
\r
33 public class RelatedValueRuleFactory implements IFieldRuleFactory {
\r
36 public IMappingRule create(ReadGraph g, Annotation _annotation, Field field) throws ResourceNotFoundException,
\r
37 ValidationException, ServiceException {
\r
38 RelatedValue annotation = (RelatedValue) _annotation;
\r
39 Class<? extends ValueAdapter> adapterClass = annotation.adapter();
\r
40 IRangeAccessor<Object> rangeAccessor = new FieldAccessor<Object>(field);
\r
42 if (adapterClass == IdentityAdapter.class) {
\r
43 valueType = DataTypeUtils.dataTypeOfClass(g, field.getType());
\r
46 ValueAdapter adapter = adapterClass.newInstance();
\r
47 rangeAccessor = new AdaptedRangeAccessor(rangeAccessor, adapter);
\r
48 valueType = adapter.rangeTypeToDomainType(g, field.getType());
\r
49 } catch (InstantiationException e) {
\r
50 throw new RuntimeException(e);
\r
51 } catch (IllegalAccessException e) {
\r
52 throw new RuntimeException(e);
\r
55 return new ValueRule(new RelatedValueAccessor(g.getResource(annotation.value()), valueType), rangeAccessor);
\r