]> gerrit.simantics Code Review - simantics/3d.git/blob
c2bc6f9eebe67f19807390c42a7227519868a59b
[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.structural.rules.domain;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 import java.util.Collections;\r
17 import java.util.List;\r
18 \r
19 import org.apache.log4j.Logger;\r
20 import org.simantics.db.ReadGraph;\r
21 import org.simantics.db.Resource;\r
22 import org.simantics.db.WriteGraph;\r
23 import org.simantics.db.exception.DatabaseException;\r
24 import org.simantics.objmap.exceptions.MappingException;\r
25 import org.simantics.objmap.graph.rules.domain.IDomainAccessor;\r
26 import org.simantics.objmap.graph.rules.domain.MappingUtils;\r
27 import org.simantics.objmap.structural.StructuralResource;\r
28 \r
29 \r
30 public class StructuralRelatedObjectsAccessor implements IDomainAccessor<StructuralResource,Collection<StructuralResource>> {\r
31 \r
32     static Logger LOGGER = Logger.getLogger("org.simantics.objmap");\r
33     \r
34         Resource relation;\r
35         boolean deleteExtraObjects;\r
36 \r
37         public StructuralRelatedObjectsAccessor(Resource relation, boolean deleteExtraObjects) {\r
38         super();\r
39         this.relation = relation;\r
40         this.deleteExtraObjects = deleteExtraObjects;\r
41     }\r
42 \r
43     @Override\r
44         public Collection<StructuralResource> get(ReadGraph g, StructuralResource element) throws MappingException {\r
45                 try {\r
46                     LOGGER.info("        RelatedObjectsAccessor.get");\r
47                         \r
48                     if (!element.isStructural())\r
49                         return Collections.emptyList();\r
50                     \r
51                     // Structural instance\r
52                     Resource instance = StructuralUtils.getContainingInstance(element);\r
53                     \r
54                     Resource publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);\r
55                     \r
56                     if (publicRelation == null)\r
57                       return Collections.emptyList();\r
58                     \r
59                     Collection<Resource> coll = g.getObjects(instance, publicRelation);\r
60                         List<StructuralResource> result = new ArrayList<StructuralResource>(coll.size());\r
61                         List<Resource> context = new ArrayList<Resource>();\r
62                         for (int i = 0; i < element.getContext().size()-1; i++)\r
63                                 context.add(element.getContext().get(i));\r
64                         for (Resource r : coll) {\r
65                                 if (StructuralUtils.isStructuralInstance(g, r)) {\r
66                                         result.add(new StructuralResource(g, r, context,r));\r
67                                 } else {\r
68                                         result.add(new StructuralResource(g, r, context));\r
69                                 }\r
70                         }\r
71                         return result;\r
72                 } catch (DatabaseException e) {\r
73                         throw new MappingException(e);\r
74                 }\r
75         }\r
76         \r
77         @Override\r
78         public boolean set(WriteGraph g, StructuralResource element, Collection<StructuralResource> value)\r
79                         throws MappingException {\r
80                 try {\r
81                     LOGGER.info("        RelatedObjectsAccessor.set");\r
82                     \r
83                     if (!element.isStructural())\r
84                         return false;\r
85                     \r
86                     Resource instance = StructuralUtils.getContainingInstance(element);\r
87                     Resource publicRelation = null;\r
88                     if (value.size() == 0) {\r
89                         publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);\r
90                         if (publicRelation == null)\r
91                                 return false;\r
92                         else {\r
93                                 return MappingUtils.synchronizeStatements(g, instance, publicRelation, new Resource[0], deleteExtraObjects);\r
94                         }\r
95                     } else {\r
96                         publicRelation = StructuralUtils.getOrCreatePublishedRelation(g, element, relation);\r
97                         if (publicRelation == null)\r
98                                 throw new MappingException("Structural Resource " + element + " cannot contain structural elements, the Resource is not published.");\r
99                             Resource[] arr = new Resource[value.size()];\r
100                             int i = 0; \r
101                             for (StructuralResource sr : value) {\r
102                                 arr[i++] = sr.getResource();\r
103                             }\r
104                             return MappingUtils.synchronizeStatements(g, instance, publicRelation, arr, deleteExtraObjects);\r
105                     }\r
106                     \r
107                     \r
108                         \r
109                 } catch (DatabaseException e) {\r
110                         throw new MappingException(e);\r
111                 }\r
112                 \r
113         }\r
114 \r
115         \r
116 }\r