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