1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2013 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.objmap.structural.schema;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collections;
\r
16 import java.util.List;
\r
18 import org.apache.log4j.Logger;
\r
19 import org.simantics.db.ReadGraph;
\r
20 import org.simantics.db.Resource;
\r
21 import org.simantics.db.WriteGraph;
\r
22 import org.simantics.db.common.utils.NameUtils;
\r
23 import org.simantics.db.exception.DatabaseException;
\r
24 import org.simantics.layer0.Layer0;
\r
25 import org.simantics.objmap.backward.IBackwardMapping;
\r
26 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;
\r
27 import org.simantics.objmap.exceptions.MappingException;
\r
28 import org.simantics.objmap.forward.IForwardMapping;
\r
29 import org.simantics.objmap.graph.schema.ILinkType;
\r
30 import org.simantics.objmap.structural.IStructuralObject;
\r
31 import org.simantics.objmap.structural.StructuralResource;
\r
35 public class SimpleLinkType implements ILinkType<StructuralResource,IStructuralObject> {
\r
37 static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
\r
39 public Resource domainType;
\r
40 public Class<?> rangeType;
\r
41 ArrayList<IBidirectionalMappingRule<StructuralResource,IStructuralObject>> rules;
\r
43 public SimpleLinkType(Resource domainType, Class<?> rangeType,
\r
44 ArrayList<IBidirectionalMappingRule<StructuralResource,IStructuralObject>> rules) {
\r
45 this.domainType = domainType;
\r
46 this.rangeType = rangeType;
\r
50 public SimpleLinkType(Resource domainType, Class<?> rangeType) {
\r
51 this(domainType, rangeType, new ArrayList<IBidirectionalMappingRule<StructuralResource,IStructuralObject>>());
\r
55 * Adds a new rule to this link type that is enforced
\r
58 public void addRule(IBidirectionalMappingRule<StructuralResource,IStructuralObject> rule) {
\r
63 public StructuralResource createDomainElement(WriteGraph g, IStructuralObject rangeElement)
\r
64 throws MappingException {
\r
66 if(LOGGER.isInfoEnabled())
\r
67 LOGGER.info("SimpleLinkType.createDomainElement " +
\r
68 rangeElement.toString()
\r
70 if (rangeElement.getContext().size() == 0) {
\r
71 // there is no context, this not a structural resource / object.
\r
72 Resource result = newResource(g, domainType);
\r
73 return new StructuralResource(g,result);
\r
75 if (rangeElement.getContext().size() == 1 && rangeElement.getContext().get(0).equals(rangeElement)) {
\r
76 // Structural object's context is itself, we are instantiating a new structural model.
\r
77 Resource type = rangeElement.getType();
\r
78 Resource result = newResource(g, type);
\r
79 return new StructuralResource(g,result,result);
\r
81 // Structural object's context is not itself, which means that the object is inside of a structural model.
\r
82 // At the moment we do not support modifying instantiated structural models.
\r
83 throw new MappingException("Cannot create a new StucturalObject " + rangeElement + " " + rangeElement.getClass());
\r
86 } catch(DatabaseException e) {
\r
87 throw new MappingException(e);
\r
91 protected Resource newResource(WriteGraph g, Resource type) throws DatabaseException {
\r
92 Resource result = g.newResource();
\r
93 g.claim(result, Layer0.getInstance(g).InstanceOf, null, type);
\r
97 public IStructuralObject createRangeElement(ReadGraph g, StructuralResource domainElement)
\r
98 throws MappingException {
\r
100 if(LOGGER.isInfoEnabled())
\r
102 LOGGER.info("SimpleLinkType.createRangeElement " + NameUtils.getSafeName(g, domainElement.getResource()));
\r
103 } catch(DatabaseException e) {
\r
104 throw new MappingException(e);
\r
106 IStructuralObject result = (IStructuralObject)rangeType.newInstance();
\r
107 if (domainElement.getContext().size() == 1) {
\r
108 if (domainElement.getContext().get(0).equals(domainElement.getResource()))
\r
109 result.setContext(Collections.singletonList(result));
\r
111 //result.setContext(result);
\r
115 } catch (InstantiationException e) {
\r
116 throw new MappingException(e);
\r
117 } catch (IllegalAccessException e) {
\r
118 throw new MappingException(e);
\r
122 @SuppressWarnings("unchecked")
\r
123 public void createDomain(WriteGraph graph, IBackwardMapping<StructuralResource,IStructuralObject> mapping, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
\r
124 if (domainElement.isStructuralRoot())
\r
125 // FIXME: this is nasty, but when a structural model is instantiated by creating new IStructuralObject, its related objects must be read from the graph first, or otherwise the objects would be deleted from the graph.
\r
126 // as a side effect, if the new IStructuralObject has any properties set, those properties are set to null (because the graph does not contain those values).
\r
128 updateRange(graph, (IForwardMapping<StructuralResource, IStructuralObject>)mapping, domainElement, rangeElement);
\r
129 updateDomain(graph, mapping, domainElement, rangeElement);
\r
134 public void createRange(ReadGraph graph, IForwardMapping<StructuralResource, IStructuralObject> mapping, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
\r
135 if (rangeElement.getContext().size() == 0 && domainElement.getContext().size() > 0) {
\r
136 List<IStructuralObject> ctx = new ArrayList<IStructuralObject>(domainElement.getContext().size());
\r
138 List<Resource> context = new ArrayList<Resource>();
\r
139 for (int i = 0; i <domainElement.getContext().size(); i++) {
\r
140 context.add(domainElement.getContext().get(i));
\r
141 IStructuralObject ctxObj = mapping.get(new StructuralResource(graph,context.get(context.size()-1),context));
\r
142 if (ctxObj == null) throw new MappingException("Cannot resolve range context for domain element " + domainElement);
\r
145 //ctx = mapping.get(new StructuralResource(graph,context.get(context.size()-1),context));
\r
146 } catch (DatabaseException e) {
\r
147 throw new MappingException(e);
\r
149 if (ctx.size() == 0)
\r
150 throw new MappingException("Cannot find context for structural object, " + domainElement);
\r
151 rangeElement.setContext(ctx);
\r
153 updateRange(graph, mapping, domainElement, rangeElement);
\r
156 public boolean updateDomain(WriteGraph g, IBackwardMapping<StructuralResource,IStructuralObject> map, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
\r
157 if(LOGGER.isInfoEnabled())
\r
159 LOGGER.info("SimpleLinkType.updateDomain " +
\r
160 NameUtils.getSafeName(g, domainElement.getResource()) + " " +
\r
161 rangeElement.toString()
\r
163 } catch(DatabaseException e) {
\r
164 throw new MappingException(e);
\r
167 boolean updated = false;
\r
168 for(IBidirectionalMappingRule<StructuralResource, IStructuralObject> rule : rules)
\r
169 updated |= rule.updateDomain(g, map, domainElement, rangeElement);
\r
173 public boolean updateRange(ReadGraph g, IForwardMapping<StructuralResource, IStructuralObject> map, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
\r
175 if(LOGGER.isInfoEnabled())
\r
177 LOGGER.info("SimpleLinkType.updateRange " +
\r
178 NameUtils.getSafeName(g, domainElement.getResource()) + " " +
\r
179 rangeElement.toString()
\r
181 } catch(DatabaseException e) {
\r
182 throw new MappingException(e);
\r
185 boolean updated = false;
\r
186 for(IBidirectionalMappingRule<StructuralResource, IStructuralObject> rule : rules)
\r
187 updated |= rule.updateRange(g, map, domainElement, rangeElement);
\r