]> gerrit.simantics Code Review - simantics/platform.git/blob
53eb0116829fe8181c61dc1147bf96d7b7cf78d9
[simantics/platform.git] /
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.Collection;
15 import java.util.HashMap;
16 import java.util.Map;
17
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;
30
31 /**
32  * 
33  * @author Marko Luukkainen
34  */
35 public class CompoundValueAccessor implements IDomainAccessor<Resource,Object> {
36
37     static final Logger LOGGER = LoggerFactory.getLogger(CompoundValueAccessor.class);
38     
39         Resource objRelation;
40         Resource objType;
41         Resource valRelation;
42         Binding valueBinding;
43
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;
49         }
50
51         @Override
52         public Object get(ReadGraph g, Resource element) throws MappingException {
53                 try {
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());
62                                         map.put(name, value);
63                                 }
64                     }
65                         return map;
66                 } catch (DatabaseException e) {
67                         throw new MappingException(e);
68                 }
69         }
70
71         @Override
72         public boolean set(WriteGraph g, Resource element, Object v)
73                         throws MappingException {
74                 try {
75                         Layer0 l0 = Layer0.getInstance(g);
76                     LOGGER.info("        CompoundValueAccessor.set");
77                     @SuppressWarnings("unchecked")
78                     Map<String,Object> values = (Map<String, Object>)v;
79                     
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)) {
86                                 stmMap.put(name, c);
87                                 valueMap.put(name, g.getRelatedValue(c.getObject(), valRelation));
88                         }
89                     }
90                     
91                     boolean changed = false;
92                     for (String key : values.keySet()) {
93                         Object value = values.get(key);
94                         if (value.equals(valueMap.get(key)))
95                                 continue;
96                         changed = true;
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);
104                         }
105                         
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());
110                                         
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);
116                                 } else {
117                                         if (!valueStatement.isAsserted(stm.getObject()))
118                                                 g.claimValue(valueStatement.getObject(), value);
119                                         else {
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);
124                                         }
125                                 }
126                     }
127                     
128                     return changed;
129                 } catch (DatabaseException e) {
130                         throw new MappingException(e);
131                 }
132                 
133         }
134
135         private void claimValue(WriteGraph g, Resource valueResource, Object value) throws ServiceException {
136                 if (valueBinding != null)
137                         g.claimValue(valueResource, value, valueBinding);
138                 else
139                         g.claimValue(valueResource, value);
140         }
141         
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))
145                                 return stm;
146                 }
147                 return null;
148         }
149         
150         private Object getValue(ReadGraph g, final Resource object) throws DatabaseException {
151                 return valueBinding != null ? g.getRelatedValue(object, valRelation, valueBinding) : g.getRelatedValue(object, valRelation);
152         }
153
154 }