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