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