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;
28 * @author Tuukka Lehtonen
30 public class TagUtil {
32 private final String virtualGraphId;
33 private final String tagURI;
34 private final boolean tag;
36 public static void execute(Session session, String virtualGraphId, String tagURI, boolean tag, Resource... resources) {
37 execute(session, virtualGraphId, tagURI, tag, Arrays.asList(resources));
40 public static void execute(Session session, String virtualGraphId, String tagURI, boolean tag, List<Resource> resources) {
41 new TagUtil(virtualGraphId, tagURI, tag).execute(session, resources);
44 public TagUtil(String virtualGraphId, String tagURI, boolean tag) {
45 this.virtualGraphId = virtualGraphId;
50 public void execute(Session session, Resource... resources) {
51 execute(session, Arrays.asList(resources));
54 public void execute(Session session, final List<Resource> resources) {
55 VirtualGraph vg = virtualGraphId == null ? null :
56 session.getService(VirtualGraphSupport.class).getWorkspacePersistent(virtualGraphId);
58 session.asyncRequest(new WriteRequest(vg) {
60 public void perform(WriteGraph graph) throws DatabaseException {
61 graph.markUndoPoint();
62 processSelection(graph, resources);
70 public void syncExecute(Session session, Resource... resources) throws DatabaseException {
71 syncExecute(session, Arrays.asList(resources));
74 public void syncExecute(Session session, final List<Resource> resources) throws DatabaseException {
75 VirtualGraph vg = virtualGraphId == null ? null :
76 session.getService(VirtualGraphSupport.class).getWorkspacePersistent(virtualGraphId);
77 session.syncRequest(new WriteRequest(vg) {
79 public void perform(WriteGraph graph) throws DatabaseException {
80 processSelection(graph, resources);
85 protected void processSelection(WriteGraph graph, List<Resource> resources) throws DatabaseException {
90 final Resource tr = graph.getResource(tagURI);
91 for (final Resource r : resources) {
93 // If r is virtual, we perform tagging in the same vg
94 if(virtualGraphId == null) {
95 VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);
96 VirtualGraph vg = vgs.getGraph(graph, r);
98 graph.sync(new WriteRequest(vg) {
101 public void perform(WriteGraph graph) throws DatabaseException {
102 graph.claim(r, tr, r);
103 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
104 graph.addMetadata(cm.add(ObjectUtils.toString("Marking " + NameUtils.getSafeLabel(graph, r) + " to " + NameUtils.getSafeName(graph, tr))));
109 graph.claim(r, tr, r);
110 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
111 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))));
118 graph.deny(r, tr, r);
119 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
120 graph.addMetadata(cm.add(ObjectUtils.toString("Denying " + NameUtils.getSafeLabel(graph, r) + " to " + NameUtils.getSafeName(graph, tr))));
126 protected void handleError(DatabaseException e) {
127 Logger.defaultLogError(e);