]> gerrit.simantics Code Review - simantics/platform.git/blob
2d3a24230e88c3b6d0edd51377c75c52f1fc0903
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2007, 2013 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.objmap.structural.rules.domain;
13
14 import java.util.Arrays;
15
16 import org.apache.log4j.Logger;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Statement;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.objmap.exceptions.MappingException;
24 import org.simantics.objmap.graph.rules.domain.IDomainAccessor;
25 import org.simantics.objmap.structural.StructuralResource;
26
27
28 public class RelatedValueAccessor implements IDomainAccessor<StructuralResource,Object> {
29
30     static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
31     
32         Resource relation;
33         Resource valueType;
34         boolean useTypeResource;
35         
36         private boolean preventStructuralChanges = true;
37         private boolean preventStructuralRootChanges = true;
38         
39         public RelatedValueAccessor(Resource relation, Resource valueType, boolean useTypeResource) {
40                 this.relation = relation;
41                 this.valueType = valueType;
42                 this.useTypeResource = useTypeResource;
43         }
44         
45         public RelatedValueAccessor(Resource relation, Resource valueType, boolean useTypeResource, boolean preventStructuralChanges, boolean preventStructuralRootChanges) {
46                 this.relation = relation;
47                 this.valueType = valueType;
48                 this.useTypeResource = useTypeResource;
49                 this.preventStructuralChanges = preventStructuralChanges;
50                 this.preventStructuralRootChanges = preventStructuralRootChanges;
51         }
52         
53         private boolean preventChange(StructuralResource element) {
54         return preventStructuralChanges && element.isStructural() && (!element.isStructuralRoot()||preventStructuralRootChanges);       
55     }
56
57         @Override
58         public Object get(ReadGraph g, StructuralResource element) throws MappingException {
59                 try {
60                     LOGGER.info("        RelatedValueAccessor.get");
61                     Resource res = getServiceResource(g, element);
62                     if (res == null)
63                         return null;
64                         Resource valueResource = g.getPossibleObject(res, relation);
65                         if(valueResource == null)
66                                 return null;
67                         return g.getValue(valueResource);
68                 } catch (DatabaseException e) {
69                         throw new MappingException(e);
70                 }
71         }
72         
73         @Override
74         public boolean set(WriteGraph g, StructuralResource relement, Object value)
75                         throws MappingException {
76                 try {
77                     LOGGER.info("        RelatedValueAccessor.set");
78                     
79                     Resource element = getServiceResource(g, relement);
80                     if (element == null)
81                         return false;
82                     Statement valueStatement = g.getPossibleStatement(element, relation);
83                         if(valueStatement == null) {
84                                 if(value == null)
85                                         return false;
86                                 if (preventChange(relement))
87                                         return false;
88                                 Resource valueResource = g.newResource();
89                                 g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null,
90                                                 valueType);
91                                 g.claim(element, relation, valueResource);
92                                 g.claimValue(valueResource, value);                             
93                                 return true;
94                         }
95                         else {
96                                 if(value == null) {
97                                         if (preventChange(relement))
98                                                 return false;
99                                         if (!valueStatement.isAsserted(element)) {
100                                                 g.deny(valueStatement.getObject());
101                                                 return true;
102                                         } else {
103                                                 return false;
104                                         }
105                                         
106                                 }                               
107                                 Object currentValue = g.getValue(valueStatement.getObject());
108                                 if(equals(currentValue,value))
109                                         return false;
110                                 if (preventChange(relement))
111                                         return false;
112                                 if (!valueStatement.isAsserted(element))
113                                         g.claimValue(valueStatement.getObject(), value);
114                                 else {
115                                         Resource valueResource = g.newResource();
116                                         g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null,
117                                                         valueType);
118                                         g.claim(element, relation, valueResource);
119                                         g.claimValue(valueResource, value);
120                                 }
121                                 return true;
122                         }
123                 } catch (DatabaseException e) {
124                         throw new MappingException(e);
125                 }
126                 
127         }
128         
129         private Resource getServiceResource(ReadGraph g, StructuralResource element) {
130                 if (!useTypeResource)
131                         return element.getResource();
132                 return element.getTypeResource();
133         }
134         
135         private boolean equals(Object o1, Object o2) {
136                 if (o1 instanceof boolean[])
137                         Arrays.equals((boolean[])o1,(boolean[])o2);
138                 if (o1 instanceof int[])
139                         Arrays.equals((int[])o1,(int[])o2);
140                 if (o1 instanceof float[])
141                         Arrays.equals((float[])o1,(float[])o2);
142                 if (o1 instanceof double[])
143                         Arrays.equals((double[])o1,(double[])o2);
144                 if (o1 instanceof byte[])
145                         Arrays.equals((byte[])o1,(byte[])o2);
146                 return o1.equals(o2);
147                 
148         }
149
150 }