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 RelatedObjectsAccessor implements IDomainAccessor<StructuralResource,Collection<StructuralResource>> {
33 static Logger LOGGER = LoggerFactory.getLogger(RelatedObjectsAccessor.class);
36 boolean deleteExtraObjects;
37 boolean useTypeResource;
39 private boolean preventStructuralChanges = true;
40 private boolean preventStructuralRootChanges = true;
42 public RelatedObjectsAccessor(Resource relation, boolean deleteExtraObjects, boolean useTypeResource) {
44 this.relation = relation;
45 this.deleteExtraObjects = deleteExtraObjects;
46 this.useTypeResource = useTypeResource;
49 public RelatedObjectsAccessor(Resource relation, boolean deleteExtraObjects, boolean useTypeResource, boolean preventStructuralChanges, boolean preventStructuralRootChanges) {
51 this.relation = relation;
52 this.deleteExtraObjects = deleteExtraObjects;
53 this.useTypeResource = useTypeResource;
54 this.preventStructuralChanges = preventStructuralChanges;
55 this.preventStructuralRootChanges = preventStructuralRootChanges;
59 public Collection<StructuralResource> get(ReadGraph g, StructuralResource element) throws MappingException {
61 LOGGER.info(" RelatedObjectsAccessor.get");
64 Resource res = getServiceResource(g, element);
66 return Collections.emptyList();
67 Collection<Resource> coll = g.getObjects(res, relation);
68 List<StructuralResource> result = new ArrayList<StructuralResource>(coll.size());
69 for (Resource r : coll) {
70 if (StructuralUtils.isStructuralInstance(g, r)) {
71 result.add(new StructuralResource(g, r, element.getContext(),r));
73 result.add(new StructuralResource(g, r, element.getContext()));
77 } catch (DatabaseException e) {
78 throw new MappingException(e);
82 private boolean preventChange(StructuralResource element) {
83 return preventStructuralChanges && element.isStructural() && (!element.isStructuralRoot()||preventStructuralRootChanges);
87 public boolean set(WriteGraph g, StructuralResource element, Collection<StructuralResource> value)
88 throws MappingException {
90 LOGGER.info(" RelatedObjectsAccessor.set");
91 Resource res = getServiceResource(g, element);
94 if (preventChange(element))
96 Resource[] arr = new Resource[value.size()];
98 for (StructuralResource sr : value) {
99 arr[i++] = sr.getResource();
101 return MappingUtils.synchronizeStatements(g, res, relation,
102 arr, deleteExtraObjects);
103 } catch (DatabaseException e) {
104 throw new MappingException(e);
109 private Resource getServiceResource(ReadGraph g, StructuralResource element) {
110 if (!useTypeResource)
111 return element.getResource();
112 return element.getTypeResource();