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 RelatedObjectsAccessor implements IDomainAccessor<StructuralResource,Collection<StructuralResource>> {
32 static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
35 boolean deleteExtraObjects;
36 boolean useTypeResource;
38 private boolean preventStructuralChanges = true;
39 private boolean preventStructuralRootChanges = true;
41 public RelatedObjectsAccessor(Resource relation, boolean deleteExtraObjects, boolean useTypeResource) {
43 this.relation = relation;
44 this.deleteExtraObjects = deleteExtraObjects;
45 this.useTypeResource = useTypeResource;
48 public RelatedObjectsAccessor(Resource relation, boolean deleteExtraObjects, boolean useTypeResource, boolean preventStructuralChanges, boolean preventStructuralRootChanges) {
50 this.relation = relation;
51 this.deleteExtraObjects = deleteExtraObjects;
52 this.useTypeResource = useTypeResource;
53 this.preventStructuralChanges = preventStructuralChanges;
54 this.preventStructuralRootChanges = preventStructuralRootChanges;
58 public Collection<StructuralResource> get(ReadGraph g, StructuralResource element) throws MappingException {
60 LOGGER.info(" RelatedObjectsAccessor.get");
63 Resource res = getServiceResource(g, element);
65 return Collections.emptyList();
66 Collection<Resource> coll = g.getObjects(res, relation);
67 List<StructuralResource> result = new ArrayList<StructuralResource>(coll.size());
68 for (Resource r : coll) {
69 if (StructuralUtils.isStructuralInstance(g, r)) {
70 result.add(new StructuralResource(g, r, element.getContext(),r));
72 result.add(new StructuralResource(g, r, element.getContext()));
76 } catch (DatabaseException e) {
77 throw new MappingException(e);
81 private boolean preventChange(StructuralResource element) {
82 return preventStructuralChanges && element.isStructural() && (!element.isStructuralRoot()||preventStructuralRootChanges);
86 public boolean set(WriteGraph g, StructuralResource element, Collection<StructuralResource> value)
87 throws MappingException {
89 LOGGER.info(" RelatedObjectsAccessor.set");
90 Resource res = getServiceResource(g, element);
93 if (preventChange(element))
95 Resource[] arr = new Resource[value.size()];
97 for (StructuralResource sr : value) {
98 arr[i++] = sr.getResource();
100 return MappingUtils.synchronizeStatements(g, res, relation,
101 arr, deleteExtraObjects);
102 } catch (DatabaseException e) {
103 throw new MappingException(e);
108 private Resource getServiceResource(ReadGraph g, StructuralResource element) {
109 if (!useTypeResource)
110 return element.getResource();
111 return element.getTypeResource();