]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.issues.common/src/org/simantics/issues/common/MaxIssueSeveritySingle.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.issues.common / src / org / simantics / issues / common / MaxIssueSeveritySingle.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * 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.issues.common;\r
13 \r
14 import java.util.concurrent.atomic.AtomicReference;\r
15 \r
16 import org.simantics.db.AsyncReadGraph;\r
17 import org.simantics.db.Resource;\r
18 import org.simantics.db.common.request.ResourceAsyncRead;\r
19 import org.simantics.db.procedure.AsyncMultiProcedure;\r
20 import org.simantics.db.procedure.AsyncProcedure;\r
21 import org.simantics.issues.Severity;\r
22 import org.simantics.issues.ontology.IssueResource;\r
23 \r
24 /**\r
25  * @author Tuukka Lehtonen\r
26  */\r
27 public class MaxIssueSeveritySingle extends ResourceAsyncRead<Severity>{\r
28 \r
29     public MaxIssueSeveritySingle(Resource resource) {\r
30         super(resource);\r
31     }\r
32 \r
33 //    @Override\r
34 //    public Severity perform(ReadGraph graph) throws DatabaseException {\r
35 //        //System.out.println("severityFor: " + NameUtils.getSafeName(graph, resource));\r
36 //        IssueResource ISSUE = IssueResource.getInstance(graph);\r
37 //        Severity maxSeverity = null;\r
38 //        Collection<Resource> issues = graph.getObjects(resource, ISSUE.IsIssueContextOf);\r
39 //        for (Resource issue : issues) {\r
40 //            boolean resolved = graph.hasStatement(issue, ISSUE.Resolved);\r
41 //            if (resolved)\r
42 //                continue;\r
43 //\r
44 //            Resource severity = graph.getPossibleObject(issue, ISSUE.HasSeverity);\r
45 //            if (severity != null)\r
46 //                maxSeverity = Severity.moreSevere(maxSeverity, toSeverity(ISSUE, severity));\r
47 //        }\r
48 //\r
49 //        //System.out.println("severityFor: " + NameUtils.getSafeName(graph, resource) + " : " + maxSeverity);\r
50 //        return maxSeverity;\r
51 //    }\r
52 \r
53     @Override\r
54     public void perform(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure) {\r
55         final IssueResource ISSUE = graph.getService(IssueResource.class);\r
56         //System.out.println(getClass().getSimpleName() + ": " + resource);\r
57 \r
58         graph.forEachObject(resource, ISSUE.Issue_HasContext_Inverse, new AsyncMultiProcedure<Resource>() {\r
59             AtomicReference<Severity> maxSeverity = new AtomicReference<Severity>();\r
60             @Override\r
61             public void execute(AsyncReadGraph graph, final Resource issue) {\r
62                 \r
63                 /*\r
64                  *  Compare severity of each related issue and find the maximum severity.\r
65                  *  The issues that are not resolved and have active issue source manager\r
66                  *  are taken into account.\r
67                  */\r
68                 acceptIfNotResolved(graph, procedure, ISSUE, issue, maxSeverity);\r
69             }\r
70             @Override\r
71             public void finished(AsyncReadGraph graph) {\r
72                 procedure.execute(graph, maxSeverity.get());\r
73             }\r
74             @Override\r
75             public void exception(AsyncReadGraph graph, Throwable throwable) {\r
76                 procedure.exception(graph, throwable);\r
77             }\r
78         });\r
79     }\r
80     \r
81     /**\r
82      * Accept an issue for maximum severity comparison, if the issue has not been resolved\r
83      * \r
84      * @param graph AsyncReadGraph\r
85      * @param procedure AsyncProcedure<Severity>\r
86      * @param issue Issue resource\r
87      * @param maxSeverity Current maximum severity\r
88      */\r
89     private void acceptIfNotResolved(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure, final IssueResource ISSUE, final Resource issue, final AtomicReference<Severity> maxSeverity) {\r
90 \r
91         graph.forHasStatement(issue, ISSUE.Resolved, new AsyncProcedure<Boolean>() {\r
92             @Override\r
93             public void execute(AsyncReadGraph graph, Boolean resolved) {\r
94                 if (resolved)\r
95                     return;\r
96                 \r
97                 acceptIfSourceIsActive(graph, procedure, ISSUE, issue, maxSeverity);\r
98 \r
99             }\r
100             @Override\r
101             public void exception(AsyncReadGraph graph, Throwable throwable) {\r
102                 procedure.exception(graph, throwable);\r
103             }\r
104         });\r
105     }\r
106     \r
107     /**\r
108      * Accept an issue for maximum severity comparison if the issue source is active\r
109      * @param graph AsyncReadGraph\r
110      * @param procedure AsyncProcedure<Severity>\r
111      * @param issue Issue resource\r
112      * @param maxSeverity Current maximum severity\r
113      */\r
114     private void acceptIfSourceIsActive(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure, final IssueResource ISSUE, final Resource issue, final AtomicReference<Severity> maxSeverity) {\r
115         graph.forPossibleObject(issue, ISSUE.IssueSource_Manages_Inverse, new AsyncProcedure<Resource>() {\r
116             @Override\r
117             public void execute(AsyncReadGraph graph, Resource issueSource) {\r
118                 if (issueSource != null) {\r
119                     graph.forPossibleRelatedValue(issueSource, ISSUE.IssueSource_active, new AsyncProcedure<Boolean>() {\r
120                         @Override\r
121                         public void execute(AsyncReadGraph graph, Boolean active) {\r
122                             if (!Boolean.FALSE.equals(active)) {\r
123                                 compareSeverity(graph, procedure, ISSUE, issue, maxSeverity);\r
124                             }\r
125                         }\r
126                         @Override\r
127                         public void exception(AsyncReadGraph graph, Throwable throwable) {\r
128                             procedure.exception(graph, throwable);\r
129                         }\r
130                     });\r
131                 } else {\r
132                     // Not managed by an issue source => user issue\r
133                     compareSeverity(graph, procedure, ISSUE, issue, maxSeverity);\r
134                 }\r
135             }\r
136             @Override\r
137             public void exception(AsyncReadGraph graph, Throwable throwable) {\r
138                 procedure.exception(graph, throwable);\r
139             }\r
140         });\r
141     }\r
142     \r
143     /**\r
144      * Compare issue's severity for current maximum severity. Update the maximum severity \r
145      * if issue is more severe.\r
146      * @param graph AsyncReadGraph\r
147      * @param procedure AsyncProcedure<Severity>\r
148      * @param issue Issue resource\r
149      * @param maxSeverity Current maximum severity\r
150      */\r
151     private void compareSeverity(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure, final IssueResource ISSUE, final Resource issue, final AtomicReference<Severity> maxSeverity) {\r
152         graph.forPossibleObject(issue, ISSUE.Issue_HasSeverity, new AsyncProcedure<Resource>() {\r
153             @Override\r
154             public void execute(AsyncReadGraph graph, Resource severity) {\r
155                 if (severity != null) {\r
156                     synchronized (maxSeverity) {\r
157                         maxSeverity.set(Severity.moreSevere(maxSeverity.get(), toSeverity(ISSUE, severity)));\r
158                     }\r
159                 }\r
160             }\r
161             @Override\r
162             public void exception(AsyncReadGraph graph, Throwable throwable) {\r
163                 procedure.exception(graph, throwable);\r
164             }\r
165         });\r
166     }\r
167     \r
168 \r
169     private static Severity toSeverity(IssueResource ISSUE, Resource severity) {\r
170         if (ISSUE.Severity_Fatal.equals(severity))\r
171             return Severity.FATAL;\r
172         if (ISSUE.Severity_Error.equals(severity))\r
173             return Severity.ERROR;\r
174         if (ISSUE.Severity_Warning.equals(severity))\r
175             return Severity.WARNING;\r
176         if (ISSUE.Severity_Info.equals(severity))\r
177             return Severity.INFO;\r
178         if (ISSUE.Severity_Note.equals(severity))\r
179             return Severity.NOTE;\r
180         return null;\r
181     }\r
182 \r
183 }\r