1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 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 = g.newResource();
\r
73 g.claim(result, Layer0.getInstance(g).InstanceOf, null, domainType);
\r
74 return new StructuralResource(g,result);
\r
76 if (rangeElement.getContext().size() == 1 && rangeElement.getContext().get(0).equals(rangeElement)) {
\r
77 // Structural object's context is itself, we are instantiating a new structural model.
\r
78 Resource type = rangeElement.getType();
\r
79 Resource result = g.newResource();
\r
80 g.claim(result, Layer0.getInstance(g).InstanceOf, null, type);
\r
81 return new StructuralResource(g,result,result);
\r
83 // Structural object's context is not itself, which means that the object is inside of a structural model.
\r
84 // At the moment we do not support modifying instantiated structural models.
\r
85 throw new MappingException("Cannot create a new StucturalObject " + rangeElement + " " + rangeElement.getClass());
\r
88 } catch(DatabaseException e) {
\r
89 throw new MappingException(e);
\r
93 public IStructuralObject createRangeElement(ReadGraph g, StructuralResource domainElement)
\r
94 throws MappingException {
\r
96 if(LOGGER.isInfoEnabled())
\r
98 LOGGER.info("SimpleLinkType.createRangeElement " + NameUtils.getSafeName(g, domainElement.getResource()));
\r
99 } catch(DatabaseException e) {
\r
100 throw new MappingException(e);
\r
102 IStructuralObject result = (IStructuralObject)rangeType.newInstance();
\r
103 if (domainElement.getContext().size() == 1) {
\r
104 if (domainElement.getContext().get(0).equals(domainElement.getResource()))
\r
105 result.setContext(Collections.singletonList(result));
\r
107 //result.setContext(result);
\r
111 } catch (InstantiationException e) {
\r
112 throw new MappingException(e);
\r
113 } catch (IllegalAccessException e) {
\r
114 throw new MappingException(e);
\r
118 @SuppressWarnings("unchecked")
\r
119 public void createDomain(WriteGraph graph, IBackwardMapping<StructuralResource,IStructuralObject> mapping, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
\r
120 if (domainElement.isStructuralRoot())
\r
121 // 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
122 // 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
124 updateRange(graph, (IForwardMapping<StructuralResource, IStructuralObject>)mapping, domainElement, rangeElement);
\r
125 updateDomain(graph, mapping, domainElement, rangeElement);
\r
130 public void createRange(ReadGraph graph, IForwardMapping<StructuralResource, IStructuralObject> mapping, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
\r
131 if (rangeElement.getContext().size() == 0 && domainElement.getContext().size() > 0) {
\r
132 List<IStructuralObject> ctx = new ArrayList<IStructuralObject>(domainElement.getContext().size());
\r
134 List<Resource> context = new ArrayList<Resource>();
\r
135 for (int i = 0; i <domainElement.getContext().size(); i++) {
\r
136 context.add(domainElement.getContext().get(i));
\r
137 IStructuralObject ctxObj = mapping.get(new StructuralResource(graph,context.get(context.size()-1),context));
\r
138 if (ctxObj == null) throw new MappingException("Cannot resolve range context for domain element " + domainElement);
\r
141 //ctx = mapping.get(new StructuralResource(graph,context.get(context.size()-1),context));
\r
142 } catch (DatabaseException e) {
\r
143 throw new MappingException(e);
\r
145 if (ctx.size() == 0)
\r
146 throw new MappingException("Cannot find context for structural object, " + domainElement);
\r
147 rangeElement.setContext(ctx);
\r
149 updateRange(graph, mapping, domainElement, rangeElement);
\r
152 public boolean updateDomain(WriteGraph g, IBackwardMapping<StructuralResource,IStructuralObject> map, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
\r
153 if(LOGGER.isInfoEnabled())
\r
155 LOGGER.info("SimpleLinkType.updateDomain " +
\r
156 NameUtils.getSafeName(g, domainElement.getResource()) + " " +
\r
157 rangeElement.toString()
\r
159 } catch(DatabaseException e) {
\r
160 throw new MappingException(e);
\r
163 boolean updated = false;
\r
164 for(IBidirectionalMappingRule<StructuralResource, IStructuralObject> rule : rules)
\r
165 updated |= rule.updateDomain(g, map, domainElement, rangeElement);
\r
169 public boolean updateRange(ReadGraph g, IForwardMapping<StructuralResource, IStructuralObject> map, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
\r
171 if(LOGGER.isInfoEnabled())
\r
173 LOGGER.info("SimpleLinkType.updateRange " +
\r
174 NameUtils.getSafeName(g, domainElement.getResource()) + " " +
\r
175 rangeElement.toString()
\r
177 } catch(DatabaseException e) {
\r
178 throw new MappingException(e);
\r
181 boolean updated = false;
\r
182 for(IBidirectionalMappingRule<StructuralResource, IStructuralObject> rule : rules)
\r
183 updated |= rule.updateRange(g, map, domainElement, rangeElement);
\r