]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/utils/TagUtil.java
Allow overriding issue hidden-ness/hiding logic in inheriting ontologies
[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
27 /**
28  * @author Tuukka Lehtonen
29  */
30 public class TagUtil {
31
32     private final String  virtualGraphId;
33     private final String  tagURI;
34     private final boolean tag;
35
36     public static void execute(Session session, String virtualGraphId, String tagURI, boolean tag, Resource... resources) {
37         execute(session, virtualGraphId, tagURI, tag, Arrays.asList(resources));
38     }
39
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);
42     }
43
44     public TagUtil(String virtualGraphId, String tagURI, boolean tag) {
45         this.virtualGraphId = virtualGraphId;
46         this.tagURI = tagURI;
47         this.tag = tag;
48     }
49
50     public void execute(Session session, Resource... resources) {
51         execute(session, Arrays.asList(resources));
52     }
53
54     public void execute(Session session, final List<Resource> resources) {
55         VirtualGraph vg = virtualGraphId == null ? null :
56             session.getService(VirtualGraphSupport.class).getWorkspacePersistent(virtualGraphId);
57
58         session.asyncRequest(new WriteRequest(vg) {
59             @Override
60             public void perform(WriteGraph graph) throws DatabaseException {
61                 graph.markUndoPoint();
62                 processSelection(graph, resources);
63             }
64         }, e -> {
65             if (e != null)
66                 handleError(e);
67         });
68     }
69
70     public void syncExecute(Session session, Resource... resources) throws DatabaseException {
71         syncExecute(session, Arrays.asList(resources));
72     }
73
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) {
78             @Override
79             public void perform(WriteGraph graph) throws DatabaseException {
80                 processSelection(graph, resources);
81             }
82         });
83     }
84
85     protected void processSelection(WriteGraph graph, List<Resource> resources) throws DatabaseException {
86
87         if (tagURI == null)
88             return;
89         
90         final Resource tr = graph.getResource(tagURI);
91         for (final Resource r : resources) {
92             if (tag) {
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);
97                         if(vg != null) {
98                                 graph.sync(new WriteRequest(vg) {
99
100                                                         @Override
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))));
105                                                         }
106                                         
107                                 });
108                         } else {
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))));                     }
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                 }
117             } else {
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))));
121             }
122         }
123         
124     }
125
126     protected void handleError(DatabaseException e) {
127         Logger.defaultLogError(e);
128     }
129
130 }