]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/domain/RelatedObjectAccessor.java
2cfc98185f5b3cd842255363c8a17e71f5bf3b07
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / rules / domain / RelatedObjectAccessor.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2013 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.objmap.graph.rules.domain;
13
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
22 /**
23  * Accesses a resource attached to the element by given functional relation.
24  * @author Hannu Niemistö
25  */
26 public class RelatedObjectAccessor implements IDomainAccessor<Resource,Resource> {
27
28     static Logger LOGGER = LoggerFactory.getLogger(RelatedObjectAccessor.class);
29     
30         Resource relation;
31         
32         public RelatedObjectAccessor(Resource relation) {
33                 this.relation = relation;
34         }
35
36         @Override
37         public Resource get(ReadGraph g, Resource element) throws MappingException {
38                 try {
39                     LOGGER.info("        RelatedObjectAccessor.get");
40                         return g.getPossibleObject(element, relation);
41                 } catch (DatabaseException e) {
42                         throw new MappingException(e);
43                 }
44         }
45         
46         @Override
47         public boolean set(WriteGraph g, Resource element, Resource value)
48                         throws MappingException {
49                 try {
50                     LOGGER.info("        RelatedObjectAccessor.set");
51                     Resource resource = g.getPossibleObject(element, relation);
52                         if(resource == null) {
53                             if(value == null)
54                                 return false;
55                             g.claim(element, relation, value);
56                             return true;
57                         }
58                         else if(resource.equals(value))
59                             return false;
60                         else {
61                             g.deny(element, relation);
62                             if(value != null)
63                                 g.claim(element, relation, value);
64                             return true;
65                         }
66                 } catch (DatabaseException e) {
67                         throw new MappingException(e);
68                 }
69                 
70         }
71
72 }