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.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;
31 public class StructuralRelatedObjectsAccessor implements IDomainAccessor<StructuralResource,Collection<StructuralResource>> {
33 static Logger LOGGER = LoggerFactory.getLogger(StructuralRelatedObjectsAccessor.class);
36 boolean deleteExtraObjects;
38 public StructuralRelatedObjectsAccessor(Resource relation, boolean deleteExtraObjects) {
40 this.relation = relation;
41 this.deleteExtraObjects = deleteExtraObjects;
45 public Collection<StructuralResource> get(ReadGraph g, StructuralResource element) throws MappingException {
47 LOGGER.info(" RelatedObjectsAccessor.get");
49 if (!element.isStructural())
50 return Collections.emptyList();
52 // Structural instance
53 Resource instance = StructuralUtils.getContainingInstance(element);
55 Resource publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);
57 if (publicRelation == null)
58 return Collections.emptyList();
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));
69 result.add(new StructuralResource(g, r, context));
73 } catch (DatabaseException e) {
74 throw new MappingException(e);
79 public boolean set(WriteGraph g, StructuralResource element, Collection<StructuralResource> value)
80 throws MappingException {
82 LOGGER.info(" RelatedObjectsAccessor.set");
84 if (!element.isStructural())
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)
94 return MappingUtils.synchronizeStatements(g, instance, publicRelation, new Resource[0], deleteExtraObjects);
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()];
102 for (StructuralResource sr : value) {
103 arr[i++] = sr.getResource();
105 return MappingUtils.synchronizeStatements(g, instance, publicRelation, arr, deleteExtraObjects);
110 } catch (DatabaseException e) {
111 throw new MappingException(e);