]> gerrit.simantics Code Review - simantics/3d.git/blob
66e6ea908f6e095c5dff644b9da7a636a35c0d7c
[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.List;\r
16 \r
17 import org.apache.log4j.Logger;\r
18 import org.simantics.db.ReadGraph;\r
19 import org.simantics.db.Resource;\r
20 import org.simantics.db.WriteGraph;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 import org.simantics.objmap.exceptions.MappingException;\r
23 import org.simantics.objmap.graph.rules.domain.IDomainAccessor;\r
24 import org.simantics.objmap.structural.StructuralResource;\r
25 \r
26 \r
27 public class StructuralRelatedObjectAccessor implements IDomainAccessor<StructuralResource,StructuralResource> {\r
28 \r
29     static Logger LOGGER = Logger.getLogger("org.simantics.objmap");\r
30     \r
31         Resource relation;\r
32         boolean useTypeResource;\r
33         \r
34         private boolean preventStructuralChanges = true;\r
35         private boolean preventStructuralRootChanges = true;\r
36         \r
37         public StructuralRelatedObjectAccessor(Resource relation, boolean useTypeResource) {\r
38                 this.relation = relation;\r
39                 this.useTypeResource = useTypeResource;\r
40         }\r
41         \r
42         public StructuralRelatedObjectAccessor(Resource relation, boolean useTypeResource, boolean preventStructuralChanges, boolean preventStructuralRootChanges) {\r
43                 this.relation = relation;\r
44                 this.useTypeResource = useTypeResource;\r
45                 this.preventStructuralChanges = preventStructuralChanges;\r
46                 this.preventStructuralRootChanges = preventStructuralRootChanges;\r
47         }\r
48         \r
49  private boolean preventChange(StructuralResource element) {\r
50         return preventStructuralChanges && element.isStructural() && (!element.isStructuralRoot()||preventStructuralRootChanges);       \r
51     }\r
52 \r
53         @Override\r
54         public StructuralResource get(ReadGraph g, StructuralResource element) throws MappingException {\r
55                 try {\r
56                         \r
57                         LOGGER.info("        RelatedObjectAccessor.get");\r
58                         \r
59                         if (!element.isStructural())\r
60                                 return null;\r
61                         Resource instance = StructuralUtils.getContainingInstance(element);\r
62                             \r
63                         Resource publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);\r
64 \r
65                      if (publicRelation == null)\r
66                         return null;\r
67                         Resource r =  g.getPossibleObject(instance, publicRelation);\r
68                         if (r == null)\r
69                                 return null;\r
70                         List<Resource> context = new ArrayList<Resource>();\r
71                         for (int i = 0; i < element.getContext().size()-1; i++)\r
72                                 context.add(element.getContext().get(i));\r
73                         if (StructuralUtils.isStructuralInstance(g, r)) {\r
74                                 return new StructuralResource(g, r, context,r);\r
75                         } else {\r
76                                 return new StructuralResource(g, r, context);\r
77                         }\r
78                 } catch (DatabaseException e) {\r
79                         throw new MappingException(e);\r
80                 }\r
81         }\r
82         \r
83         @Override\r
84         public boolean set(WriteGraph g, StructuralResource element, StructuralResource value)\r
85                         throws MappingException {\r
86                 try {\r
87                     LOGGER.info("        RelatedObjectAccessor.set");\r
88                     Resource instance = StructuralUtils.getContainingInstance(element);\r
89                     Resource publicRelation = null;\r
90                     if (instance == null)\r
91                         return false;\r
92                     publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);\r
93                     if (value == null) {\r
94                         if (publicRelation == null)\r
95                                 return false;\r
96                         if (preventChange(element))\r
97                                 return false;\r
98                         g.deny(instance, publicRelation);\r
99                         return true;\r
100                     } else {\r
101                         if (publicRelation == null) {\r
102                                 if (preventChange(element))\r
103                                         return false;\r
104                                 publicRelation = StructuralUtils.getOrCreatePublishedRelation(g, element, relation);\r
105                                 g.claim(instance, publicRelation, value.getResource());\r
106                                 return true;\r
107                         } else {\r
108                                 Resource r = g.getPossibleObject(instance, publicRelation);\r
109                                 if (r == null) {\r
110                                         if (preventChange(element))\r
111                                                 return false;\r
112                                         g.claim(instance, publicRelation, value.getResource());\r
113                                         return true;\r
114                                 } else {\r
115                                         if (r.equals(value.getResource()))\r
116                                                 return false;\r
117                                         if (preventChange(element))\r
118                                                 return false;\r
119                                         g.deny(instance, publicRelation);\r
120                                         g.claim(instance, publicRelation, value.getResource());\r
121                                         return true;\r
122                                 }\r
123                         }\r
124                     }\r
125                 } catch (DatabaseException e) {\r
126                         throw new MappingException(e);\r
127                 }\r
128                 \r
129         }\r
130 \r
131 }\r