]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
59b544f0f19ff9ee2b28cc0eec775ac37c2bcf78
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 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 import java.util.Collections;\r
16 \r
17 import org.apache.log4j.Logger;\r
18 import org.simantics.db.ReadGraph;\r
19 import org.simantics.db.Resource;\r
20 import org.simantics.db.WriteGraph;\r
21 import org.simantics.db.common.utils.ListUtils;\r
22 import org.simantics.db.exception.DatabaseException;\r
23 import org.simantics.objmap.MappingException;\r
24 \r
25 public class RelatedListElementsAccessor implements IDomainAccessor<Collection<Resource>> {\r
26 \r
27     static Logger LOGGER = Logger.getLogger("org.simantics.objmap");\r
28     \r
29     Resource relation;\r
30 \r
31         public RelatedListElementsAccessor(Resource relation) {\r
32         super();\r
33         this.relation = relation;\r
34     }\r
35 \r
36     @Override\r
37         public Collection<Resource> get(ReadGraph g, Resource element) throws MappingException {\r
38                 try {\r
39                     LOGGER.info("        RelatedListElementsAccessor.get");\r
40                     Resource listResource = g.getPossibleObject(element, relation);\r
41                     if(listResource != null)\r
42                         return ListUtils.toList(g, listResource);\r
43                     else\r
44                         return Collections.emptyList();\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("        RelatedListElementsAccessor.set");\r
55                 Resource listResource = g.getPossibleObject(element, relation);\r
56                 if(listResource != null)\r
57                     ListUtils.createExisting(g, listResource, value);\r
58                 else {\r
59                     listResource = ListUtils.create(g, value);\r
60                     g.claim(element, relation, listResource);\r
61                 }\r
62                     \r
63                     return true;\r
64                     // FIXME Implement deleteExtraObjects\r
65                 } catch (DatabaseException e) {\r
66                         throw new MappingException(e);\r
67                 }\r
68                 \r
69         }\r
70 \r
71 }\r