]> gerrit.simantics Code Review - simantics/3d.git/blob
84b1134ac01f9cec9cbdd7c65a16c47bcecb05ac
[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 import java.util.Collection;\r
17 import java.util.Collections;\r
18 \r
19 import org.simantics.db.ReadGraph;\r
20 import org.simantics.db.Resource;\r
21 import org.simantics.db.exception.ResourceNotFoundException;\r
22 import org.simantics.db.exception.ServiceException;\r
23 import org.simantics.db.exception.ValidationException;\r
24 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;\r
25 import org.simantics.objmap.graph.annotations.OptionalRelatedElements;\r
26 import org.simantics.objmap.graph.rules.MappedElementsRule;\r
27 import org.simantics.objmap.graph.rules.domain.RelatedObjectsAccessor;\r
28 import org.simantics.objmap.graph.rules.factory.IFieldRuleFactory;\r
29 import org.simantics.objmap.graph.rules.range.FieldAccessorWithDefault;\r
30 \r
31 \r
32 public class OptionalRelatedElementsRuleFactory<Range> implements IFieldRuleFactory<Resource, Range> {\r
33 \r
34     @Override\r
35     public IBidirectionalMappingRule<Resource, Range> create(ReadGraph g, Annotation _annotation, Field field) throws ResourceNotFoundException, ValidationException, ServiceException {\r
36         OptionalRelatedElements annotation = (OptionalRelatedElements)_annotation;\r
37         return new MappedElementsRule<Resource, Range>(\r
38                 new RelatedObjectsAccessor(g.getResource(annotation.value()),\r
39                         annotation.composition()),\r
40                 new FieldAccessorWithDefault<Range,Collection<Range>>(field, Collections.<Range>emptyList())\r
41                 );\r
42     }\r
43 \r
44 }\r