1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2013 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.structural.rules.domain;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collection;
\r
16 import java.util.Collections;
\r
17 import java.util.List;
\r
19 import org.apache.log4j.Logger;
\r
20 import org.simantics.db.ReadGraph;
\r
21 import org.simantics.db.Resource;
\r
22 import org.simantics.db.WriteGraph;
\r
23 import org.simantics.db.common.utils.OrderedSetUtils;
\r
24 import org.simantics.db.exception.DatabaseException;
\r
25 import org.simantics.objmap.exceptions.MappingException;
\r
26 import org.simantics.objmap.graph.rules.domain.IDomainAccessor;
\r
27 import org.simantics.objmap.structural.StructuralResource;
\r
29 public class RelatedOrderedSetElementsAccessor implements IDomainAccessor<StructuralResource, Collection<StructuralResource>> {
\r
31 static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
\r
33 boolean deleteExtraObjects;
\r
34 boolean useTypeResource;
\r
36 public RelatedOrderedSetElementsAccessor(boolean deleteExtraObjects, boolean useTypeResource) {
\r
38 this.deleteExtraObjects = deleteExtraObjects;
\r
39 this.useTypeResource = useTypeResource;
\r
43 public Collection<StructuralResource> get(ReadGraph g, StructuralResource element) throws MappingException {
\r
45 LOGGER.info(" RelatedOrderedSetElementsAccessor.get");
\r
46 Resource res = getServiceResource(g, element);
\r
48 return Collections.emptyList();
\r
49 List<Resource> list = OrderedSetUtils.toList(g, res);
\r
50 List<StructuralResource> result = new ArrayList<StructuralResource>(list.size());
\r
51 for (Resource r : list) {
\r
52 result.add(new StructuralResource(g,r,element.getContext()));
\r
55 } catch (DatabaseException e) {
\r
56 throw new MappingException(e);
\r
61 public boolean set(WriteGraph g, StructuralResource element, Collection<StructuralResource> value)
\r
62 throws MappingException {
\r
64 LOGGER.info(" RelatedOrderedSetElementsAccessor.set");
\r
65 Resource res = getServiceResource(g, element);
\r
68 List<Resource> list = new ArrayList<Resource>(value.size());
\r
69 for (StructuralResource r : value) {
\r
70 list.add(r.getResource());
\r
72 return OrderedSetUtils.set(g, res, list);
\r
73 // FIXME Implement deleteExtraObjects
\r
74 } catch (DatabaseException e) {
\r
75 throw new MappingException(e);
\r
80 private Resource getServiceResource(ReadGraph g, StructuralResource element) {
\r
81 if (!useTypeResource)
\r
82 return element.getResource();
\r
83 return element.getTypeResource();
\r