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%2FRelatedObjectAccessor.java;fp=bundles%2Forg.simantics.objmap2%2Fsrc%2Forg%2Fsimantics%2Fobjmap%2Fstructural%2Frules%2Fdomain%2FRelatedObjectAccessor.java;h=6fa4af73b2c132a269264fe95799194e80321730;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/rules/domain/RelatedObjectAccessor.java b/bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/rules/domain/RelatedObjectAccessor.java new file mode 100644 index 000000000..6fa4af73b --- /dev/null +++ b/bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/rules/domain/RelatedObjectAccessor.java @@ -0,0 +1,111 @@ +/******************************************************************************* + * 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 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.structural.StructuralResource; + + +public class RelatedObjectAccessor implements IDomainAccessor { + + static Logger LOGGER = Logger.getLogger("org.simantics.objmap"); + + Resource relation; + boolean useTypeResource; + + private boolean preventStructuralChanges = true; + private boolean preventStructuralRootChanges = true; + + public RelatedObjectAccessor(Resource relation, boolean useTypeResource) { + this.relation = relation; + this.useTypeResource = useTypeResource; + } + + public RelatedObjectAccessor(Resource relation, boolean useTypeResource, boolean preventStructuralChanges, boolean preventStructuralRootChanges) { + this.relation = relation; + this.useTypeResource = useTypeResource; + this.preventStructuralChanges = preventStructuralChanges; + this.preventStructuralRootChanges = preventStructuralRootChanges; + } + + private boolean preventChange(StructuralResource element) { + return preventStructuralChanges && element.isStructural() && (!element.isStructuralRoot()||preventStructuralRootChanges); + } + + @Override + public StructuralResource get(ReadGraph g, StructuralResource element) throws MappingException { + try { + LOGGER.info(" RelatedObjectAccessor.get"); + Resource res = getServiceResource(g, element); + if (res == null) + return null; + Resource r = g.getPossibleObject(res, relation); + if (r == null) + return null; + if (StructuralUtils.isStructuralInstance(g, r)) { + return new StructuralResource(g, r, element.getContext(),r); + } else { + return new StructuralResource(g, r, element.getContext()); + } + } catch (DatabaseException e) { + throw new MappingException(e); + } + } + + @Override + public boolean set(WriteGraph g, StructuralResource selement, StructuralResource value) + throws MappingException { + try { + LOGGER.info(" RelatedObjectAccessor.set"); + Resource element = getServiceResource(g, selement); + if (element == null) + return false; + Resource resource = g.getPossibleObject(element, relation); + if(resource == null) { + if(value == null) + return false; + if (preventChange(selement)) + return false; + g.claim(element, relation, value.getResource()); + return true; + } + else if(resource.equals(value)) + return false; + else { + if (preventChange(selement)) + return false; + g.deny(element, relation); + if(value != null) + g.claim(element, relation, value.getResource()); + return true; + } + } catch (DatabaseException e) { + throw new MappingException(e); + } + + } + + + + private Resource getServiceResource(ReadGraph g, StructuralResource element) { + if(!useTypeResource) + return element.getResource(); + return element.getTypeResource(); + } + +}