X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.objmap2%2Fsrc%2Forg%2Fsimantics%2Fobjmap%2Fstructural%2Frules%2Fdomain%2FRelatedOrderedSetElementsAccessor.java;h=35f98994d23dd9ccb638fb493939c3b8523bdded;hb=993f8fda339c833e182a540bd4c10d3a91624966;hp=c9b63e4c3e976e66a93aa5fd210de531149fbc10;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/rules/domain/RelatedOrderedSetElementsAccessor.java b/bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/rules/domain/RelatedOrderedSetElementsAccessor.java index c9b63e4c3..35f98994d 100644 --- a/bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/rules/domain/RelatedOrderedSetElementsAccessor.java +++ b/bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/rules/domain/RelatedOrderedSetElementsAccessor.java @@ -1,86 +1,87 @@ -/******************************************************************************* - * Copyright (c) 2007, 2013 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.objmap.structural.rules.domain; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -import org.apache.log4j.Logger; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.common.utils.OrderedSetUtils; -import org.simantics.db.exception.DatabaseException; -import org.simantics.objmap.exceptions.MappingException; -import org.simantics.objmap.graph.rules.domain.IDomainAccessor; -import org.simantics.objmap.structural.StructuralResource; - -public class RelatedOrderedSetElementsAccessor implements IDomainAccessor> { - - static Logger LOGGER = Logger.getLogger("org.simantics.objmap"); - - boolean deleteExtraObjects; - boolean useTypeResource; - - public RelatedOrderedSetElementsAccessor(boolean deleteExtraObjects, boolean useTypeResource) { - super(); - this.deleteExtraObjects = deleteExtraObjects; - this.useTypeResource = useTypeResource; - } - - @Override - public Collection get(ReadGraph g, StructuralResource element) throws MappingException { - try { - LOGGER.info(" RelatedOrderedSetElementsAccessor.get"); - Resource res = getServiceResource(g, element); - if (res == null) - return Collections.emptyList(); - List list = OrderedSetUtils.toList(g, res); - List result = new ArrayList(list.size()); - for (Resource r : list) { - result.add(new StructuralResource(g,r,element.getContext())); - } - return result; - } catch (DatabaseException e) { - throw new MappingException(e); - } - } - - @Override - public boolean set(WriteGraph g, StructuralResource element, Collection value) - throws MappingException { - try { - LOGGER.info(" RelatedOrderedSetElementsAccessor.set"); - Resource res = getServiceResource(g, element); - if (res == null) - return false; - List list = new ArrayList(value.size()); - for (StructuralResource r : value) { - list.add(r.getResource()); - } - return OrderedSetUtils.set(g, res, list); - // FIXME Implement deleteExtraObjects - } catch (DatabaseException e) { - throw new MappingException(e); - } - - } - - private Resource getServiceResource(ReadGraph g, StructuralResource element) { - if (!useTypeResource) - return element.getResource(); - return element.getTypeResource(); - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2013 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.objmap.structural.rules.domain; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.utils.OrderedSetUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.objmap.exceptions.MappingException; +import org.simantics.objmap.graph.rules.domain.IDomainAccessor; +import org.simantics.objmap.structural.StructuralResource; + +public class RelatedOrderedSetElementsAccessor implements IDomainAccessor> { + + static Logger LOGGER = LoggerFactory.getLogger(RelatedOrderedSetElementsAccessor.class); + + boolean deleteExtraObjects; + boolean useTypeResource; + + public RelatedOrderedSetElementsAccessor(boolean deleteExtraObjects, boolean useTypeResource) { + super(); + this.deleteExtraObjects = deleteExtraObjects; + this.useTypeResource = useTypeResource; + } + + @Override + public Collection get(ReadGraph g, StructuralResource element) throws MappingException { + try { + LOGGER.trace(" RelatedOrderedSetElementsAccessor.get"); + Resource res = getServiceResource(g, element); + if (res == null) + return Collections.emptyList(); + List list = OrderedSetUtils.toList(g, res); + List result = new ArrayList(list.size()); + for (Resource r : list) { + result.add(new StructuralResource(g,r,element.getContext())); + } + return result; + } catch (DatabaseException e) { + throw new MappingException(e); + } + } + + @Override + public boolean set(WriteGraph g, StructuralResource element, Collection value) + throws MappingException { + try { + LOGGER.trace(" RelatedOrderedSetElementsAccessor.set"); + Resource res = getServiceResource(g, element); + if (res == null) + return false; + List list = new ArrayList(value.size()); + for (StructuralResource r : value) { + list.add(r.getResource()); + } + return OrderedSetUtils.set(g, res, list); + // FIXME Implement deleteExtraObjects + } catch (DatabaseException e) { + throw new MappingException(e); + } + + } + + private Resource getServiceResource(ReadGraph g, StructuralResource element) { + if (!useTypeResource) + return element.getResource(); + return element.getTypeResource(); + } + +}