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.Collections;
\r
15 import java.util.Set;
\r
16 import java.util.concurrent.atomic.AtomicReference;
\r
18 import org.simantics.db.AsyncReadGraph;
\r
19 import org.simantics.db.Resource;
\r
20 import org.simantics.db.common.request.TernaryAsyncRead;
\r
21 import org.simantics.db.procedure.AsyncMultiProcedure;
\r
22 import org.simantics.db.procedure.AsyncProcedure;
\r
23 import org.simantics.issues.Severity;
\r
26 * @author Tuukka Lehtonen
\r
28 public class ChildMaxIssueSeverity extends TernaryAsyncRead<Resource, Resource, Set<Resource>, Severity>{
\r
30 public ChildMaxIssueSeverity(Resource resource, Resource childRelation, Set<Resource> typesToRecurse) {
\r
31 super(resource, childRelation, typesToRecurse);
\r
35 // public Severity perform(ReadGraph graph) throws DatabaseException {
\r
36 // Severity maxSeverity = null;
\r
37 // //System.out.println("severityForChildren: " + NameUtils.getSafeName(graph, resource));
\r
38 // for (Resource child : graph.getObjects(resource, resource2)) {
\r
39 // Severity s = graph.syncRequest(new MaxIssueSeverityRecursive(child));
\r
40 // maxSeverity = Severity.moreSevere(maxSeverity, s);
\r
42 // //System.out.println("severityForChildren: " + NameUtils.getSafeName(graph, resource) + " : " + maxSeverity);
\r
43 // return maxSeverity;
\r
47 public void perform(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure) {
\r
48 //System.out.println(getClass().getSimpleName() + ": " + parameter);
\r
50 graph.forTypes(parameter, new AsyncProcedure<Set<Resource>>() {
\r
52 public void execute(AsyncReadGraph graph, Set<Resource> result) {
\r
53 if (!Collections.disjoint(parameter3, result)) {
\r
54 checkChildren(graph, procedure);
\r
56 procedure.execute(graph, null);
\r
60 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
61 procedure.exception(graph, throwable);
\r
66 protected void checkChildren(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure) {
\r
67 graph.forEachObject(parameter, parameter2, new AsyncMultiProcedure<Resource>() {
\r
68 AtomicReference<Severity> maxSeverity = new AtomicReference<Severity>();
\r
70 public void execute(AsyncReadGraph graph, Resource child) {
\r
71 graph.asyncRequest(new MaxIssueSeverityRecursive(child, parameter2, parameter3), new AsyncProcedure<Severity>() {
\r
73 public void execute(AsyncReadGraph graph, Severity severity) {
\r
74 if (severity != null) {
\r
75 synchronized (maxSeverity) {
\r
76 maxSeverity.set(Severity.moreSevere(maxSeverity.get(), severity));
\r
81 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
82 procedure.exception(graph, throwable);
\r
87 public void finished(AsyncReadGraph graph) {
\r
88 procedure.execute(graph, maxSeverity.get());
\r
91 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
92 procedure.exception(graph, throwable);
\r