]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/SimpleLinkType.java
fcfb3b012a802439398bc6d020325e9875494f38
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / 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.graph.schema;
13
14 import java.util.ArrayList;
15
16 import org.apache.log4j.Logger;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.common.utils.NameUtils;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.objmap.backward.IBackwardMapping;
24 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;
25 import org.simantics.objmap.exceptions.MappingException;
26 import org.simantics.objmap.forward.IForwardMapping;
27
28
29 /**
30  * A link type that is associated with single domain and range type (class).
31  * SimpleLinkType is composed of simpler rules whose combination determines
32  * its update policy.
33  * @author Hannu Niemist�
34  */
35 public class SimpleLinkType<Range> implements ILinkType<Resource,Range> {
36     
37     static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
38     
39     public Resource domainType;
40     public Class<?> rangeType;
41     ArrayList<IBidirectionalMappingRule<Resource, Range>> rules;
42     
43     public SimpleLinkType(Resource domainType, Class<?> rangeType,
44             ArrayList<IBidirectionalMappingRule<Resource, Range>> 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<Resource, Range>>());
52     }
53
54     /**
55      * Adds a new rule to this link type that is enforced
56      * during updates.
57      */
58     public void addRule(IBidirectionalMappingRule<Resource, Range> rule) {
59         rules.add(rule);
60     }
61     
62     @Override
63     public Resource createDomainElement(WriteGraph g, Range rangeElement)
64             throws MappingException {
65         try {
66             if(LOGGER.isInfoEnabled())
67                 LOGGER.info("SimpleLinkType.createDomainElement " +
68                         rangeElement.toString()
69                 );
70             Resource result = g.newResource();
71             g.claim(result, Layer0.getInstance(g).InstanceOf, null, domainType);
72             return result;
73         } catch(DatabaseException e) {
74             throw new MappingException(e);
75         }
76     }
77     @SuppressWarnings("unchecked")
78         @Override
79     public Range createRangeElement(ReadGraph g, Resource domainElement)
80             throws MappingException {
81         try {
82             if(LOGGER.isInfoEnabled())
83                 try { 
84                     LOGGER.info("SimpleLinkType.createRangeElement " +
85                                 NameUtils.getSafeName(g, domainElement)
86                             );
87                 } catch(DatabaseException e) {
88                     throw new MappingException(e);
89                 }
90             return (Range)rangeType.newInstance();
91         } catch (InstantiationException e) {
92             throw new MappingException(e);
93         } catch (IllegalAccessException e) {
94             throw new MappingException(e);
95         }
96     }
97     
98     public void createDomain(WriteGraph graph, IBackwardMapping<Resource,Range> mapping, Resource domainElement, Range rangeElement) throws MappingException {
99         updateDomain(graph, mapping, domainElement, rangeElement);
100     };
101     
102     public void createRange(ReadGraph graph, org.simantics.objmap.forward.IForwardMapping<Resource,Range> mapping, Resource domainElement, Range rangeElement) throws MappingException {
103         updateRange(graph, mapping, domainElement, rangeElement);
104     };
105     
106     public boolean updateDomain(WriteGraph g, IBackwardMapping<Resource,Range> map, Resource domainElement, Range rangeElement) throws MappingException {
107         if(LOGGER.isInfoEnabled())
108             try { 
109                 LOGGER.info("SimpleLinkType.updateDomain " +
110                         NameUtils.getSafeName(g, domainElement) + " " +
111                         rangeElement.toString()
112                         );
113             } catch(DatabaseException e) {
114                 throw new MappingException(e);
115             }
116         
117         boolean updated = false;
118         for(IBidirectionalMappingRule<Resource, Range> rule : rules)
119                 updated |= rule.updateDomain(g, map, domainElement, rangeElement);
120         return updated;
121     }
122     
123     public boolean updateRange(ReadGraph g, IForwardMapping<Resource,Range> map, Resource domainElement, Range rangeElement) throws MappingException {
124     
125         if(LOGGER.isInfoEnabled())
126             try { 
127                 LOGGER.info("SimpleLinkType.updateRange " +
128                                 NameUtils.getSafeName(g, domainElement) + " " +
129                         rangeElement.toString()
130                         );
131             } catch(DatabaseException e) {
132                 throw new MappingException(e);
133             }
134         
135         boolean updated = false;
136         for(IBidirectionalMappingRule<Resource, Range> rule : rules)
137             updated |= rule.updateRange(g, map, domainElement, rangeElement);
138         return updated;
139     }
140 }