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