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