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