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.apache.log4j.Logger;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.objmap.exceptions.MappingException;
23 import org.simantics.objmap.graph.rules.domain.IDomainAccessor;
24 import org.simantics.objmap.structural.StructuralResource;
27 public class StructuralRelatedObjectAccessor implements IDomainAccessor<StructuralResource,StructuralResource> {
29 static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
32 boolean useTypeResource;
34 private boolean preventStructuralChanges = true;
35 private boolean preventStructuralRootChanges = true;
37 public StructuralRelatedObjectAccessor(Resource relation, boolean useTypeResource) {
38 this.relation = relation;
39 this.useTypeResource = useTypeResource;
42 public StructuralRelatedObjectAccessor(Resource relation, boolean useTypeResource, boolean preventStructuralChanges, boolean preventStructuralRootChanges) {
43 this.relation = relation;
44 this.useTypeResource = useTypeResource;
45 this.preventStructuralChanges = preventStructuralChanges;
46 this.preventStructuralRootChanges = preventStructuralRootChanges;
49 private boolean preventChange(StructuralResource element) {
50 return preventStructuralChanges && element.isStructural() && (!element.isStructuralRoot()||preventStructuralRootChanges);
54 public StructuralResource get(ReadGraph g, StructuralResource element) throws MappingException {
57 LOGGER.info(" RelatedObjectAccessor.get");
59 if (!element.isStructural())
61 Resource instance = StructuralUtils.getContainingInstance(element);
63 Resource publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);
65 if (publicRelation == null)
67 Resource r = g.getPossibleObject(instance, publicRelation);
70 List<Resource> context = new ArrayList<Resource>();
71 for (int i = 0; i < element.getContext().size()-1; i++)
72 context.add(element.getContext().get(i));
73 if (StructuralUtils.isStructuralInstance(g, r)) {
74 return new StructuralResource(g, r, context,r);
76 return new StructuralResource(g, r, context);
78 } catch (DatabaseException e) {
79 throw new MappingException(e);
84 public boolean set(WriteGraph g, StructuralResource element, StructuralResource value)
85 throws MappingException {
87 LOGGER.info(" RelatedObjectAccessor.set");
88 Resource instance = StructuralUtils.getContainingInstance(element);
89 Resource publicRelation = null;
92 publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);
94 if (publicRelation == null)
96 if (preventChange(element))
98 g.deny(instance, publicRelation);
101 if (publicRelation == null) {
102 if (preventChange(element))
104 publicRelation = StructuralUtils.getOrCreatePublishedRelation(g, element, relation);
105 g.claim(instance, publicRelation, value.getResource());
108 Resource r = g.getPossibleObject(instance, publicRelation);
110 if (preventChange(element))
112 g.claim(instance, publicRelation, value.getResource());
115 if (r.equals(value.getResource()))
117 if (preventChange(element))
119 g.deny(instance, publicRelation);
120 g.claim(instance, publicRelation, value.getResource());
125 } catch (DatabaseException e) {
126 throw new MappingException(e);