1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.common.utils;
\r
14 import java.util.Arrays;
\r
15 import java.util.List;
\r
17 import org.simantics.databoard.util.ObjectUtils;
\r
18 import org.simantics.db.Resource;
\r
19 import org.simantics.db.Session;
\r
20 import org.simantics.db.VirtualGraph;
\r
21 import org.simantics.db.WriteGraph;
\r
22 import org.simantics.db.common.CommentMetadata;
\r
23 import org.simantics.db.common.request.WriteRequest;
\r
24 import org.simantics.db.exception.DatabaseException;
\r
25 import org.simantics.db.service.VirtualGraphSupport;
\r
26 import org.simantics.utils.datastructures.Callback;
\r
29 * @author Tuukka Lehtonen
\r
31 public class TagUtil {
\r
33 private final String virtualGraphId;
\r
34 private final String tagURI;
\r
35 private final boolean tag;
\r
37 public static void execute(Session session, String virtualGraphId, String tagURI, boolean tag, Resource... resources) {
\r
38 execute(session, virtualGraphId, tagURI, tag, Arrays.asList(resources));
\r
41 public static void execute(Session session, String virtualGraphId, String tagURI, boolean tag, List<Resource> resources) {
\r
42 new TagUtil(virtualGraphId, tagURI, tag).execute(session, resources);
\r
45 public TagUtil(String virtualGraphId, String tagURI, boolean tag) {
\r
46 this.virtualGraphId = virtualGraphId;
\r
47 this.tagURI = tagURI;
\r
51 public void execute(Session session, Resource... resources) {
\r
52 execute(session, Arrays.asList(resources));
\r
55 public void execute(Session session, final List<Resource> resources) {
\r
56 VirtualGraph vg = virtualGraphId == null ? null :
\r
57 session.getService(VirtualGraphSupport.class).getWorkspacePersistent(virtualGraphId);
\r
59 session.asyncRequest(new WriteRequest(vg) {
\r
61 public void perform(WriteGraph graph) throws DatabaseException {
\r
62 graph.markUndoPoint();
\r
63 processSelection(graph, resources);
\r
65 }, new Callback<DatabaseException>() {
\r
67 public void run(DatabaseException e) {
\r
74 public void syncExecute(Session session, Resource... resources) throws DatabaseException {
\r
75 syncExecute(session, Arrays.asList(resources));
\r
78 public void syncExecute(Session session, final List<Resource> resources) throws DatabaseException {
\r
79 VirtualGraph vg = virtualGraphId == null ? null :
\r
80 session.getService(VirtualGraphSupport.class).getWorkspacePersistent(virtualGraphId);
\r
81 session.syncRequest(new WriteRequest(vg) {
\r
83 public void perform(WriteGraph graph) throws DatabaseException {
\r
84 processSelection(graph, resources);
\r
89 protected void processSelection(WriteGraph graph, List<Resource> resources) throws DatabaseException {
\r
94 final Resource tr = graph.getResource(tagURI);
\r
95 for (final Resource r : resources) {
\r
97 // If r is virtual, we perform tagging in the same vg
\r
98 if(virtualGraphId == null) {
\r
99 VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);
\r
100 VirtualGraph vg = vgs.getGraph(graph, r);
\r
102 graph.sync(new WriteRequest(vg) {
\r
105 public void perform(WriteGraph graph) throws DatabaseException {
\r
106 graph.claim(r, tr, r);
\r
107 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
\r
108 graph.addMetadata(cm.add(ObjectUtils.toString("Marking " + NameUtils.getSafeLabel(graph, r) + " to " + NameUtils.getSafeName(graph, tr))));
\r
113 graph.claim(r, tr, r);
\r
114 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
\r
115 graph.addMetadata(cm.add(ObjectUtils.toString("Marking " + NameUtils.getSafeLabel(graph, r) + " to " + NameUtils.getSafeName(graph, tr)))); }
\r
117 graph.claim(r, tr, r);
\r
118 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
\r
119 graph.addMetadata(cm.add(ObjectUtils.toString("Marking " + NameUtils.getSafeLabel(graph, r) + " to " + NameUtils.getSafeName(graph, tr))));
\r
122 graph.deny(r, tr, r);
\r
123 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
\r
124 graph.addMetadata(cm.add(ObjectUtils.toString("Denying " + NameUtils.getSafeLabel(graph, r) + " to " + NameUtils.getSafeName(graph, tr))));
\r
130 protected void handleError(DatabaseException e) {
\r
131 Logger.defaultLogError(e);
\r