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