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.layer0.Layer0;
\r
25 import org.simantics.objmap.exceptions.MappingException;
\r
26 import org.simantics.objmap.graph.rules.domain.IDomainAccessor;
\r
27 import org.simantics.objmap.graph.rules.domain.MappingUtils;
\r
28 import org.simantics.objmap.structural.StructuralResource;
\r
29 import org.simantics.structural.stubs.StructuralResource2;
\r
32 public class StructuralRelatedObjectsAccessor implements IDomainAccessor<StructuralResource,Collection<StructuralResource>> {
\r
34 static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
\r
37 boolean deleteExtraObjects;
\r
39 public StructuralRelatedObjectsAccessor(Resource relation, boolean deleteExtraObjects) {
\r
41 this.relation = relation;
\r
42 this.deleteExtraObjects = deleteExtraObjects;
\r
46 public Collection<StructuralResource> get(ReadGraph g, StructuralResource element) throws MappingException {
\r
48 LOGGER.info(" RelatedObjectsAccessor.get");
\r
50 if (!element.isStructural())
\r
51 return Collections.EMPTY_LIST;
\r
53 // Structural instance
\r
54 Resource instance = StructuralUtils.getContainingInstance(element);
\r
56 Resource publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);
\r
58 if (publicRelation == null)
\r
59 return Collections.EMPTY_LIST;
\r
61 Collection<Resource> coll = g.getObjects(instance, publicRelation);
\r
62 List<StructuralResource> result = new ArrayList<StructuralResource>(coll.size());
\r
63 List<Resource> context = new ArrayList<Resource>();
\r
64 for (int i = 0; i < element.getContext().size()-1; i++)
\r
65 context.add(element.getContext().get(i));
\r
66 for (Resource r : coll) {
\r
67 if (StructuralUtils.isStructuralInstance(g, r)) {
\r
68 result.add(new StructuralResource(g, r, context,r));
\r
70 result.add(new StructuralResource(g, r, context));
\r
74 } catch (DatabaseException e) {
\r
75 throw new MappingException(e);
\r
80 public boolean set(WriteGraph g, StructuralResource element, Collection<StructuralResource> value)
\r
81 throws MappingException {
\r
83 LOGGER.info(" RelatedObjectsAccessor.set");
\r
85 if (!element.isStructural())
\r
88 Resource instance = StructuralUtils.getContainingInstance(element);
\r
89 Resource publicRelation = null;
\r
90 if (value.size() == 0) {
\r
91 publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);
\r
92 if (publicRelation == null)
\r
95 return MappingUtils.synchronizeStatements(g, instance, publicRelation, new Resource[0], deleteExtraObjects);
\r
98 publicRelation = StructuralUtils.getOrCreatePublishedRelation(g, element, relation);
\r
99 if (publicRelation == null)
\r
100 throw new MappingException("Structural Resource " + element + " cannot contain structural elements, the Resource is not published.");
\r
101 Resource[] arr = new Resource[value.size()];
\r
103 for (StructuralResource sr : value) {
\r
104 arr[i++] = sr.getResource();
\r
106 return MappingUtils.synchronizeStatements(g, instance, publicRelation, arr, deleteExtraObjects);
\r
111 } catch (DatabaseException e) {
\r
112 throw new MappingException(e);
\r