1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2013 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.objmap.structural.rules.domain;
\r
14 import java.util.ArrayList;
\r
15 import java.util.List;
\r
17 import org.apache.log4j.Logger;
\r
18 import org.simantics.db.ReadGraph;
\r
19 import org.simantics.db.Resource;
\r
20 import org.simantics.db.WriteGraph;
\r
21 import org.simantics.db.exception.DatabaseException;
\r
22 import org.simantics.objmap.exceptions.MappingException;
\r
23 import org.simantics.objmap.graph.rules.domain.IDomainAccessor;
\r
24 import org.simantics.objmap.structural.StructuralResource;
\r
27 public class StructuralRelatedObjectAccessor implements IDomainAccessor<StructuralResource,StructuralResource> {
\r
29 static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
\r
32 boolean useTypeResource;
\r
34 private boolean preventStructuralChanges = true;
\r
35 private boolean preventStructuralRootChanges = true;
\r
37 public StructuralRelatedObjectAccessor(Resource relation, boolean useTypeResource) {
\r
38 this.relation = relation;
\r
39 this.useTypeResource = useTypeResource;
\r
42 public StructuralRelatedObjectAccessor(Resource relation, boolean useTypeResource, boolean preventStructuralChanges, boolean preventStructuralRootChanges) {
\r
43 this.relation = relation;
\r
44 this.useTypeResource = useTypeResource;
\r
45 this.preventStructuralChanges = preventStructuralChanges;
\r
46 this.preventStructuralRootChanges = preventStructuralRootChanges;
\r
49 private boolean preventChange(StructuralResource element) {
\r
50 return preventStructuralChanges && element.isStructural() && (!element.isStructuralRoot()||preventStructuralRootChanges);
\r
54 public StructuralResource get(ReadGraph g, StructuralResource element) throws MappingException {
\r
57 LOGGER.info(" RelatedObjectAccessor.get");
\r
59 if (!element.isStructural())
\r
61 Resource instance = StructuralUtils.getContainingInstance(element);
\r
63 Resource publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);
\r
65 if (publicRelation == null)
\r
67 Resource r = g.getPossibleObject(instance, publicRelation);
\r
70 List<Resource> context = new ArrayList<Resource>();
\r
71 for (int i = 0; i < element.getContext().size()-1; i++)
\r
72 context.add(element.getContext().get(i));
\r
73 if (StructuralUtils.isStructuralInstance(g, r)) {
\r
74 return new StructuralResource(g, r, context,r);
\r
76 return new StructuralResource(g, r, context);
\r
78 } catch (DatabaseException e) {
\r
79 throw new MappingException(e);
\r
84 public boolean set(WriteGraph g, StructuralResource element, StructuralResource value)
\r
85 throws MappingException {
\r
87 LOGGER.info(" RelatedObjectAccessor.set");
\r
88 Resource instance = StructuralUtils.getContainingInstance(element);
\r
89 Resource publicRelation = null;
\r
90 if (instance == null)
\r
92 publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);
\r
93 if (value == null) {
\r
94 if (publicRelation == null)
\r
96 if (preventChange(element))
\r
98 g.deny(instance, publicRelation);
\r
101 if (publicRelation == null) {
\r
102 if (preventChange(element))
\r
104 publicRelation = StructuralUtils.getOrCreatePublishedRelation(g, element, relation);
\r
105 g.claim(instance, publicRelation, value.getResource());
\r
108 Resource r = g.getPossibleObject(instance, publicRelation);
\r
110 if (preventChange(element))
\r
112 g.claim(instance, publicRelation, value.getResource());
\r
115 if (r.equals(value.getResource()))
\r
117 if (preventChange(element))
\r
119 g.deny(instance, publicRelation);
\r
120 g.claim(instance, publicRelation, value.getResource());
\r
125 } catch (DatabaseException e) {
\r
126 throw new MappingException(e);
\r