1 /*******************************************************************************
2 * Copyright (c) 2007, 2013 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.objmap.structural.rules.domain;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
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;
29 public class RelatedOrderedSetElementsAccessor implements IDomainAccessor<StructuralResource, Collection<StructuralResource>> {
31 static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
33 boolean deleteExtraObjects;
34 boolean useTypeResource;
36 public RelatedOrderedSetElementsAccessor(boolean deleteExtraObjects, boolean useTypeResource) {
38 this.deleteExtraObjects = deleteExtraObjects;
39 this.useTypeResource = useTypeResource;
43 public Collection<StructuralResource> get(ReadGraph g, StructuralResource element) throws MappingException {
45 LOGGER.info(" RelatedOrderedSetElementsAccessor.get");
46 Resource res = getServiceResource(g, element);
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()));
55 } catch (DatabaseException e) {
56 throw new MappingException(e);
61 public boolean set(WriteGraph g, StructuralResource element, Collection<StructuralResource> value)
62 throws MappingException {
64 LOGGER.info(" RelatedOrderedSetElementsAccessor.set");
65 Resource res = getServiceResource(g, element);
68 List<Resource> list = new ArrayList<Resource>(value.size());
69 for (StructuralResource r : value) {
70 list.add(r.getResource());
72 return OrderedSetUtils.set(g, res, list);
73 // FIXME Implement deleteExtraObjects
74 } catch (DatabaseException e) {
75 throw new MappingException(e);
80 private Resource getServiceResource(ReadGraph g, StructuralResource element) {
82 return element.getResource();
83 return element.getTypeResource();