]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/domain/RelatedObjectAccessor.java
99253790cf0e0f0611eadcf06c9f297189882892
[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.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
21 /**
22  * Accesses a resource attached to the element by given functional relation.
23  * @author Hannu Niemistö
24  */
25 public class RelatedObjectAccessor implements IDomainAccessor<Resource,Resource> {
26
27     static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
28     
29         Resource relation;
30         
31         public RelatedObjectAccessor(Resource relation) {
32                 this.relation = relation;
33         }
34
35         @Override
36         public Resource get(ReadGraph g, Resource element) throws MappingException {
37                 try {
38                     LOGGER.info("        RelatedObjectAccessor.get");
39                         return g.getPossibleObject(element, relation);
40                 } catch (DatabaseException e) {
41                         throw new MappingException(e);
42                 }
43         }
44         
45         @Override
46         public boolean set(WriteGraph g, Resource element, Resource value)
47                         throws MappingException {
48                 try {
49                     LOGGER.info("        RelatedObjectAccessor.set");
50                     Resource resource = g.getPossibleObject(element, relation);
51                         if(resource == null) {
52                             if(value == null)
53                                 return false;
54                             g.claim(element, relation, value);
55                             return true;
56                         }
57                         else if(resource.equals(value))
58                             return false;
59                         else {
60                             g.deny(element, relation);
61                             if(value != null)
62                                 g.claim(element, relation, value);
63                             return true;
64                         }
65                 } catch (DatabaseException e) {
66                         throw new MappingException(e);
67                 }
68                 
69         }
70
71 }