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