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