]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.issues.common/src/org/simantics/issues/common/ChildMaxIssueSeverity.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.issues.common / src / org / simantics / issues / common / ChildMaxIssueSeverity.java
diff --git a/bundles/org.simantics.issues.common/src/org/simantics/issues/common/ChildMaxIssueSeverity.java b/bundles/org.simantics.issues.common/src/org/simantics/issues/common/ChildMaxIssueSeverity.java
new file mode 100644 (file)
index 0000000..760f75d
--- /dev/null
@@ -0,0 +1,97 @@
+/*******************************************************************************\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.Collections;\r
+import java.util.Set;\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.TernaryAsyncRead;\r
+import org.simantics.db.procedure.AsyncMultiProcedure;\r
+import org.simantics.db.procedure.AsyncProcedure;\r
+import org.simantics.issues.Severity;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ChildMaxIssueSeverity extends TernaryAsyncRead<Resource, Resource, Set<Resource>, Severity>{\r
+\r
+    public ChildMaxIssueSeverity(Resource resource, Resource childRelation, Set<Resource> typesToRecurse) {\r
+        super(resource, childRelation, typesToRecurse);\r
+    }\r
+\r
+//    @Override\r
+//    public Severity perform(ReadGraph graph) throws DatabaseException {\r
+//        Severity maxSeverity = null;\r
+//        //System.out.println("severityForChildren: " + NameUtils.getSafeName(graph, resource));\r
+//        for (Resource child : graph.getObjects(resource, resource2)) {\r
+//            Severity s = graph.syncRequest(new MaxIssueSeverityRecursive(child));\r
+//            maxSeverity = Severity.moreSevere(maxSeverity, s);\r
+//        }\r
+//        //System.out.println("severityForChildren: " + NameUtils.getSafeName(graph, resource) + " : " + maxSeverity);\r
+//        return maxSeverity;\r
+//    }\r
+\r
+    @Override\r
+    public void perform(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure) {\r
+        //System.out.println(getClass().getSimpleName() + ": " + parameter);\r
+        \r
+        graph.forTypes(parameter, new AsyncProcedure<Set<Resource>>() {\r
+            @Override\r
+            public void execute(AsyncReadGraph graph, Set<Resource> result) {\r
+                if (!Collections.disjoint(parameter3, result)) {\r
+                    checkChildren(graph, procedure);\r
+                } else {\r
+                    procedure.execute(graph, null);\r
+                }\r
+            }\r
+            @Override\r
+            public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+                procedure.exception(graph, throwable);\r
+            }\r
+        });\r
+    }\r
+\r
+    protected void checkChildren(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure) {\r
+        graph.forEachObject(parameter, parameter2, new AsyncMultiProcedure<Resource>() {\r
+            AtomicReference<Severity> maxSeverity = new AtomicReference<Severity>();\r
+            @Override\r
+            public void execute(AsyncReadGraph graph, Resource child) {\r
+                graph.asyncRequest(new MaxIssueSeverityRecursive(child, parameter2, parameter3), new AsyncProcedure<Severity>() {\r
+                    @Override\r
+                    public void execute(AsyncReadGraph graph, Severity severity) {\r
+                        if (severity != null) {\r
+                            synchronized (maxSeverity) {\r
+                                maxSeverity.set(Severity.moreSevere(maxSeverity.get(), severity));\r
+                            }\r
+                        }\r
+                    }\r
+                    @Override\r
+                    public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+                        procedure.exception(graph, throwable);\r
+                    }\r
+                });\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