1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2013 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.objmap.structural.rules.domain;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collection;
\r
16 import java.util.Collections;
\r
17 import java.util.List;
\r
19 import org.apache.log4j.Logger;
\r
20 import org.simantics.db.ReadGraph;
\r
21 import org.simantics.db.Resource;
\r
22 import org.simantics.db.WriteGraph;
\r
23 import org.simantics.db.exception.DatabaseException;
\r
24 import org.simantics.objmap.exceptions.MappingException;
\r
25 import org.simantics.objmap.graph.rules.domain.IDomainAccessor;
\r
26 import org.simantics.objmap.graph.rules.domain.MappingUtils;
\r
27 import org.simantics.objmap.structural.StructuralResource;
\r
30 public class StructuralRelatedObjectsAccessor implements IDomainAccessor<StructuralResource,Collection<StructuralResource>> {
\r
32 static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
\r
35 boolean deleteExtraObjects;
\r
37 public StructuralRelatedObjectsAccessor(Resource relation, boolean deleteExtraObjects) {
\r
39 this.relation = relation;
\r
40 this.deleteExtraObjects = deleteExtraObjects;
\r
44 public Collection<StructuralResource> get(ReadGraph g, StructuralResource element) throws MappingException {
\r
46 LOGGER.info(" RelatedObjectsAccessor.get");
\r
48 if (!element.isStructural())
\r
49 return Collections.emptyList();
\r
51 // Structural instance
\r
52 Resource instance = StructuralUtils.getContainingInstance(element);
\r
54 Resource publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);
\r
56 if (publicRelation == null)
\r
57 return Collections.emptyList();
\r
59 Collection<Resource> coll = g.getObjects(instance, publicRelation);
\r
60 List<StructuralResource> result = new ArrayList<StructuralResource>(coll.size());
\r
61 List<Resource> context = new ArrayList<Resource>();
\r
62 for (int i = 0; i < element.getContext().size()-1; i++)
\r
63 context.add(element.getContext().get(i));
\r
64 for (Resource r : coll) {
\r
65 if (StructuralUtils.isStructuralInstance(g, r)) {
\r
66 result.add(new StructuralResource(g, r, context,r));
\r
68 result.add(new StructuralResource(g, r, context));
\r
72 } catch (DatabaseException e) {
\r
73 throw new MappingException(e);
\r
78 public boolean set(WriteGraph g, StructuralResource element, Collection<StructuralResource> value)
\r
79 throws MappingException {
\r
81 LOGGER.info(" RelatedObjectsAccessor.set");
\r
83 if (!element.isStructural())
\r
86 Resource instance = StructuralUtils.getContainingInstance(element);
\r
87 Resource publicRelation = null;
\r
88 if (value.size() == 0) {
\r
89 publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);
\r
90 if (publicRelation == null)
\r
93 return MappingUtils.synchronizeStatements(g, instance, publicRelation, new Resource[0], deleteExtraObjects);
\r
96 publicRelation = StructuralUtils.getOrCreatePublishedRelation(g, element, relation);
\r
97 if (publicRelation == null)
\r
98 throw new MappingException("Structural Resource " + element + " cannot contain structural elements, the Resource is not published.");
\r
99 Resource[] arr = new Resource[value.size()];
\r
101 for (StructuralResource sr : value) {
\r
102 arr[i++] = sr.getResource();
\r
104 return MappingUtils.synchronizeStatements(g, instance, publicRelation, arr, deleteExtraObjects);
\r
109 } catch (DatabaseException e) {
\r
110 throw new MappingException(e);
\r