X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.objmap2%2Fsrc%2Forg%2Fsimantics%2Fobjmap%2Fstructural%2Frules%2Fdomain%2FRelatedObjectsAccessor.java;fp=bundles%2Forg.simantics.objmap2%2Fsrc%2Forg%2Fsimantics%2Fobjmap%2Fstructural%2Frules%2Fdomain%2FRelatedObjectsAccessor.java;h=c56ffb6d1b3fb72e4b5df9c4583e062b2d124933;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/rules/domain/RelatedObjectsAccessor.java b/bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/rules/domain/RelatedObjectsAccessor.java new file mode 100644 index 000000000..c56ffb6d1 --- /dev/null +++ b/bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/rules/domain/RelatedObjectsAccessor.java @@ -0,0 +1,113 @@ +/******************************************************************************* + * 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 RelatedObjectsAccessor implements IDomainAccessor> { + + static Logger LOGGER = Logger.getLogger("org.simantics.objmap"); + + Resource relation; + boolean deleteExtraObjects; + boolean useTypeResource; + + private boolean preventStructuralChanges = true; + private boolean preventStructuralRootChanges = true; + + public RelatedObjectsAccessor(Resource relation, boolean deleteExtraObjects, boolean useTypeResource) { + super(); + this.relation = relation; + this.deleteExtraObjects = deleteExtraObjects; + this.useTypeResource = useTypeResource; + } + + public RelatedObjectsAccessor(Resource relation, boolean deleteExtraObjects, boolean useTypeResource, boolean preventStructuralChanges, boolean preventStructuralRootChanges) { + super(); + this.relation = relation; + this.deleteExtraObjects = deleteExtraObjects; + this.useTypeResource = useTypeResource; + this.preventStructuralChanges = preventStructuralChanges; + this.preventStructuralRootChanges = preventStructuralRootChanges; + } + + @Override + public Collection get(ReadGraph g, StructuralResource element) throws MappingException { + try { + LOGGER.info(" RelatedObjectsAccessor.get"); + + + Resource res = getServiceResource(g, element); + if (res == null) + return Collections.emptyList(); + Collection coll = g.getObjects(res, relation); + List result = new ArrayList(coll.size()); + for (Resource r : coll) { + if (StructuralUtils.isStructuralInstance(g, r)) { + result.add(new StructuralResource(g, r, element.getContext(),r)); + } else { + result.add(new StructuralResource(g, r, element.getContext())); + } + } + return result; + } catch (DatabaseException e) { + throw new MappingException(e); + } + } + + private boolean preventChange(StructuralResource element) { + return preventStructuralChanges && element.isStructural() && (!element.isStructuralRoot()||preventStructuralRootChanges); + } + + @Override + public boolean set(WriteGraph g, StructuralResource element, Collection value) + throws MappingException { + try { + LOGGER.info(" RelatedObjectsAccessor.set"); + Resource res = getServiceResource(g, element); + if (res == null) + return false; + if (preventChange(element)) + return false; + Resource[] arr = new Resource[value.size()]; + int i = 0; + for (StructuralResource sr : value) { + arr[i++] = sr.getResource(); + } + return MappingUtils.synchronizeStatements(g, res, relation, + arr, deleteExtraObjects); + } catch (DatabaseException e) { + throw new MappingException(e); + } + + } + + private Resource getServiceResource(ReadGraph g, StructuralResource element) { + if (!useTypeResource) + return element.getResource(); + return element.getTypeResource(); + } +}