]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/schema/SimpleLinkType.java
8a7091fd46e828ff2774df30265d7ff719bee8e4
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / structural / schema / SimpleLinkType.java
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.structural.schema;
13
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17
18 import org.apache.log4j.Logger;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.common.utils.NameUtils;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.layer0.Layer0;
25 import org.simantics.objmap.backward.IBackwardMapping;
26 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;
27 import org.simantics.objmap.exceptions.MappingException;
28 import org.simantics.objmap.forward.IForwardMapping;
29 import org.simantics.objmap.graph.schema.ILinkType;
30 import org.simantics.objmap.structural.IStructuralObject;
31 import org.simantics.objmap.structural.StructuralResource;
32
33
34
35 public class SimpleLinkType implements ILinkType<StructuralResource,IStructuralObject> {
36     
37     static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
38     
39     public Resource domainType;
40     public Class<?> rangeType;
41     ArrayList<IBidirectionalMappingRule<StructuralResource,IStructuralObject>> rules;
42     
43     public SimpleLinkType(Resource domainType, Class<?> rangeType,
44             ArrayList<IBidirectionalMappingRule<StructuralResource,IStructuralObject>> rules) {
45         this.domainType = domainType;
46         this.rangeType = rangeType;
47         this.rules = rules;
48     }
49
50     public SimpleLinkType(Resource domainType, Class<?> rangeType) {
51         this(domainType, rangeType, new ArrayList<IBidirectionalMappingRule<StructuralResource,IStructuralObject>>());
52     }
53
54     /**
55      * Adds a new rule to this link type that is enforced
56      * during updates.
57      */
58     public void addRule(IBidirectionalMappingRule<StructuralResource,IStructuralObject> rule) {
59         rules.add(rule);
60     }
61     
62     @Override
63     public StructuralResource createDomainElement(WriteGraph g, IStructuralObject rangeElement)
64             throws MappingException {
65         try {
66             if(LOGGER.isInfoEnabled())
67                 LOGGER.info("SimpleLinkType.createDomainElement " +
68                         rangeElement.toString()
69                 );
70             if (rangeElement.getContext().size() == 0) {
71                 // there is no context, this not a structural resource / object.
72                 Resource result = newResource(g, domainType);
73                 return new StructuralResource(g,result);
74             } else {
75                 if (rangeElement.getContext().size() == 1 && rangeElement.getContext().get(0).equals(rangeElement)) {
76                         // Structural object's context is itself, we are instantiating a new structural model.
77                         Resource type = rangeElement.getType();
78                         Resource result = newResource(g, type);
79                         return new StructuralResource(g,result,result);
80                 } else {
81                         // Structural object's context is not itself, which means that the object is inside of a structural model.
82                         // At the moment we do not support modifying instantiated structural models.
83                         throw new MappingException("Cannot create a new StucturalObject " + rangeElement + " " + rangeElement.getClass());
84                 }
85             }
86         } catch(DatabaseException e) {
87             throw new MappingException(e);
88         }
89     }
90     
91     protected Resource newResource(WriteGraph g, Resource type) throws DatabaseException {
92         Resource result = g.newResource();
93         g.claim(result, Layer0.getInstance(g).InstanceOf, null, type);
94         return result;
95     }
96     @Override
97     public IStructuralObject createRangeElement(ReadGraph g, StructuralResource domainElement)
98             throws MappingException {
99         try {
100             if(LOGGER.isInfoEnabled())
101                 try { 
102                     LOGGER.info("SimpleLinkType.createRangeElement " + NameUtils.getSafeName(g, domainElement.getResource()));
103                 } catch(DatabaseException e) {
104                     throw new MappingException(e);
105                 }
106             IStructuralObject result = (IStructuralObject)rangeType.newInstance();
107             if (domainElement.getContext().size() == 1) {
108                 if (domainElement.getContext().get(0).equals(domainElement.getResource()))
109                         result.setContext(Collections.singletonList(result));
110                 else {
111                         //result.setContext(result); 
112                 }
113             }
114             return result;
115         } catch (InstantiationException e) {
116             throw new MappingException(e);
117         } catch (IllegalAccessException e) {
118             throw new MappingException(e);
119         }
120     }
121     
122     @SuppressWarnings("unchecked")
123         public void createDomain(WriteGraph graph, IBackwardMapping<StructuralResource,IStructuralObject> mapping, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
124         if (domainElement.isStructuralRoot())
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.
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).
127                 // 
128                 updateRange(graph, (IForwardMapping<StructuralResource, IStructuralObject>)mapping, domainElement, rangeElement);
129         updateDomain(graph, mapping, domainElement, rangeElement);
130     };
131     
132     
133     @Override
134     public void createRange(ReadGraph graph, IForwardMapping<StructuralResource, IStructuralObject> mapping, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
135         if (rangeElement.getContext().size() == 0 && domainElement.getContext().size() > 0) {
136                 List<IStructuralObject> ctx = new ArrayList<IStructuralObject>(domainElement.getContext().size());
137                         try {
138                                 List<Resource> context = new ArrayList<Resource>();
139                                 for (int i = 0; i <domainElement.getContext().size(); i++) {
140                                         context.add(domainElement.getContext().get(i));
141                                         IStructuralObject ctxObj = mapping.get(new StructuralResource(graph,context.get(context.size()-1),context));
142                                         if (ctxObj == null) throw new MappingException("Cannot resolve range context for domain element " + domainElement);
143                                         ctx.add(ctxObj);
144                                 }
145                                 //ctx = mapping.get(new StructuralResource(graph,context.get(context.size()-1),context));
146                         } catch (DatabaseException e) {
147                                 throw new MappingException(e);
148                         }
149                 if (ctx.size() == 0)
150                         throw new MappingException("Cannot find context for structural object, " + domainElement);
151                 rangeElement.setContext(ctx);
152         }
153         updateRange(graph, mapping, domainElement, rangeElement);
154     }
155     
156     public boolean updateDomain(WriteGraph g, IBackwardMapping<StructuralResource,IStructuralObject> map, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
157         if(LOGGER.isInfoEnabled())
158             try { 
159                 LOGGER.info("SimpleLinkType.updateDomain " +
160                         NameUtils.getSafeName(g, domainElement.getResource()) + " " +
161                         rangeElement.toString()
162                         );
163             } catch(DatabaseException e) {
164                 throw new MappingException(e);
165             }
166         
167         boolean updated = false;
168         for(IBidirectionalMappingRule<StructuralResource, IStructuralObject> rule : rules)
169                 updated |= rule.updateDomain(g, map, domainElement, rangeElement);
170         return updated;
171     }
172     
173     public boolean updateRange(ReadGraph g, IForwardMapping<StructuralResource, IStructuralObject> map, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
174     
175         if(LOGGER.isInfoEnabled())
176             try { 
177                 LOGGER.info("SimpleLinkType.updateRange " +
178                                 NameUtils.getSafeName(g, domainElement.getResource()) + " " +
179                         rangeElement.toString()
180                         );
181             } catch(DatabaseException e) {
182                 throw new MappingException(e);
183             }
184         
185         boolean updated = false;
186         for(IBidirectionalMappingRule<StructuralResource, IStructuralObject> rule : rules)
187             updated |= rule.updateRange(g, map, domainElement, rangeElement);
188         return updated;
189     }
190 }