--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.issues.common;\r
+\r
+import java.util.concurrent.atomic.AtomicReference;\r
+\r
+import org.simantics.db.AsyncReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ResourceAsyncRead;\r
+import org.simantics.db.procedure.AsyncMultiProcedure;\r
+import org.simantics.db.procedure.AsyncProcedure;\r
+import org.simantics.issues.Severity;\r
+import org.simantics.issues.ontology.IssueResource;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class MaxIssueSeveritySingle extends ResourceAsyncRead<Severity>{\r
+\r
+ public MaxIssueSeveritySingle(Resource resource) {\r
+ super(resource);\r
+ }\r
+\r
+// @Override\r
+// public Severity perform(ReadGraph graph) throws DatabaseException {\r
+// //System.out.println("severityFor: " + NameUtils.getSafeName(graph, resource));\r
+// IssueResource ISSUE = IssueResource.getInstance(graph);\r
+// Severity maxSeverity = null;\r
+// Collection<Resource> issues = graph.getObjects(resource, ISSUE.IsIssueContextOf);\r
+// for (Resource issue : issues) {\r
+// boolean resolved = graph.hasStatement(issue, ISSUE.Resolved);\r
+// if (resolved)\r
+// continue;\r
+//\r
+// Resource severity = graph.getPossibleObject(issue, ISSUE.HasSeverity);\r
+// if (severity != null)\r
+// maxSeverity = Severity.moreSevere(maxSeverity, toSeverity(ISSUE, severity));\r
+// }\r
+//\r
+// //System.out.println("severityFor: " + NameUtils.getSafeName(graph, resource) + " : " + maxSeverity);\r
+// return maxSeverity;\r
+// }\r
+\r
+ @Override\r
+ public void perform(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure) {\r
+ final IssueResource ISSUE = graph.getService(IssueResource.class);\r
+ //System.out.println(getClass().getSimpleName() + ": " + resource);\r
+\r
+ graph.forEachObject(resource, ISSUE.Issue_HasContext_Inverse, new AsyncMultiProcedure<Resource>() {\r
+ AtomicReference<Severity> maxSeverity = new AtomicReference<Severity>();\r
+ @Override\r
+ public void execute(AsyncReadGraph graph, final Resource issue) {\r
+ \r
+ /*\r
+ * Compare severity of each related issue and find the maximum severity.\r
+ * The issues that are not resolved and have active issue source manager\r
+ * are taken into account.\r
+ */\r
+ acceptIfNotResolved(graph, procedure, ISSUE, issue, maxSeverity);\r
+ }\r
+ @Override\r
+ public void finished(AsyncReadGraph graph) {\r
+ procedure.execute(graph, maxSeverity.get());\r
+ }\r
+ @Override\r
+ public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+ procedure.exception(graph, throwable);\r
+ }\r
+ });\r
+ }\r
+ \r
+ /**\r
+ * Accept an issue for maximum severity comparison, if the issue has not been resolved\r
+ * \r
+ * @param graph AsyncReadGraph\r
+ * @param procedure AsyncProcedure<Severity>\r
+ * @param issue Issue resource\r
+ * @param maxSeverity Current maximum severity\r
+ */\r
+ private void acceptIfNotResolved(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure, final IssueResource ISSUE, final Resource issue, final AtomicReference<Severity> maxSeverity) {\r
+\r
+ graph.forHasStatement(issue, ISSUE.Resolved, new AsyncProcedure<Boolean>() {\r
+ @Override\r
+ public void execute(AsyncReadGraph graph, Boolean resolved) {\r
+ if (resolved)\r
+ return;\r
+ \r
+ acceptIfSourceIsActive(graph, procedure, ISSUE, issue, maxSeverity);\r
+\r
+ }\r
+ @Override\r
+ public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+ procedure.exception(graph, throwable);\r
+ }\r
+ });\r
+ }\r
+ \r
+ /**\r
+ * Accept an issue for maximum severity comparison if the issue source is active\r
+ * @param graph AsyncReadGraph\r
+ * @param procedure AsyncProcedure<Severity>\r
+ * @param issue Issue resource\r
+ * @param maxSeverity Current maximum severity\r
+ */\r
+ private void acceptIfSourceIsActive(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure, final IssueResource ISSUE, final Resource issue, final AtomicReference<Severity> maxSeverity) {\r
+ graph.forPossibleObject(issue, ISSUE.IssueSource_Manages_Inverse, new AsyncProcedure<Resource>() {\r
+ @Override\r
+ public void execute(AsyncReadGraph graph, Resource issueSource) {\r
+ if (issueSource != null) {\r
+ graph.forPossibleRelatedValue(issueSource, ISSUE.IssueSource_active, new AsyncProcedure<Boolean>() {\r
+ @Override\r
+ public void execute(AsyncReadGraph graph, Boolean active) {\r
+ if (!Boolean.FALSE.equals(active)) {\r
+ compareSeverity(graph, procedure, ISSUE, issue, maxSeverity);\r
+ }\r
+ }\r
+ @Override\r
+ public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+ procedure.exception(graph, throwable);\r
+ }\r
+ });\r
+ } else {\r
+ // Not managed by an issue source => user issue\r
+ compareSeverity(graph, procedure, ISSUE, issue, maxSeverity);\r
+ }\r
+ }\r
+ @Override\r
+ public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+ procedure.exception(graph, throwable);\r
+ }\r
+ });\r
+ }\r
+ \r
+ /**\r
+ * Compare issue's severity for current maximum severity. Update the maximum severity \r
+ * if issue is more severe.\r
+ * @param graph AsyncReadGraph\r
+ * @param procedure AsyncProcedure<Severity>\r
+ * @param issue Issue resource\r
+ * @param maxSeverity Current maximum severity\r
+ */\r
+ private void compareSeverity(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure, final IssueResource ISSUE, final Resource issue, final AtomicReference<Severity> maxSeverity) {\r
+ graph.forPossibleObject(issue, ISSUE.Issue_HasSeverity, new AsyncProcedure<Resource>() {\r
+ @Override\r
+ public void execute(AsyncReadGraph graph, Resource severity) {\r
+ if (severity != null) {\r
+ synchronized (maxSeverity) {\r
+ maxSeverity.set(Severity.moreSevere(maxSeverity.get(), toSeverity(ISSUE, severity)));\r
+ }\r
+ }\r
+ }\r
+ @Override\r
+ public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+ procedure.exception(graph, throwable);\r
+ }\r
+ });\r
+ }\r
+ \r
+\r
+ private static Severity toSeverity(IssueResource ISSUE, Resource severity) {\r
+ if (ISSUE.Severity_Fatal.equals(severity))\r
+ return Severity.FATAL;\r
+ if (ISSUE.Severity_Error.equals(severity))\r
+ return Severity.ERROR;\r
+ if (ISSUE.Severity_Warning.equals(severity))\r
+ return Severity.WARNING;\r
+ if (ISSUE.Severity_Info.equals(severity))\r
+ return Severity.INFO;\r
+ if (ISSUE.Severity_Note.equals(severity))\r
+ return Severity.NOTE;\r
+ return null;\r
+ }\r
+\r
+}\r