]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/annotations/factories/RelatedValueRuleFactory.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / structural / annotations / factories / RelatedValueRuleFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2013 Association for Decentralized Information Management
3  * in 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.structural.annotations.factories;
13
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Field;
16
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.exception.ResourceNotFoundException;
20 import org.simantics.db.exception.ServiceException;
21 import org.simantics.db.exception.ValidationException;
22
23 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;
24 import org.simantics.objmap.graph.annotations.RelatedValue;
25 import org.simantics.objmap.graph.rules.ValueRule;
26 import org.simantics.objmap.graph.rules.adapters.IdentityAdapter;
27 import org.simantics.objmap.graph.rules.adapters.ValueAdapter;
28 import org.simantics.objmap.structural.rules.domain.RelatedValueAccessor;
29 import org.simantics.objmap.graph.rules.factory.IFieldRuleFactory;
30 import org.simantics.objmap.graph.rules.range.AdaptedRangeAccessor;
31 import org.simantics.objmap.graph.rules.range.FieldAccessor;
32 import org.simantics.objmap.graph.rules.range.IRangeAccessor;
33 import org.simantics.objmap.structural.StructuralResource;
34
35
36 public class RelatedValueRuleFactory<Range> implements IFieldRuleFactory<StructuralResource, Range> {
37
38     @Override
39     public IBidirectionalMappingRule<StructuralResource, Range> create(ReadGraph g, Annotation _annotation, Field field) throws ResourceNotFoundException,
40             ValidationException, ServiceException {
41         RelatedValue annotation = (RelatedValue) _annotation;
42         Class<? extends ValueAdapter> adapterClass = annotation.adapter();
43         IRangeAccessor<Range,Object> rangeAccessor = new FieldAccessor<Range,Object>(field);
44         Resource valueType;
45         if (adapterClass == IdentityAdapter.class) {
46             valueType = DataTypeUtils.dataTypeOfClass(g, field.getType());
47         } else {
48             try {
49                 ValueAdapter adapter = adapterClass.newInstance();
50                 rangeAccessor = new AdaptedRangeAccessor<Range>(rangeAccessor, adapter);
51                 valueType = adapter.rangeTypeToDomainType(g, field.getType());
52             } catch (InstantiationException e) {
53                 throw new RuntimeException(e);
54             } catch (IllegalAccessException e) {
55                 throw new RuntimeException(e);
56             }
57         }
58         return new ValueRule<StructuralResource,Range>(new RelatedValueAccessor(g.getResource(annotation.value()), valueType,false,true,false), rangeAccessor);
59     }
60
61 }