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