]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
c7b6cabb3c4ae6ee9d2c2dbaa664a7c59ac14df2
[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 java.util.Collection;\r
15 \r
16 import org.apache.log4j.Logger;\r
17 import org.simantics.db.ReadGraph;\r
18 import org.simantics.db.Resource;\r
19 import org.simantics.db.WriteGraph;\r
20 import org.simantics.db.exception.DatabaseException;\r
21 import org.simantics.objmap.MappingException;\r
22 \r
23 /**\r
24  * Accesses the set of objects attached to the element by the given relation.\r
25  * @author Hannu Niemistö\r
26  */\r
27 public class RelatedObjectsAccessor implements IDomainAccessor<Collection<Resource>> {\r
28 \r
29     static Logger LOGGER = Logger.getLogger("org.simantics.objmap");\r
30     \r
31         Resource relation;\r
32         boolean deleteExtraObjects;\r
33 \r
34         public RelatedObjectsAccessor(Resource relation, boolean deleteExtraObjects) {\r
35         super();\r
36         this.relation = relation;\r
37         this.deleteExtraObjects = deleteExtraObjects;\r
38     }\r
39 \r
40     @Override\r
41         public Collection<Resource> get(ReadGraph g, Resource element) throws MappingException {\r
42                 try {\r
43                     LOGGER.info("        RelatedObjectsAccessor.get");\r
44                         return g.getObjects(element, relation);\r
45                 } catch (DatabaseException e) {\r
46                         throw new MappingException(e);\r
47                 }\r
48         }\r
49         \r
50         @Override\r
51         public boolean set(WriteGraph g, Resource element, Collection<Resource> value)\r
52                         throws MappingException {\r
53                 try {\r
54                     LOGGER.info("        RelatedObjectsAccessor.set");\r
55                         return MappingUtils.synchronizeStatements(g, element, relation, \r
56                                 value.toArray(new Resource[value.size()]), deleteExtraObjects);\r
57                 } catch (DatabaseException e) {\r
58                         throw new MappingException(e);\r
59                 }\r
60                 \r
61         }\r
62 \r
63 }\r