]> gerrit.simantics Code Review - simantics/3d.git/blob
654a32581dbc7df20d9e3e365595d1bda0a607c9
[simantics/3d.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.structural.rules.domain;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 import java.util.Collections;\r
17 import java.util.List;\r
18 \r
19 import org.apache.log4j.Logger;\r
20 import org.simantics.db.ReadGraph;\r
21 import org.simantics.db.Resource;\r
22 import org.simantics.db.WriteGraph;\r
23 import org.simantics.db.common.utils.OrderedSetUtils;\r
24 import org.simantics.db.exception.DatabaseException;\r
25 import org.simantics.objmap.exceptions.MappingException;\r
26 import org.simantics.objmap.graph.rules.domain.IDomainAccessor;\r
27 import org.simantics.objmap.structural.StructuralResource;\r
28 \r
29 public class RelatedOrderedSetElementsAccessor implements IDomainAccessor<StructuralResource, Collection<StructuralResource>> {\r
30 \r
31     static Logger LOGGER = Logger.getLogger("org.simantics.objmap");\r
32     \r
33         boolean deleteExtraObjects;\r
34         boolean useTypeResource;\r
35 \r
36         public RelatedOrderedSetElementsAccessor(boolean deleteExtraObjects, boolean useTypeResource) {\r
37         super();\r
38         this.deleteExtraObjects = deleteExtraObjects;\r
39         this.useTypeResource = useTypeResource;\r
40     }\r
41 \r
42     @Override\r
43         public Collection<StructuralResource> get(ReadGraph g, StructuralResource element) throws MappingException {\r
44                 try {\r
45                     LOGGER.info("        RelatedOrderedSetElementsAccessor.get");\r
46                     Resource res = getServiceResource(g, element);\r
47                     if (res == null)\r
48                         return Collections.EMPTY_LIST;\r
49                         List<Resource> list =  OrderedSetUtils.toList(g, res);\r
50                         List<StructuralResource> result = new ArrayList<StructuralResource>(list.size());\r
51                         for (Resource r : list) {\r
52                                 result.add(new StructuralResource(g,r,element.getContext()));\r
53                         }\r
54                         return result;\r
55                 } catch (DatabaseException e) {\r
56                         throw new MappingException(e);\r
57                 }\r
58         }\r
59         \r
60         @Override\r
61         public boolean set(WriteGraph g, StructuralResource element, Collection<StructuralResource> value)\r
62                         throws MappingException {\r
63                 try {\r
64                     LOGGER.info("        RelatedOrderedSetElementsAccessor.set");\r
65                     Resource res = getServiceResource(g, element);\r
66                     if (res == null)\r
67                         return false;\r
68                     List<Resource> list = new ArrayList<Resource>(value.size());\r
69                     for (StructuralResource r : value) {\r
70                         list.add(r.getResource());\r
71                     }\r
72                     return OrderedSetUtils.set(g, res, list);\r
73                     // FIXME Implement deleteExtraObjects\r
74                 } catch (DatabaseException e) {\r
75                         throw new MappingException(e);\r
76                 }\r
77                 \r
78         }\r
79         \r
80         private Resource getServiceResource(ReadGraph g, StructuralResource element) {\r
81                 if (!useTypeResource)\r
82                         return element.getResource();\r
83                 return element.getTypeResource();\r
84         }\r
85 \r
86 }\r