]> gerrit.simantics Code Review - simantics/3d.git/blob
5ee9f20c5733d698101fdb06cb8df94fe6a9f347
[simantics/3d.git] /
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.objmap.structural.annotations.factories;\r
13 \r
14 import java.lang.annotation.Annotation;\r
15 import java.lang.reflect.Field;\r
16 \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 \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
34 \r
35 \r
36 public class RelatedValueRuleFactory<Range> implements IFieldRuleFactory<StructuralResource, Range> {\r
37 \r
38     @Override\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
44         Resource valueType;\r
45         if (adapterClass == IdentityAdapter.class) {\r
46             valueType = DataTypeUtils.dataTypeOfClass(g, field.getType());\r
47         } else {\r
48             try {\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
56             }\r
57         }\r
58         return new ValueRule<StructuralResource,Range>(new RelatedValueAccessor(g.getResource(annotation.value()), valueType,false,true,false), rangeAccessor);\r
59     }\r
60 \r
61 }\r