-/*******************************************************************************\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
+/*******************************************************************************
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.issues.common;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.TernaryAsyncRead;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.procedure.AsyncProcedure;
+import org.simantics.issues.Severity;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class ChildMaxIssueSeverity extends TernaryAsyncRead<Resource, Resource, Set<Resource>, Severity> {
+
+ static class AsyncReadResult<T> {
+ private AtomicReference<T> resultRef;
+ private Throwable throwable;
+ private AtomicInteger counter = new AtomicInteger(1);
+ private AsyncProcedure<T> procedure;
+ AsyncReadResult(AsyncProcedure<T> procedure, AtomicReference<T> resultRef) {
+ this.procedure = procedure;
+ this.resultRef = resultRef;
+ }
+ void except(AsyncReadGraph graph, Throwable throwable) {
+ this.throwable = throwable;
+ dec(graph);
+ }
+ void set(AsyncReadGraph graph, T result) {
+ resultRef.set(result);
+ dec(graph);
+ }
+ void inc() {
+ counter.incrementAndGet();
+ }
+ void dec(AsyncReadGraph graph) {
+ if(counter.decrementAndGet() == 0) {
+ if(throwable != null)
+ procedure.exception(graph, throwable);
+ else
+ procedure.execute(graph, resultRef.get());
+ }
+ }
+
+ }
+
+ public ChildMaxIssueSeverity(Resource resource, Resource childRelation, Set<Resource> typesToRecurse) {
+ super(resource, childRelation, typesToRecurse);
+ }
+
+ @Override
+ public void perform(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure) {
+
+ try {
+ Set<Resource> types = graph.getTypes(parameter);
+ if (!Collections.disjoint(parameter3, types)) {
+ checkChildren(graph, procedure);
+ } else {
+ procedure.execute(graph, null);
+ }
+ } catch (DatabaseException e) {
+ procedure.exception(graph, e);
+ }
+
+ }
+
+ protected void checkChildren(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure) {
+
+ AsyncReadResult<Severity> maxSeverity = new AsyncReadResult<Severity>(procedure, new AtomicReference<Severity>());
+
+ try {
+ Collection<Resource> children = graph.getObjects(parameter, parameter2);
+ for(Resource child : children) {
+ maxSeverity.inc();
+ graph.asyncRequest(new MaxIssueSeverityRecursive(child, parameter2, parameter3), new AsyncProcedure<Severity>() {
+ @Override
+ public void execute(AsyncReadGraph graph, Severity severity) {
+ if (severity != null) {
+ synchronized (maxSeverity) {
+ maxSeverity.set(graph, Severity.moreSevere(maxSeverity.resultRef.get(), severity));
+ }
+ } else {
+ maxSeverity.dec(graph);
+ }
+ }
+ @Override
+ public void exception(AsyncReadGraph graph, Throwable throwable) {
+ maxSeverity.except(graph, throwable);
+ }
+ });
+ }
+ maxSeverity.dec(graph);
+ } catch (DatabaseException e) {
+ maxSeverity.except(graph, e);
+ return;
+ }
+
+ }
+
+}