]> gerrit.simantics Code Review - simantics/3d.git/blob
52d6caa6fdbc91ae0ecb1d126fb2c88dc62831a2
[simantics/3d.git] /
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.objmap.graph.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.graph.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 \r
34 \r
35 public class RelatedValueRuleFactory<Range> implements IFieldRuleFactory<Resource, Range> {\r
36 \r
37     @Override\r
38     public IBidirectionalMappingRule<Resource, Range> create(ReadGraph g, Annotation _annotation, Field field) throws ResourceNotFoundException,\r
39             ValidationException, ServiceException {\r
40         RelatedValue annotation = (RelatedValue) _annotation;\r
41         Class<? extends ValueAdapter> adapterClass = annotation.adapter();\r
42         IRangeAccessor<Range,Object> rangeAccessor = new FieldAccessor<Range,Object>(field);\r
43         Resource valueType;\r
44         if (adapterClass == IdentityAdapter.class) {\r
45             valueType = DataTypeUtils.dataTypeOfClass(g, field.getType());\r
46         } else {\r
47             try {\r
48                 ValueAdapter adapter = adapterClass.newInstance();\r
49                 rangeAccessor = new AdaptedRangeAccessor<Range>(rangeAccessor, adapter);\r
50                 valueType = adapter.rangeTypeToDomainType(g, field.getType());\r
51             } catch (InstantiationException e) {\r
52                 throw new RuntimeException(e);\r
53             } catch (IllegalAccessException e) {\r
54                 throw new RuntimeException(e);\r
55             }\r
56         }\r
57         return new ValueRule<Resource,Range>(new RelatedValueAccessor(g.getResource(annotation.value()), valueType), rangeAccessor);\r
58     }\r
59 \r
60 }\r