X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.objmap2%2Fsrc%2Forg%2Fsimantics%2Fobjmap%2Fstructural%2Frules%2Fdomain%2FStructuralRelatedObjectsAccessor.java;h=730edc2696cec373b64643d8c8e632634073ec4e;hp=c2bc6f9eebe67f19807390c42a7227519868a59b;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/rules/domain/StructuralRelatedObjectsAccessor.java b/bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/rules/domain/StructuralRelatedObjectsAccessor.java index c2bc6f9ee..730edc269 100644 --- a/bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/rules/domain/StructuralRelatedObjectsAccessor.java +++ b/bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/rules/domain/StructuralRelatedObjectsAccessor.java @@ -1,116 +1,116 @@ -/******************************************************************************* - * 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.exception.DatabaseException; -import org.simantics.objmap.exceptions.MappingException; -import org.simantics.objmap.graph.rules.domain.IDomainAccessor; -import org.simantics.objmap.graph.rules.domain.MappingUtils; -import org.simantics.objmap.structural.StructuralResource; - - -public class StructuralRelatedObjectsAccessor implements IDomainAccessor> { - - static Logger LOGGER = Logger.getLogger("org.simantics.objmap"); - - Resource relation; - boolean deleteExtraObjects; - - public StructuralRelatedObjectsAccessor(Resource relation, boolean deleteExtraObjects) { - super(); - this.relation = relation; - this.deleteExtraObjects = deleteExtraObjects; - } - - @Override - public Collection get(ReadGraph g, StructuralResource element) throws MappingException { - try { - LOGGER.info(" RelatedObjectsAccessor.get"); - - if (!element.isStructural()) - return Collections.emptyList(); - - // Structural instance - Resource instance = StructuralUtils.getContainingInstance(element); - - Resource publicRelation = StructuralUtils.getPublishedRelation(g, element, relation); - - if (publicRelation == null) - return Collections.emptyList(); - - Collection coll = g.getObjects(instance, publicRelation); - List result = new ArrayList(coll.size()); - List context = new ArrayList(); - for (int i = 0; i < element.getContext().size()-1; i++) - context.add(element.getContext().get(i)); - for (Resource r : coll) { - if (StructuralUtils.isStructuralInstance(g, r)) { - result.add(new StructuralResource(g, r, context,r)); - } else { - result.add(new StructuralResource(g, r, context)); - } - } - return result; - } catch (DatabaseException e) { - throw new MappingException(e); - } - } - - @Override - public boolean set(WriteGraph g, StructuralResource element, Collection value) - throws MappingException { - try { - LOGGER.info(" RelatedObjectsAccessor.set"); - - if (!element.isStructural()) - return false; - - Resource instance = StructuralUtils.getContainingInstance(element); - Resource publicRelation = null; - if (value.size() == 0) { - publicRelation = StructuralUtils.getPublishedRelation(g, element, relation); - if (publicRelation == null) - return false; - else { - return MappingUtils.synchronizeStatements(g, instance, publicRelation, new Resource[0], deleteExtraObjects); - } - } else { - publicRelation = StructuralUtils.getOrCreatePublishedRelation(g, element, relation); - if (publicRelation == null) - throw new MappingException("Structural Resource " + element + " cannot contain structural elements, the Resource is not published."); - Resource[] arr = new Resource[value.size()]; - int i = 0; - for (StructuralResource sr : value) { - arr[i++] = sr.getResource(); - } - return MappingUtils.synchronizeStatements(g, instance, publicRelation, arr, deleteExtraObjects); - } - - - - } catch (DatabaseException e) { - throw new MappingException(e); - } - - } - - -} +/******************************************************************************* + * 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.exception.DatabaseException; +import org.simantics.objmap.exceptions.MappingException; +import org.simantics.objmap.graph.rules.domain.IDomainAccessor; +import org.simantics.objmap.graph.rules.domain.MappingUtils; +import org.simantics.objmap.structural.StructuralResource; + + +public class StructuralRelatedObjectsAccessor implements IDomainAccessor> { + + static Logger LOGGER = Logger.getLogger("org.simantics.objmap"); + + Resource relation; + boolean deleteExtraObjects; + + public StructuralRelatedObjectsAccessor(Resource relation, boolean deleteExtraObjects) { + super(); + this.relation = relation; + this.deleteExtraObjects = deleteExtraObjects; + } + + @Override + public Collection get(ReadGraph g, StructuralResource element) throws MappingException { + try { + LOGGER.info(" RelatedObjectsAccessor.get"); + + if (!element.isStructural()) + return Collections.emptyList(); + + // Structural instance + Resource instance = StructuralUtils.getContainingInstance(element); + + Resource publicRelation = StructuralUtils.getPublishedRelation(g, element, relation); + + if (publicRelation == null) + return Collections.emptyList(); + + Collection coll = g.getObjects(instance, publicRelation); + List result = new ArrayList(coll.size()); + List context = new ArrayList(); + for (int i = 0; i < element.getContext().size()-1; i++) + context.add(element.getContext().get(i)); + for (Resource r : coll) { + if (StructuralUtils.isStructuralInstance(g, r)) { + result.add(new StructuralResource(g, r, context,r)); + } else { + result.add(new StructuralResource(g, r, context)); + } + } + return result; + } catch (DatabaseException e) { + throw new MappingException(e); + } + } + + @Override + public boolean set(WriteGraph g, StructuralResource element, Collection value) + throws MappingException { + try { + LOGGER.info(" RelatedObjectsAccessor.set"); + + if (!element.isStructural()) + return false; + + Resource instance = StructuralUtils.getContainingInstance(element); + Resource publicRelation = null; + if (value.size() == 0) { + publicRelation = StructuralUtils.getPublishedRelation(g, element, relation); + if (publicRelation == null) + return false; + else { + return MappingUtils.synchronizeStatements(g, instance, publicRelation, new Resource[0], deleteExtraObjects); + } + } else { + publicRelation = StructuralUtils.getOrCreatePublishedRelation(g, element, relation); + if (publicRelation == null) + throw new MappingException("Structural Resource " + element + " cannot contain structural elements, the Resource is not published."); + Resource[] arr = new Resource[value.size()]; + int i = 0; + for (StructuralResource sr : value) { + arr[i++] = sr.getResource(); + } + return MappingUtils.synchronizeStatements(g, instance, publicRelation, arr, deleteExtraObjects); + } + + + + } catch (DatabaseException e) { + throw new MappingException(e); + } + + } + + +}