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.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;
30 public class RelatedOrderedSetElementsAccessor implements IDomainAccessor<StructuralResource, Collection<StructuralResource>> {
32 static Logger LOGGER = LoggerFactory.getLogger(RelatedOrderedSetElementsAccessor.class);
34 boolean deleteExtraObjects;
35 boolean useTypeResource;
37 public RelatedOrderedSetElementsAccessor(boolean deleteExtraObjects, boolean useTypeResource) {
39 this.deleteExtraObjects = deleteExtraObjects;
40 this.useTypeResource = useTypeResource;
44 public Collection<StructuralResource> get(ReadGraph g, StructuralResource element) throws MappingException {
46 LOGGER.info(" RelatedOrderedSetElementsAccessor.get");
47 Resource res = getServiceResource(g, element);
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()));
56 } catch (DatabaseException e) {
57 throw new MappingException(e);
62 public boolean set(WriteGraph g, StructuralResource element, Collection<StructuralResource> value)
63 throws MappingException {
65 LOGGER.info(" RelatedOrderedSetElementsAccessor.set");
66 Resource res = getServiceResource(g, element);
69 List<Resource> list = new ArrayList<Resource>(value.size());
70 for (StructuralResource r : value) {
71 list.add(r.getResource());
73 return OrderedSetUtils.set(g, res, list);
74 // FIXME Implement deleteExtraObjects
75 } catch (DatabaseException e) {
76 throw new MappingException(e);
81 private Resource getServiceResource(ReadGraph g, StructuralResource element) {
83 return element.getResource();
84 return element.getTypeResource();