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