]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/rules/domain/RelatedObjectsAccessor.java
cbd4ef3b40c9e5f1fae6b5e0159eb98e3f2e68cd
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / structural / rules / domain / RelatedObjectsAccessor.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.rules.domain;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
18
19 import org.apache.log4j.Logger;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.objmap.exceptions.MappingException;
25 import org.simantics.objmap.graph.rules.domain.IDomainAccessor;
26 import org.simantics.objmap.graph.rules.domain.MappingUtils;
27 import org.simantics.objmap.structural.StructuralResource;
28
29
30 public class RelatedObjectsAccessor implements IDomainAccessor<StructuralResource,Collection<StructuralResource>> {
31
32     static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
33     
34         Resource relation;
35         boolean deleteExtraObjects;
36         boolean useTypeResource;
37         
38         private boolean preventStructuralChanges = true;
39         private boolean preventStructuralRootChanges = true;
40
41         public RelatedObjectsAccessor(Resource relation, boolean deleteExtraObjects, boolean useTypeResource) {
42         super();
43         this.relation = relation;
44         this.deleteExtraObjects = deleteExtraObjects;
45         this.useTypeResource = useTypeResource;
46     }
47         
48         public RelatedObjectsAccessor(Resource relation, boolean deleteExtraObjects, boolean useTypeResource, boolean preventStructuralChanges, boolean preventStructuralRootChanges) {
49         super();
50         this.relation = relation;
51         this.deleteExtraObjects = deleteExtraObjects;
52         this.useTypeResource = useTypeResource;
53                 this.preventStructuralChanges = preventStructuralChanges;
54                 this.preventStructuralRootChanges = preventStructuralRootChanges;
55     }
56
57     @Override
58         public Collection<StructuralResource> get(ReadGraph g, StructuralResource element) throws MappingException {
59                 try {
60                     LOGGER.info("        RelatedObjectsAccessor.get");
61                         
62                    
63                     Resource res = getServiceResource(g, element);
64                     if (res == null)
65                         return Collections.emptyList();
66                     Collection<Resource> coll = g.getObjects(res, relation);
67                         List<StructuralResource> result = new ArrayList<StructuralResource>(coll.size());
68                         for (Resource r : coll) {
69                                 if (StructuralUtils.isStructuralInstance(g, r)) {
70                                         result.add(new StructuralResource(g, r, element.getContext(),r));
71                                 } else {
72                                         result.add(new StructuralResource(g, r, element.getContext()));
73                                 }
74                         }
75                         return result;
76                 } catch (DatabaseException e) {
77                         throw new MappingException(e);
78                 }
79         }
80     
81     private boolean preventChange(StructuralResource element) {
82         return preventStructuralChanges && element.isStructural() && (!element.isStructuralRoot()||preventStructuralRootChanges);       
83     }
84         
85         @Override
86         public boolean set(WriteGraph g, StructuralResource element, Collection<StructuralResource> value)
87                         throws MappingException {
88                 try {
89                     LOGGER.info("        RelatedObjectsAccessor.set");
90                     Resource res = getServiceResource(g, element);
91                     if (res == null)
92                         return false;
93                     if (preventChange(element))
94                                 return false;
95                     Resource[] arr = new Resource[value.size()];
96                     int i = 0; 
97                     for (StructuralResource sr : value) {
98                         arr[i++] = sr.getResource();
99                     }
100                         return MappingUtils.synchronizeStatements(g, res, relation, 
101                                 arr, deleteExtraObjects);
102                 } catch (DatabaseException e) {
103                         throw new MappingException(e);
104                 }
105                 
106         }
107
108         private Resource getServiceResource(ReadGraph g, StructuralResource element) {
109                 if (!useTypeResource)
110                         return element.getResource();
111                 return element.getTypeResource();
112         }
113 }