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