1 /*******************************************************************************
2 * Copyright (c) 2007, 2013 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.objmap.structural.rules.domain;
14 import java.util.Arrays;
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 import org.simantics.objmap.graph.rules.domain.IDomainAccessor;
26 import org.simantics.objmap.structural.StructuralResource;
29 public class RelatedValueAccessor implements IDomainAccessor<StructuralResource,Object> {
31 static Logger LOGGER = LoggerFactory.getLogger(RelatedValueAccessor.class);
35 boolean useTypeResource;
37 private boolean preventStructuralChanges = true;
38 private boolean preventStructuralRootChanges = true;
40 public RelatedValueAccessor(Resource relation, Resource valueType, boolean useTypeResource) {
41 this.relation = relation;
42 this.valueType = valueType;
43 this.useTypeResource = useTypeResource;
46 public RelatedValueAccessor(Resource relation, Resource valueType, boolean useTypeResource, boolean preventStructuralChanges, boolean preventStructuralRootChanges) {
47 this.relation = relation;
48 this.valueType = valueType;
49 this.useTypeResource = useTypeResource;
50 this.preventStructuralChanges = preventStructuralChanges;
51 this.preventStructuralRootChanges = preventStructuralRootChanges;
54 private boolean preventChange(StructuralResource element) {
55 return preventStructuralChanges && element.isStructural() && (!element.isStructuralRoot()||preventStructuralRootChanges);
59 public Object get(ReadGraph g, StructuralResource element) throws MappingException {
61 LOGGER.info(" RelatedValueAccessor.get");
62 Resource res = getServiceResource(g, element);
65 Resource valueResource = g.getPossibleObject(res, relation);
66 if(valueResource == null)
68 return g.getValue(valueResource);
69 } catch (DatabaseException e) {
70 throw new MappingException(e);
75 public boolean set(WriteGraph g, StructuralResource relement, Object value)
76 throws MappingException {
78 LOGGER.info(" RelatedValueAccessor.set");
80 Resource element = getServiceResource(g, relement);
83 Statement valueStatement = g.getPossibleStatement(element, relation);
84 if(valueStatement == null) {
87 if (preventChange(relement))
89 Resource valueResource = g.newResource();
90 g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null,
92 g.claim(element, relation, valueResource);
93 g.claimValue(valueResource, value);
98 if (preventChange(relement))
100 if (!valueStatement.isAsserted(element)) {
101 g.deny(valueStatement.getObject());
108 Object currentValue = g.getValue(valueStatement.getObject());
109 if(equals(currentValue,value))
111 if (preventChange(relement))
113 if (!valueStatement.isAsserted(element))
114 g.claimValue(valueStatement.getObject(), value);
116 Resource valueResource = g.newResource();
117 g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null,
119 g.claim(element, relation, valueResource);
120 g.claimValue(valueResource, value);
124 } catch (DatabaseException e) {
125 throw new MappingException(e);
130 private Resource getServiceResource(ReadGraph g, StructuralResource element) {
131 if (!useTypeResource)
132 return element.getResource();
133 return element.getTypeResource();
136 private boolean equals(Object o1, Object o2) {
137 if (o1 instanceof boolean[])
138 Arrays.equals((boolean[])o1,(boolean[])o2);
139 if (o1 instanceof int[])
140 Arrays.equals((int[])o1,(int[])o2);
141 if (o1 instanceof float[])
142 Arrays.equals((float[])o1,(float[])o2);
143 if (o1 instanceof double[])
144 Arrays.equals((double[])o1,(double[])o2);
145 if (o1 instanceof byte[])
146 Arrays.equals((byte[])o1,(byte[])o2);
147 return o1.equals(o2);