]> gerrit.simantics Code Review - simantics/platform.git/blob
6984bdcd02269a556eaef001f210d7be4268db96
[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 StructuralRelatedObjectsAccessor implements IDomainAccessor<StructuralResource,Collection<StructuralResource>> {
32
33     static Logger LOGGER = LoggerFactory.getLogger(StructuralRelatedObjectsAccessor.class);
34     
35         Resource relation;
36         boolean deleteExtraObjects;
37
38         public StructuralRelatedObjectsAccessor(Resource relation, boolean deleteExtraObjects) {
39         super();
40         this.relation = relation;
41         this.deleteExtraObjects = deleteExtraObjects;
42     }
43
44     @Override
45         public Collection<StructuralResource> get(ReadGraph g, StructuralResource element) throws MappingException {
46                 try {
47                     LOGGER.info("        RelatedObjectsAccessor.get");
48                         
49                     if (!element.isStructural())
50                         return Collections.emptyList();
51                     
52                     // Structural instance
53                     Resource instance = StructuralUtils.getContainingInstance(element);
54                     
55                     Resource publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);
56                     
57                     if (publicRelation == null)
58                       return Collections.emptyList();
59                     
60                     Collection<Resource> coll = g.getObjects(instance, publicRelation);
61                         List<StructuralResource> result = new ArrayList<StructuralResource>(coll.size());
62                         List<Resource> context = new ArrayList<Resource>();
63                         for (int i = 0; i < element.getContext().size()-1; i++)
64                                 context.add(element.getContext().get(i));
65                         for (Resource r : coll) {
66                                 if (StructuralUtils.isStructuralInstance(g, r)) {
67                                         result.add(new StructuralResource(g, r, context,r));
68                                 } else {
69                                         result.add(new StructuralResource(g, r, context));
70                                 }
71                         }
72                         return result;
73                 } catch (DatabaseException e) {
74                         throw new MappingException(e);
75                 }
76         }
77         
78         @Override
79         public boolean set(WriteGraph g, StructuralResource element, Collection<StructuralResource> value)
80                         throws MappingException {
81                 try {
82                     LOGGER.info("        RelatedObjectsAccessor.set");
83                     
84                     if (!element.isStructural())
85                         return false;
86                     
87                     Resource instance = StructuralUtils.getContainingInstance(element);
88                     Resource publicRelation = null;
89                     if (value.size() == 0) {
90                         publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);
91                         if (publicRelation == null)
92                                 return false;
93                         else {
94                                 return MappingUtils.synchronizeStatements(g, instance, publicRelation, new Resource[0], deleteExtraObjects);
95                         }
96                     } else {
97                         publicRelation = StructuralUtils.getOrCreatePublishedRelation(g, element, relation);
98                         if (publicRelation == null)
99                                 throw new MappingException("Structural Resource " + element + " cannot contain structural elements, the Resource is not published.");
100                             Resource[] arr = new Resource[value.size()];
101                             int i = 0; 
102                             for (StructuralResource sr : value) {
103                                 arr[i++] = sr.getResource();
104                             }
105                             return MappingUtils.synchronizeStatements(g, instance, publicRelation, arr, deleteExtraObjects);
106                     }
107                     
108                     
109                         
110                 } catch (DatabaseException e) {
111                         throw new MappingException(e);
112                 }
113                 
114         }
115
116         
117 }