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.graph.rules.domain;
14 import java.util.Collection;
15 import java.util.HashMap;
18 import org.simantics.databoard.binding.Binding;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.Statement;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.exception.ServiceException;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.objmap.exceptions.MappingException;
27 import org.simantics.objmap.graph.annotations.factories.DataTypeUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
33 * @author Marko Luukkainen
35 public class CompoundValueAccessor implements IDomainAccessor<Resource,Object> {
37 static final Logger LOGGER = LoggerFactory.getLogger(CompoundValueAccessor.class);
44 public CompoundValueAccessor(Resource objRelation, Resource objType, Resource valRelation, Binding valueBinding) {
45 this.objRelation = objRelation;
46 this.objType = objType;
47 this.valRelation = valRelation;
48 this.valueBinding = valueBinding;
52 public Object get(ReadGraph g, Resource element) throws MappingException {
54 Layer0 l0 = Layer0.getInstance(g);
55 LOGGER.info(" CompoundValueAccessor.get");
56 Collection<Statement> coll = g.getStatements(element, objRelation);
57 Map<String,Object> map = new HashMap<String, Object>();
58 for (Statement c : coll) {
59 String name = g.getRelatedValue(c.getObject(), l0.HasName);
60 if (!map.containsKey(name) || !c.isAsserted(element)) {
61 final Object value = getValue(g, c.getObject());
66 } catch (DatabaseException e) {
67 throw new MappingException(e);
72 public boolean set(WriteGraph g, Resource element, Object v)
73 throws MappingException {
75 Layer0 l0 = Layer0.getInstance(g);
76 LOGGER.info(" CompoundValueAccessor.set");
77 @SuppressWarnings("unchecked")
78 Map<String,Object> values = (Map<String, Object>)v;
80 Collection<Statement> coll = g.getStatements(element, objRelation);
81 Map<String,Statement> stmMap = new HashMap<String, Statement>();
82 Map<String,Object> valueMap = new HashMap<String, Object>();
83 for (Statement c : coll) {
84 String name = g.getRelatedValue(c.getObject(), l0.HasName);
85 if (!stmMap.containsKey(name) || !c.isAsserted(element)) {
87 valueMap.put(name, g.getRelatedValue(c.getObject(), valRelation));
91 boolean changed = false;
92 for (String key : values.keySet()) {
93 Object value = values.get(key);
94 if (value.equals(valueMap.get(key)))
97 Statement stm = stmMap.get(key);
98 if (stm == null || stm.isAsserted(element)) {
99 Resource obj = g.newResource();
100 g.claim(obj, l0.InstanceOf, objType);
101 g.claimLiteral(obj, l0.HasName, key);
102 g.claim(element, objRelation, obj);
103 stm = getStatement(g, element, objRelation, obj);
106 Statement valueStatement = g.getPossibleStatement(stm.getObject(), valRelation);
107 Resource valueType = valueBinding != null ?
108 DataTypeUtils.dataTypeOfDatatype(g, valueBinding.type()) :
109 DataTypeUtils.dataTypeOfClass(g, value.getClass());
111 if(valueStatement == null) {
112 Resource valueResource = g.newResource();
113 g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null, valueType);
114 g.claim(stm.getObject(), valRelation, valueResource);
115 claimValue(g, valueResource, value);
117 if (!valueStatement.isAsserted(stm.getObject()))
118 g.claimValue(valueStatement.getObject(), value);
120 Resource valueResource = g.newResource();
121 g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null, valueType);
122 g.claim(stm.getObject(), valRelation, valueResource);
123 claimValue(g, valueResource, value);
129 } catch (DatabaseException e) {
130 throw new MappingException(e);
135 private void claimValue(WriteGraph g, Resource valueResource, Object value) throws ServiceException {
136 if (valueBinding != null)
137 g.claimValue(valueResource, value, valueBinding);
139 g.claimValue(valueResource, value);
142 private Statement getStatement(ReadGraph g, Resource s, Resource p, Resource o) throws DatabaseException{
143 for (Statement stm : g.getStatements(s, p)) {
144 if (stm.getObject().equals(o))
150 private Object getValue(ReadGraph g, final Resource object) throws DatabaseException {
151 return valueBinding != null ? g.getRelatedValue(object, valRelation, valueBinding) : g.getRelatedValue(object, valRelation);