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