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