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.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;
25 * @author Tuukka Lehtonen
27 public class MaxIssueSeveritySingle extends ResourceAsyncRead<Severity>{
29 public MaxIssueSeveritySingle(Resource resource) {
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);
44 // Resource severity = graph.getPossibleObject(issue, ISSUE.HasSeverity);
45 // if (severity != null)
46 // maxSeverity = Severity.moreSevere(maxSeverity, toSeverity(ISSUE, severity));
49 // //System.out.println("severityFor: " + NameUtils.getSafeName(graph, resource) + " : " + maxSeverity);
50 // return maxSeverity;
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);
58 graph.forEachObject(resource, ISSUE.Issue_HasContext_Inverse, new AsyncMultiProcedure<Resource>() {
59 AtomicReference<Severity> maxSeverity = new AtomicReference<Severity>();
61 public void execute(AsyncReadGraph graph, final Resource issue) {
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.
68 acceptIfNotResolved(graph, procedure, ISSUE, issue, maxSeverity);
71 public void finished(AsyncReadGraph graph) {
72 procedure.execute(graph, maxSeverity.get());
75 public void exception(AsyncReadGraph graph, Throwable throwable) {
76 procedure.exception(graph, throwable);
82 * Accept an issue for maximum severity comparison, if the issue has not been resolved
84 * @param graph AsyncReadGraph
85 * @param procedure AsyncProcedure<Severity>
86 * @param issue Issue resource
87 * @param maxSeverity Current maximum severity
89 private void acceptIfNotResolved(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure, final IssueResource ISSUE, final Resource issue, final AtomicReference<Severity> maxSeverity) {
91 graph.forHasStatement(issue, ISSUE.Resolved, new AsyncProcedure<Boolean>() {
93 public void execute(AsyncReadGraph graph, Boolean resolved) {
97 acceptIfSourceIsActive(graph, procedure, ISSUE, issue, maxSeverity);
101 public void exception(AsyncReadGraph graph, Throwable throwable) {
102 procedure.exception(graph, throwable);
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
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>() {
117 public void execute(AsyncReadGraph graph, Resource issueSource) {
118 if (issueSource != null) {
119 graph.forPossibleRelatedValue(issueSource, ISSUE.IssueSource_active, new AsyncProcedure<Boolean>() {
121 public void execute(AsyncReadGraph graph, Boolean active) {
122 if (!Boolean.FALSE.equals(active)) {
123 compareSeverity(graph, procedure, ISSUE, issue, maxSeverity);
127 public void exception(AsyncReadGraph graph, Throwable throwable) {
128 procedure.exception(graph, throwable);
132 // Not managed by an issue source => user issue
133 compareSeverity(graph, procedure, ISSUE, issue, maxSeverity);
137 public void exception(AsyncReadGraph graph, Throwable throwable) {
138 procedure.exception(graph, throwable);
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
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>() {
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)));
162 public void exception(AsyncReadGraph graph, Throwable throwable) {
163 procedure.exception(graph, throwable);
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;