1 /*******************************************************************************
2 * Copyright (c) 2007, 2013 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.objmap.structural.rules.domain;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
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;
30 public class StructuralRelatedObjectsAccessor implements IDomainAccessor<StructuralResource,Collection<StructuralResource>> {
32 static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
35 boolean deleteExtraObjects;
37 public StructuralRelatedObjectsAccessor(Resource relation, boolean deleteExtraObjects) {
39 this.relation = relation;
40 this.deleteExtraObjects = deleteExtraObjects;
44 public Collection<StructuralResource> get(ReadGraph g, StructuralResource element) throws MappingException {
46 LOGGER.info(" RelatedObjectsAccessor.get");
48 if (!element.isStructural())
49 return Collections.emptyList();
51 // Structural instance
52 Resource instance = StructuralUtils.getContainingInstance(element);
54 Resource publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);
56 if (publicRelation == null)
57 return Collections.emptyList();
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));
68 result.add(new StructuralResource(g, r, context));
72 } catch (DatabaseException e) {
73 throw new MappingException(e);
78 public boolean set(WriteGraph g, StructuralResource element, Collection<StructuralResource> value)
79 throws MappingException {
81 LOGGER.info(" RelatedObjectsAccessor.set");
83 if (!element.isStructural())
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)
93 return MappingUtils.synchronizeStatements(g, instance, publicRelation, new Resource[0], deleteExtraObjects);
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()];
101 for (StructuralResource sr : value) {
102 arr[i++] = sr.getResource();
104 return MappingUtils.synchronizeStatements(g, instance, publicRelation, arr, deleteExtraObjects);
109 } catch (DatabaseException e) {
110 throw new MappingException(e);