--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.common.utils;\r
+\r
+import java.util.Arrays;\r
+import java.util.List;\r
+\r
+import org.simantics.databoard.util.ObjectUtils;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.VirtualGraph;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.CommentMetadata;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.service.VirtualGraphSupport;\r
+import org.simantics.utils.datastructures.Callback;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class TagUtil {\r
+\r
+ private final String virtualGraphId;\r
+ private final String tagURI;\r
+ private final boolean tag;\r
+\r
+ public static void execute(Session session, String virtualGraphId, String tagURI, boolean tag, Resource... resources) {\r
+ execute(session, virtualGraphId, tagURI, tag, Arrays.asList(resources));\r
+ }\r
+\r
+ public static void execute(Session session, String virtualGraphId, String tagURI, boolean tag, List<Resource> resources) {\r
+ new TagUtil(virtualGraphId, tagURI, tag).execute(session, resources);\r
+ }\r
+\r
+ public TagUtil(String virtualGraphId, String tagURI, boolean tag) {\r
+ this.virtualGraphId = virtualGraphId;\r
+ this.tagURI = tagURI;\r
+ this.tag = tag;\r
+ }\r
+\r
+ public void execute(Session session, Resource... resources) {\r
+ execute(session, Arrays.asList(resources));\r
+ }\r
+\r
+ public void execute(Session session, final List<Resource> resources) {\r
+ VirtualGraph vg = virtualGraphId == null ? null :\r
+ session.getService(VirtualGraphSupport.class).getWorkspacePersistent(virtualGraphId);\r
+\r
+ session.asyncRequest(new WriteRequest(vg) {\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ graph.markUndoPoint();\r
+ processSelection(graph, resources);\r
+ }\r
+ }, new Callback<DatabaseException>() {\r
+ @Override\r
+ public void run(DatabaseException e) {\r
+ if (e != null)\r
+ handleError(e);\r
+ }\r
+ });\r
+ }\r
+\r
+ public void syncExecute(Session session, Resource... resources) throws DatabaseException {\r
+ syncExecute(session, Arrays.asList(resources));\r
+ }\r
+\r
+ public void syncExecute(Session session, final List<Resource> resources) throws DatabaseException {\r
+ VirtualGraph vg = virtualGraphId == null ? null :\r
+ session.getService(VirtualGraphSupport.class).getWorkspacePersistent(virtualGraphId);\r
+ session.syncRequest(new WriteRequest(vg) {\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ processSelection(graph, resources);\r
+ }\r
+ });\r
+ }\r
+\r
+ protected void processSelection(WriteGraph graph, List<Resource> resources) throws DatabaseException {\r
+\r
+ if (tagURI == null)\r
+ return;\r
+ \r
+ final Resource tr = graph.getResource(tagURI);\r
+ for (final Resource r : resources) {\r
+ if (tag) {\r
+ // If r is virtual, we perform tagging in the same vg\r
+ if(virtualGraphId == null) {\r
+ VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);\r
+ VirtualGraph vg = vgs.getGraph(graph, r);\r
+ if(vg != null) {\r
+ graph.sync(new WriteRequest(vg) {\r
+\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ graph.claim(r, tr, r);\r
+ CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
+ graph.addMetadata(cm.add(ObjectUtils.toString("Marking " + NameUtils.getSafeLabel(graph, r) + " to " + NameUtils.getSafeName(graph, tr))));\r
+ }\r
+ \r
+ });\r
+ } else {\r
+ graph.claim(r, tr, r);\r
+ CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
+ graph.addMetadata(cm.add(ObjectUtils.toString("Marking " + NameUtils.getSafeLabel(graph, r) + " to " + NameUtils.getSafeName(graph, tr)))); }\r
+ } else {\r
+ graph.claim(r, tr, r);\r
+ CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
+ graph.addMetadata(cm.add(ObjectUtils.toString("Marking " + NameUtils.getSafeLabel(graph, r) + " to " + NameUtils.getSafeName(graph, tr))));\r
+ }\r
+ } else {\r
+ graph.deny(r, tr, r);\r
+ CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
+ graph.addMetadata(cm.add(ObjectUtils.toString("Denying " + NameUtils.getSafeLabel(graph, r) + " to " + NameUtils.getSafeName(graph, tr))));\r
+ }\r
+ }\r
+ \r
+ }\r
+\r
+ protected void handleError(DatabaseException e) {\r
+ Logger.defaultLogError(e);\r
+ }\r
+\r
+}\r