]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.issues.common/src/org/simantics/issues/common/MaxIssueSeverityRecursive.java
7646271e3d91443564d9d51fd47af401d2b9ca68
[simantics/platform.git] / bundles / org.simantics.issues.common / src / org / simantics / issues / common / MaxIssueSeverityRecursive.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.issues.common;
13
14 import java.util.Set;
15 import java.util.concurrent.atomic.AtomicBoolean;
16 import java.util.concurrent.atomic.AtomicInteger;
17
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;
25
26 /**
27  * @author Tuukka Lehtonen
28  */
29 public class MaxIssueSeverityRecursive extends TernaryAsyncRead<Resource, Resource, Set<Resource>, Severity> {
30
31     public MaxIssueSeverityRecursive(Resource resource, Resource childRelation, Set<Resource> typesToRecurse) {
32         super(resource, childRelation, typesToRecurse);
33     }
34     
35     @Override
36     public void perform(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure) {
37
38         IssueResource ISSUE = graph.getService(IssueResource.class);
39
40         AtomicInteger issues = new AtomicInteger();
41         AtomicBoolean excepted = new AtomicBoolean(false);
42
43         graph.forEachObject(parameter, ISSUE.Issue_HasContext_Inverse, new AsyncMultiProcedure<Resource>() {
44             @Override
45             public void execute(AsyncReadGraph graph, Resource result) {
46                 issues.incrementAndGet();
47             }
48             @Override
49             public void finished(AsyncReadGraph graph) {
50             
51             }
52             @Override
53             public void exception(AsyncReadGraph graph, Throwable throwable) {
54                 if(excepted.compareAndSet(false, true))
55                     procedure.exception(graph, throwable);
56             }
57         });
58
59         graph.forEachObject(parameter, ISSUE.Issue_ContextList_Element_Inverse, new AsyncMultiProcedure<Resource>() {
60             @Override
61             public void execute(AsyncReadGraph graph, Resource result) {
62                 issues.incrementAndGet();
63             }
64             @Override
65             public void finished(AsyncReadGraph graph) {
66             
67             }
68             @Override
69             public void exception(AsyncReadGraph graph, Throwable throwable) {
70                 if(excepted.compareAndSet(false, true))
71                   procedure.exception(graph, throwable);
72             }
73         });
74         
75         if(excepted.get()) return;
76
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);
81         } else {
82             // Try local issues first
83             graph.asyncRequest(new MaxIssueSeveritySingle(parameter), new AsyncProcedure<Severity>() {
84                 @Override
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);
89                     else
90                         procedure.execute(graph, maxSeverity);
91                 }
92                 @Override
93                 public void exception(AsyncReadGraph graph, Throwable throwable) {
94                     if(excepted.compareAndSet(false, true))
95                       procedure.exception(graph, throwable);
96                 }
97             });
98         }
99     }
100
101 }