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 org.apache.log4j.Logger;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.WriteGraph;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.objmap.exceptions.MappingException;
20 import org.simantics.objmap.graph.rules.domain.IDomainAccessor;
21 import org.simantics.objmap.structural.StructuralResource;
24 public class RelatedObjectAccessor implements IDomainAccessor<StructuralResource,StructuralResource> {
26 static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
29 boolean useTypeResource;
31 private boolean preventStructuralChanges = true;
32 private boolean preventStructuralRootChanges = true;
34 public RelatedObjectAccessor(Resource relation, boolean useTypeResource) {
35 this.relation = relation;
36 this.useTypeResource = useTypeResource;
39 public RelatedObjectAccessor(Resource relation, boolean useTypeResource, boolean preventStructuralChanges, boolean preventStructuralRootChanges) {
40 this.relation = relation;
41 this.useTypeResource = useTypeResource;
42 this.preventStructuralChanges = preventStructuralChanges;
43 this.preventStructuralRootChanges = preventStructuralRootChanges;
46 private boolean preventChange(StructuralResource element) {
47 return preventStructuralChanges && element.isStructural() && (!element.isStructuralRoot()||preventStructuralRootChanges);
51 public StructuralResource get(ReadGraph g, StructuralResource element) throws MappingException {
53 LOGGER.info(" RelatedObjectAccessor.get");
54 Resource res = getServiceResource(g, element);
57 Resource r = g.getPossibleObject(res, relation);
60 if (StructuralUtils.isStructuralInstance(g, r)) {
61 return new StructuralResource(g, r, element.getContext(),r);
63 return new StructuralResource(g, r, element.getContext());
65 } catch (DatabaseException e) {
66 throw new MappingException(e);
71 public boolean set(WriteGraph g, StructuralResource selement, StructuralResource value)
72 throws MappingException {
74 LOGGER.info(" RelatedObjectAccessor.set");
75 Resource element = getServiceResource(g, selement);
78 Resource resource = g.getPossibleObject(element, relation);
79 if(resource == null) {
82 if (preventChange(selement))
84 g.claim(element, relation, value.getResource());
87 else if(resource.equals(value.getResource()))
90 if (preventChange(selement))
92 g.deny(element, relation);
94 g.claim(element, relation, value.getResource());
97 } catch (DatabaseException e) {
98 throw new MappingException(e);
105 private Resource getServiceResource(ReadGraph g, StructuralResource element) {
107 return element.getResource();
108 return element.getTypeResource();