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.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;
28 public class RelatedValueAccessor implements IDomainAccessor<StructuralResource,Object> {
30 static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
34 boolean useTypeResource;
36 private boolean preventStructuralChanges = true;
37 private boolean preventStructuralRootChanges = true;
39 public RelatedValueAccessor(Resource relation, Resource valueType, boolean useTypeResource) {
40 this.relation = relation;
41 this.valueType = valueType;
42 this.useTypeResource = useTypeResource;
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;
53 private boolean preventChange(StructuralResource element) {
54 return preventStructuralChanges && element.isStructural() && (!element.isStructuralRoot()||preventStructuralRootChanges);
58 public Object get(ReadGraph g, StructuralResource element) throws MappingException {
60 LOGGER.info(" RelatedValueAccessor.get");
61 Resource res = getServiceResource(g, element);
64 Resource valueResource = g.getPossibleObject(res, relation);
65 if(valueResource == null)
67 return g.getValue(valueResource);
68 } catch (DatabaseException e) {
69 throw new MappingException(e);
74 public boolean set(WriteGraph g, StructuralResource relement, Object value)
75 throws MappingException {
77 LOGGER.info(" RelatedValueAccessor.set");
79 Resource element = getServiceResource(g, relement);
82 Statement valueStatement = g.getPossibleStatement(element, relation);
83 if(valueStatement == null) {
86 if (preventChange(relement))
88 Resource valueResource = g.newResource();
89 g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null,
91 g.claim(element, relation, valueResource);
92 g.claimValue(valueResource, value);
97 if (preventChange(relement))
99 if (!valueStatement.isAsserted(element)) {
100 g.deny(valueStatement.getObject());
107 Object currentValue = g.getValue(valueStatement.getObject());
108 if(equals(currentValue,value))
110 if (preventChange(relement))
112 if (!valueStatement.isAsserted(element))
113 g.claimValue(valueStatement.getObject(), value);
115 Resource valueResource = g.newResource();
116 g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null,
118 g.claim(element, relation, valueResource);
119 g.claimValue(valueResource, value);
123 } catch (DatabaseException e) {
124 throw new MappingException(e);
129 private Resource getServiceResource(ReadGraph g, StructuralResource element) {
130 if (!useTypeResource)
131 return element.getResource();
132 return element.getTypeResource();
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);