]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/domain/RelatedValueAccessor.java
d5ad40b77cd8f82f2369978872c7e967e24aac7d
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / rules / domain / RelatedValueAccessor.java
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.graph.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
25 /**
26  * Accesses a value attached to the element by given functional relation.
27  * @author Hannu Niemist�
28  */
29 public class RelatedValueAccessor implements IDomainAccessor<Resource,Object> {
30
31     static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
32     
33         Resource relation;
34         Resource valueType;
35
36         public RelatedValueAccessor(Resource relation, Resource valueType) {
37                 this.relation = relation;
38                 this.valueType = valueType;
39         }
40
41         @Override
42         public Object get(ReadGraph g, Resource element) throws MappingException {
43                 try {
44                     LOGGER.info("        RelatedValueAccessor.get");
45                         Resource valueResource = g.getPossibleObject(element, relation);
46                         if(valueResource == null)
47                                 return null;
48                         return g.getValue(valueResource);
49                 } catch (DatabaseException e) {
50                         throw new MappingException(e);
51                 }
52         }
53         
54         @Override
55         public boolean set(WriteGraph g, Resource element, Object value)
56                         throws MappingException {
57                 try {
58                     LOGGER.info("        RelatedValueAccessor.set");
59                     Statement valueStatement = g.getPossibleStatement(element, relation);
60                         if(valueStatement == null) {
61                                 if(value == null)
62                                         return false;
63                                 Resource valueResource = g.newResource();
64                                 g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null,
65                                                 valueType);
66                                 g.claim(element, relation, valueResource);
67                                 g.claimValue(valueResource, value);                             
68                                 return true;
69                         }
70                         else {
71                                 if(value == null) {
72                                         if (!valueStatement.isAsserted(element)) {
73                                                 g.deny(valueStatement.getObject());
74                                                 return true;
75                                         } else {
76                                                 return false;
77                                         }
78                                 }                               
79                                 Object currentValue = g.getValue(valueStatement.getObject());
80                                 if(equals(currentValue,value))
81                                         return false;
82                                 if (!valueStatement.isAsserted(element))
83                                         g.claimValue(valueStatement.getObject(), value);
84                                 else {
85                                         Resource valueResource = g.newResource();
86                                         g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null,
87                                                         valueType);
88                                         g.claim(element, relation, valueResource);
89                                         g.claimValue(valueResource, value);
90                                 }
91                                 return true;
92                         }
93                 } catch (DatabaseException e) {
94                         throw new MappingException(e);
95                 }
96                 
97         }
98         
99         private boolean equals(Object o1, Object o2) {
100                 if (o1 instanceof boolean[])
101                         Arrays.equals((boolean[])o1,(boolean[])o2);
102                 if (o1 instanceof int[])
103                         Arrays.equals((int[])o1,(int[])o2);
104                 if (o1 instanceof float[])
105                         Arrays.equals((float[])o1,(float[])o2);
106                 if (o1 instanceof double[])
107                         Arrays.equals((double[])o1,(double[])o2);
108                 if (o1 instanceof byte[])
109                         Arrays.equals((byte[])o1,(byte[])o2);
110                 return o1.equals(o2);
111                 
112         }
113
114 }