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.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.objmap.exceptions.MappingException;
21 import org.simantics.objmap.graph.rules.domain.IDomainAccessor;
22 import org.simantics.objmap.structural.StructuralResource;
25 public class RelatedObjectAccessor implements IDomainAccessor<StructuralResource,StructuralResource> {
27 static Logger LOGGER = LoggerFactory.getLogger(RelatedObjectAccessor.class);
30 boolean useTypeResource;
32 private boolean preventStructuralChanges = true;
33 private boolean preventStructuralRootChanges = true;
35 public RelatedObjectAccessor(Resource relation, boolean useTypeResource) {
36 this.relation = relation;
37 this.useTypeResource = useTypeResource;
40 public RelatedObjectAccessor(Resource relation, boolean useTypeResource, boolean preventStructuralChanges, boolean preventStructuralRootChanges) {
41 this.relation = relation;
42 this.useTypeResource = useTypeResource;
43 this.preventStructuralChanges = preventStructuralChanges;
44 this.preventStructuralRootChanges = preventStructuralRootChanges;
47 private boolean preventChange(StructuralResource element) {
48 return preventStructuralChanges && element.isStructural() && (!element.isStructuralRoot()||preventStructuralRootChanges);
52 public StructuralResource get(ReadGraph g, StructuralResource element) throws MappingException {
54 LOGGER.info(" RelatedObjectAccessor.get");
55 Resource res = getServiceResource(g, element);
58 Resource r = g.getPossibleObject(res, relation);
61 if (StructuralUtils.isStructuralInstance(g, r)) {
62 return new StructuralResource(g, r, element.getContext(),r);
64 return new StructuralResource(g, r, element.getContext());
66 } catch (DatabaseException e) {
67 throw new MappingException(e);
72 public boolean set(WriteGraph g, StructuralResource selement, StructuralResource value)
73 throws MappingException {
75 LOGGER.info(" RelatedObjectAccessor.set");
76 Resource element = getServiceResource(g, selement);
79 Resource resource = g.getPossibleObject(element, relation);
80 if(resource == null) {
83 if (preventChange(selement))
85 g.claim(element, relation, value.getResource());
88 else if(resource.equals(value.getResource()))
91 if (preventChange(selement))
93 g.deny(element, relation);
95 g.claim(element, relation, value.getResource());
98 } catch (DatabaseException e) {
99 throw new MappingException(e);
106 private Resource getServiceResource(ReadGraph g, StructuralResource element) {
108 return element.getResource();
109 return element.getTypeResource();