1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.issues.common;
\r
14 import java.util.concurrent.atomic.AtomicReference;
\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
25 * @author Tuukka Lehtonen
\r
27 public class MaxIssueSeveritySingle extends ResourceAsyncRead<Severity>{
\r
29 public MaxIssueSeveritySingle(Resource resource) {
\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
44 // Resource severity = graph.getPossibleObject(issue, ISSUE.HasSeverity);
\r
45 // if (severity != null)
\r
46 // maxSeverity = Severity.moreSevere(maxSeverity, toSeverity(ISSUE, severity));
\r
49 // //System.out.println("severityFor: " + NameUtils.getSafeName(graph, resource) + " : " + maxSeverity);
\r
50 // return maxSeverity;
\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
58 graph.forEachObject(resource, ISSUE.Issue_HasContext_Inverse, new AsyncMultiProcedure<Resource>() {
\r
59 AtomicReference<Severity> maxSeverity = new AtomicReference<Severity>();
\r
61 public void execute(AsyncReadGraph graph, final Resource issue) {
\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
68 acceptIfNotResolved(graph, procedure, ISSUE, issue, maxSeverity);
\r
71 public void finished(AsyncReadGraph graph) {
\r
72 procedure.execute(graph, maxSeverity.get());
\r
75 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
76 procedure.exception(graph, throwable);
\r
82 * Accept an issue for maximum severity comparison, if the issue has not been resolved
\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
89 private void acceptIfNotResolved(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure, final IssueResource ISSUE, final Resource issue, final AtomicReference<Severity> maxSeverity) {
\r
91 graph.forHasStatement(issue, ISSUE.Resolved, new AsyncProcedure<Boolean>() {
\r
93 public void execute(AsyncReadGraph graph, Boolean resolved) {
\r
97 acceptIfSourceIsActive(graph, procedure, ISSUE, issue, maxSeverity);
\r
101 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
102 procedure.exception(graph, throwable);
\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
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
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
121 public void execute(AsyncReadGraph graph, Boolean active) {
\r
122 if (!Boolean.FALSE.equals(active)) {
\r
123 compareSeverity(graph, procedure, ISSUE, issue, maxSeverity);
\r
127 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
128 procedure.exception(graph, throwable);
\r
132 // Not managed by an issue source => user issue
\r
133 compareSeverity(graph, procedure, ISSUE, issue, maxSeverity);
\r
137 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
138 procedure.exception(graph, throwable);
\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
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
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
162 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
163 procedure.exception(graph, throwable);
\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