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.graph.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;
22 * Accesses a resource attached to the element by given functional relation.
23 * @author Hannu Niemistö
25 public class RelatedObjectAccessor implements IDomainAccessor<Resource,Resource> {
27 static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
31 public RelatedObjectAccessor(Resource relation) {
32 this.relation = relation;
36 public Resource get(ReadGraph g, Resource element) throws MappingException {
38 LOGGER.info(" RelatedObjectAccessor.get");
39 return g.getPossibleObject(element, relation);
40 } catch (DatabaseException e) {
41 throw new MappingException(e);
46 public boolean set(WriteGraph g, Resource element, Resource value)
47 throws MappingException {
49 LOGGER.info(" RelatedObjectAccessor.set");
50 Resource resource = g.getPossibleObject(element, relation);
51 if(resource == null) {
54 g.claim(element, relation, value);
57 else if(resource.equals(value))
60 g.deny(element, relation);
62 g.claim(element, relation, value);
65 } catch (DatabaseException e) {
66 throw new MappingException(e);