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