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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.objmap.rules.domain;
\r
14 import java.util.Collection;
\r
15 import java.util.Collections;
\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
25 public class RelatedListElementsAccessor implements IDomainAccessor<Collection<Resource>> {
\r
27 static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
\r
31 public RelatedListElementsAccessor(Resource relation) {
\r
33 this.relation = relation;
\r
37 public Collection<Resource> get(ReadGraph g, Resource element) throws MappingException {
\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
44 return Collections.emptyList();
\r
45 } catch (DatabaseException e) {
\r
46 throw new MappingException(e);
\r
51 public boolean set(WriteGraph g, Resource element, Collection<Resource> value)
\r
52 throws MappingException {
\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
59 listResource = ListUtils.create(g, value);
\r
60 g.claim(element, relation, listResource);
\r
64 // FIXME Implement deleteExtraObjects
\r
65 } catch (DatabaseException e) {
\r
66 throw new MappingException(e);
\r