]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/utils/TagUtil.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / utils / TagUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.common.utils;
13
14 import java.util.Arrays;
15 import java.util.List;
16
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;
27
28 /**
29  * @author Tuukka Lehtonen
30  */
31 public class TagUtil {
32
33     private final String  virtualGraphId;
34     private final String  tagURI;
35     private final boolean tag;
36
37     public static void execute(Session session, String virtualGraphId, String tagURI, boolean tag, Resource... resources) {
38         execute(session, virtualGraphId, tagURI, tag, Arrays.asList(resources));
39     }
40
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);
43     }
44
45     public TagUtil(String virtualGraphId, String tagURI, boolean tag) {
46         this.virtualGraphId = virtualGraphId;
47         this.tagURI = tagURI;
48         this.tag = tag;
49     }
50
51     public void execute(Session session, Resource... resources) {
52         execute(session, Arrays.asList(resources));
53     }
54
55     public void execute(Session session, final List<Resource> resources) {
56         VirtualGraph vg = virtualGraphId == null ? null :
57             session.getService(VirtualGraphSupport.class).getWorkspacePersistent(virtualGraphId);
58
59         session.asyncRequest(new WriteRequest(vg) {
60             @Override
61             public void perform(WriteGraph graph) throws DatabaseException {
62                 graph.markUndoPoint();
63                 processSelection(graph, resources);
64             }
65         }, new Callback<DatabaseException>() {
66             @Override
67             public void run(DatabaseException e) {
68                 if (e != null)
69                     handleError(e);
70             }
71         });
72     }
73
74     public void syncExecute(Session session, Resource... resources) throws DatabaseException {
75         syncExecute(session, Arrays.asList(resources));
76     }
77
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) {
82             @Override
83             public void perform(WriteGraph graph) throws DatabaseException {
84                 processSelection(graph, resources);
85             }
86         });
87     }
88
89     protected void processSelection(WriteGraph graph, List<Resource> resources) throws DatabaseException {
90
91         if (tagURI == null)
92             return;
93         
94         final Resource tr = graph.getResource(tagURI);
95         for (final Resource r : resources) {
96             if (tag) {
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);
101                         if(vg != null) {
102                                 graph.sync(new WriteRequest(vg) {
103
104                                                         @Override
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))));
109                                                         }
110                                         
111                                 });
112                         } else {
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))));                     }
116                 } else {
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))));
120                 }
121             } else {
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))));
125             }
126         }
127         
128     }
129
130     protected void handleError(DatabaseException e) {
131         Logger.defaultLogError(e);
132     }
133
134 }