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.List;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.objmap.exceptions.MappingException;
24 import org.simantics.objmap.graph.rules.domain.IDomainAccessor;
25 import org.simantics.objmap.structural.StructuralResource;
28 public class StructuralRelatedObjectAccessor implements IDomainAccessor<StructuralResource,StructuralResource> {
30 static Logger LOGGER = LoggerFactory.getLogger(StructuralRelatedObjectAccessor.class);
33 boolean useTypeResource;
35 private boolean preventStructuralChanges = true;
36 private boolean preventStructuralRootChanges = true;
38 public StructuralRelatedObjectAccessor(Resource relation, boolean useTypeResource) {
39 this.relation = relation;
40 this.useTypeResource = useTypeResource;
43 public StructuralRelatedObjectAccessor(Resource relation, boolean useTypeResource, boolean preventStructuralChanges, boolean preventStructuralRootChanges) {
44 this.relation = relation;
45 this.useTypeResource = useTypeResource;
46 this.preventStructuralChanges = preventStructuralChanges;
47 this.preventStructuralRootChanges = preventStructuralRootChanges;
50 private boolean preventChange(StructuralResource element) {
51 return preventStructuralChanges && element.isStructural() && (!element.isStructuralRoot()||preventStructuralRootChanges);
55 public StructuralResource get(ReadGraph g, StructuralResource element) throws MappingException {
58 LOGGER.info(" RelatedObjectAccessor.get");
60 if (!element.isStructural())
62 Resource instance = StructuralUtils.getContainingInstance(element);
64 Resource publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);
66 if (publicRelation == null)
68 Resource r = g.getPossibleObject(instance, publicRelation);
71 List<Resource> context = new ArrayList<Resource>();
72 for (int i = 0; i < element.getContext().size()-1; i++)
73 context.add(element.getContext().get(i));
74 if (StructuralUtils.isStructuralInstance(g, r)) {
75 return new StructuralResource(g, r, context,r);
77 return new StructuralResource(g, r, context);
79 } catch (DatabaseException e) {
80 throw new MappingException(e);
85 public boolean set(WriteGraph g, StructuralResource element, StructuralResource value)
86 throws MappingException {
88 LOGGER.info(" RelatedObjectAccessor.set");
89 Resource instance = StructuralUtils.getContainingInstance(element);
90 Resource publicRelation = null;
93 publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);
95 if (publicRelation == null)
97 if (preventChange(element))
99 g.deny(instance, publicRelation);
102 if (publicRelation == null) {
103 if (preventChange(element))
105 publicRelation = StructuralUtils.getOrCreatePublishedRelation(g, element, relation);
106 g.claim(instance, publicRelation, value.getResource());
109 Resource r = g.getPossibleObject(instance, publicRelation);
111 if (preventChange(element))
113 g.claim(instance, publicRelation, value.getResource());
116 if (r.equals(value.getResource()))
118 if (preventChange(element))
120 g.deny(instance, publicRelation);
121 g.claim(instance, publicRelation, value.getResource());
126 } catch (DatabaseException e) {
127 throw new MappingException(e);