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;
15 import java.util.concurrent.atomic.AtomicBoolean;
16 import java.util.concurrent.atomic.AtomicInteger;
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;
24 import org.simantics.issues.ontology.IssueResource;
27 * @author Tuukka Lehtonen
29 public class MaxIssueSeverityRecursive extends TernaryAsyncRead<Resource, Resource, Set<Resource>, Severity> {
31 public MaxIssueSeverityRecursive(Resource resource, Resource childRelation, Set<Resource> typesToRecurse) {
32 super(resource, childRelation, typesToRecurse);
36 public void perform(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure) {
38 IssueResource ISSUE = graph.getService(IssueResource.class);
40 AtomicInteger issues = new AtomicInteger();
41 AtomicBoolean excepted = new AtomicBoolean(false);
43 graph.forEachObject(parameter, ISSUE.Issue_HasContext_Inverse, new AsyncMultiProcedure<Resource>() {
45 public void execute(AsyncReadGraph graph, Resource result) {
46 issues.incrementAndGet();
49 public void finished(AsyncReadGraph graph) {
53 public void exception(AsyncReadGraph graph, Throwable throwable) {
54 if(excepted.compareAndSet(false, true))
55 procedure.exception(graph, throwable);
59 graph.forEachObject(parameter, ISSUE.Issue_ContextList_Element_Inverse, new AsyncMultiProcedure<Resource>() {
61 public void execute(AsyncReadGraph graph, Resource result) {
62 issues.incrementAndGet();
65 public void finished(AsyncReadGraph graph) {
69 public void exception(AsyncReadGraph graph, Throwable throwable) {
70 if(excepted.compareAndSet(false, true))
71 procedure.exception(graph, throwable);
75 if(excepted.get()) return;
77 if (issues.get() == 0) {
78 // This content does not have directly attached issues, try to look
79 // for some in the child components.
80 graph.asyncRequest(new ChildMaxIssueSeverity(parameter, parameter2, parameter3), procedure);
82 // Try local issues first
83 graph.asyncRequest(new MaxIssueSeveritySingle(parameter), new AsyncProcedure<Severity>() {
85 public void execute(AsyncReadGraph graph, Severity maxSeverity) {
86 if (maxSeverity == null)
87 // No severity for local issues, try children next.
88 graph.asyncRequest(new ChildMaxIssueSeverity(parameter, parameter2, parameter3), procedure);
90 procedure.execute(graph, maxSeverity);
93 public void exception(AsyncReadGraph graph, Throwable throwable) {
94 if(excepted.compareAndSet(false, true))
95 procedure.exception(graph, throwable);