1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.issues.common;
14 import java.util.concurrent.atomic.AtomicReference;
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;
29 * @author Tuukka Lehtonen
31 public class MaxIssueSeveritySingle extends ResourceAsyncRead<Severity>{
33 public MaxIssueSeveritySingle(Resource resource) {
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);
48 // Resource severity = graph.getPossibleObject(issue, ISSUE.HasSeverity);
49 // if (severity != null)
50 // maxSeverity = Severity.moreSevere(maxSeverity, toSeverity(ISSUE, severity));
53 // //System.out.println("severityFor: " + NameUtils.getSafeName(graph, resource) + " : " + maxSeverity);
54 // return maxSeverity;
58 public void perform(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure) {
60 final IssueResource ISSUE = graph.getService(IssueResource.class);
62 AtomicReference<Severity> maxSeverity = new AtomicReference<Severity>();
64 graph.forEachObject(resource, ISSUE.Issue_HasContext_Inverse, new AsyncMultiProcedure<Resource>() {
66 public void execute(AsyncReadGraph graph, final Resource issue) {
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.
73 acceptIfNotResolved(graph, procedure, ISSUE, issue, maxSeverity);
76 public void finished(AsyncReadGraph graph) {
79 public void exception(AsyncReadGraph graph, Throwable throwable) {
80 procedure.exception(graph, throwable);
84 graph.forEachObject(resource, ISSUE.Issue_ContextList_Element_Inverse, new AsyncMultiProcedure<Resource>() {
86 public void execute(AsyncReadGraph graph, final Resource element) {
88 graph.asyncRequest(new ResourceRead<Resource>(element) {
90 public Resource perform(ReadGraph graph) throws DatabaseException {
91 Resource list = ListUtils.getListElementList(graph, resource);
92 return graph.getSingleObject(list, ISSUE.Issue_HasContexts_Inverse);
94 }, new AsyncProcedure<Resource>() {
97 public void execute(AsyncReadGraph graph, Resource issue) {
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.
103 acceptIfNotResolved(graph, procedure, ISSUE, issue, maxSeverity);
107 public void exception(AsyncReadGraph graph, Throwable throwable) {
108 procedure.exception(graph, throwable);
115 public void finished(AsyncReadGraph graph) {
118 public void exception(AsyncReadGraph graph, Throwable throwable) {
119 procedure.exception(graph, throwable);
123 procedure.execute(graph, maxSeverity.get());
128 * Accept an issue for maximum severity comparison, if the issue has not been resolved
130 * @param graph AsyncReadGraph
131 * @param procedure AsyncProcedure<Severity>
132 * @param issue Issue resource
133 * @param maxSeverity Current maximum severity
135 private void acceptIfNotResolved(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure, final IssueResource ISSUE, final Resource issue, final AtomicReference<Severity> maxSeverity) {
137 graph.forHasStatement(issue, ISSUE.Resolved, new AsyncProcedure<Boolean>() {
139 public void execute(AsyncReadGraph graph, Boolean resolved) {
143 acceptIfSourceIsActive(graph, procedure, ISSUE, issue, maxSeverity);
147 public void exception(AsyncReadGraph graph, Throwable throwable) {
148 procedure.exception(graph, throwable);
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
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>() {
163 public void execute(AsyncReadGraph graph, Resource issueSource) {
164 if (issueSource != null) {
165 graph.forPossibleRelatedValue(issueSource, ISSUE.IssueSource_active, new AsyncProcedure<Boolean>() {
167 public void execute(AsyncReadGraph graph, Boolean active) {
168 if (!Boolean.FALSE.equals(active)) {
169 compareSeverity(graph, procedure, ISSUE, issue, maxSeverity);
173 public void exception(AsyncReadGraph graph, Throwable throwable) {
174 procedure.exception(graph, throwable);
178 // Not managed by an issue source => user issue
179 compareSeverity(graph, procedure, ISSUE, issue, maxSeverity);
183 public void exception(AsyncReadGraph graph, Throwable throwable) {
184 procedure.exception(graph, throwable);
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
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>() {
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)));
208 public void exception(AsyncReadGraph graph, Throwable throwable) {
209 procedure.exception(graph, throwable);
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;