X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2Futils%2FTagUtil.java;h=81457b4086e68874536aa63d76691b0d33412188;hb=3e6b970912a5b3a61fc594c5464f1e2582ed0b3c;hp=80acb07bd8b0c490b5d0396530eed45a0d62dda2;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/TagUtil.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/TagUtil.java index 80acb07bd..81457b408 100644 --- a/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/TagUtil.java +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/TagUtil.java @@ -1,134 +1,134 @@ -/******************************************************************************* - * Copyright (c) 2007, 2011 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.db.common.utils; - -import java.util.Arrays; -import java.util.List; - -import org.simantics.databoard.util.ObjectUtils; -import org.simantics.db.Resource; -import org.simantics.db.Session; -import org.simantics.db.VirtualGraph; -import org.simantics.db.WriteGraph; -import org.simantics.db.common.CommentMetadata; -import org.simantics.db.common.request.WriteRequest; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.service.VirtualGraphSupport; -import org.simantics.utils.datastructures.Callback; - -/** - * @author Tuukka Lehtonen - */ -public class TagUtil { - - private final String virtualGraphId; - private final String tagURI; - private final boolean tag; - - public static void execute(Session session, String virtualGraphId, String tagURI, boolean tag, Resource... resources) { - execute(session, virtualGraphId, tagURI, tag, Arrays.asList(resources)); - } - - public static void execute(Session session, String virtualGraphId, String tagURI, boolean tag, List resources) { - new TagUtil(virtualGraphId, tagURI, tag).execute(session, resources); - } - - public TagUtil(String virtualGraphId, String tagURI, boolean tag) { - this.virtualGraphId = virtualGraphId; - this.tagURI = tagURI; - this.tag = tag; - } - - public void execute(Session session, Resource... resources) { - execute(session, Arrays.asList(resources)); - } - - public void execute(Session session, final List resources) { - VirtualGraph vg = virtualGraphId == null ? null : - session.getService(VirtualGraphSupport.class).getWorkspacePersistent(virtualGraphId); - - session.asyncRequest(new WriteRequest(vg) { - @Override - public void perform(WriteGraph graph) throws DatabaseException { - graph.markUndoPoint(); - processSelection(graph, resources); - } - }, new Callback() { - @Override - public void run(DatabaseException e) { - if (e != null) - handleError(e); - } - }); - } - - public void syncExecute(Session session, Resource... resources) throws DatabaseException { - syncExecute(session, Arrays.asList(resources)); - } - - public void syncExecute(Session session, final List resources) throws DatabaseException { - VirtualGraph vg = virtualGraphId == null ? null : - session.getService(VirtualGraphSupport.class).getWorkspacePersistent(virtualGraphId); - session.syncRequest(new WriteRequest(vg) { - @Override - public void perform(WriteGraph graph) throws DatabaseException { - processSelection(graph, resources); - } - }); - } - - protected void processSelection(WriteGraph graph, List resources) throws DatabaseException { - - if (tagURI == null) - return; - - final Resource tr = graph.getResource(tagURI); - for (final Resource r : resources) { - if (tag) { - // If r is virtual, we perform tagging in the same vg - if(virtualGraphId == null) { - VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class); - VirtualGraph vg = vgs.getGraph(graph, r); - if(vg != null) { - graph.sync(new WriteRequest(vg) { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - graph.claim(r, tr, r); - CommentMetadata cm = graph.getMetadata(CommentMetadata.class); - graph.addMetadata(cm.add(ObjectUtils.toString("Marking " + NameUtils.getSafeLabel(graph, r) + " to " + NameUtils.getSafeName(graph, tr)))); - } - - }); - } else { - graph.claim(r, tr, r); - CommentMetadata cm = graph.getMetadata(CommentMetadata.class); - graph.addMetadata(cm.add(ObjectUtils.toString("Marking " + NameUtils.getSafeLabel(graph, r) + " to " + NameUtils.getSafeName(graph, tr)))); } - } else { - graph.claim(r, tr, r); - CommentMetadata cm = graph.getMetadata(CommentMetadata.class); - graph.addMetadata(cm.add(ObjectUtils.toString("Marking " + NameUtils.getSafeLabel(graph, r) + " to " + NameUtils.getSafeName(graph, tr)))); - } - } else { - graph.deny(r, tr, r); - CommentMetadata cm = graph.getMetadata(CommentMetadata.class); - graph.addMetadata(cm.add(ObjectUtils.toString("Denying " + NameUtils.getSafeLabel(graph, r) + " to " + NameUtils.getSafeName(graph, tr)))); - } - } - - } - - protected void handleError(DatabaseException e) { - Logger.defaultLogError(e); - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2011 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.db.common.utils; + +import java.util.Arrays; +import java.util.List; + +import org.simantics.databoard.util.ObjectUtils; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.VirtualGraph; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.CommentMetadata; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.service.VirtualGraphSupport; +import org.simantics.utils.datastructures.Callback; + +/** + * @author Tuukka Lehtonen + */ +public class TagUtil { + + private final String virtualGraphId; + private final String tagURI; + private final boolean tag; + + public static void execute(Session session, String virtualGraphId, String tagURI, boolean tag, Resource... resources) { + execute(session, virtualGraphId, tagURI, tag, Arrays.asList(resources)); + } + + public static void execute(Session session, String virtualGraphId, String tagURI, boolean tag, List resources) { + new TagUtil(virtualGraphId, tagURI, tag).execute(session, resources); + } + + public TagUtil(String virtualGraphId, String tagURI, boolean tag) { + this.virtualGraphId = virtualGraphId; + this.tagURI = tagURI; + this.tag = tag; + } + + public void execute(Session session, Resource... resources) { + execute(session, Arrays.asList(resources)); + } + + public void execute(Session session, final List resources) { + VirtualGraph vg = virtualGraphId == null ? null : + session.getService(VirtualGraphSupport.class).getWorkspacePersistent(virtualGraphId); + + session.asyncRequest(new WriteRequest(vg) { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + graph.markUndoPoint(); + processSelection(graph, resources); + } + }, new Callback() { + @Override + public void run(DatabaseException e) { + if (e != null) + handleError(e); + } + }); + } + + public void syncExecute(Session session, Resource... resources) throws DatabaseException { + syncExecute(session, Arrays.asList(resources)); + } + + public void syncExecute(Session session, final List resources) throws DatabaseException { + VirtualGraph vg = virtualGraphId == null ? null : + session.getService(VirtualGraphSupport.class).getWorkspacePersistent(virtualGraphId); + session.syncRequest(new WriteRequest(vg) { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + processSelection(graph, resources); + } + }); + } + + protected void processSelection(WriteGraph graph, List resources) throws DatabaseException { + + if (tagURI == null) + return; + + final Resource tr = graph.getResource(tagURI); + for (final Resource r : resources) { + if (tag) { + // If r is virtual, we perform tagging in the same vg + if(virtualGraphId == null) { + VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class); + VirtualGraph vg = vgs.getGraph(graph, r); + if(vg != null) { + graph.sync(new WriteRequest(vg) { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + graph.claim(r, tr, r); + CommentMetadata cm = graph.getMetadata(CommentMetadata.class); + graph.addMetadata(cm.add(ObjectUtils.toString("Marking " + NameUtils.getSafeLabel(graph, r) + " to " + NameUtils.getSafeName(graph, tr)))); + } + + }); + } else { + graph.claim(r, tr, r); + CommentMetadata cm = graph.getMetadata(CommentMetadata.class); + graph.addMetadata(cm.add(ObjectUtils.toString("Marking " + NameUtils.getSafeLabel(graph, r) + " to " + NameUtils.getSafeName(graph, tr)))); } + } else { + graph.claim(r, tr, r); + CommentMetadata cm = graph.getMetadata(CommentMetadata.class); + graph.addMetadata(cm.add(ObjectUtils.toString("Marking " + NameUtils.getSafeLabel(graph, r) + " to " + NameUtils.getSafeName(graph, tr)))); + } + } else { + graph.deny(r, tr, r); + CommentMetadata cm = graph.getMetadata(CommentMetadata.class); + graph.addMetadata(cm.add(ObjectUtils.toString("Denying " + NameUtils.getSafeLabel(graph, r) + " to " + NameUtils.getSafeName(graph, tr)))); + } + } + + } + + protected void handleError(DatabaseException e) { + Logger.defaultLogError(e); + } + +}