]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.issues.common/src/org/simantics/issues/common/ChildMaxIssueSeverity.java
1103b5f262af788774072ef3d400ada1800fea51
[simantics/platform.git] / bundles / org.simantics.issues.common / src / org / simantics / issues / common / ChildMaxIssueSeverity.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.Collections;
15 import java.util.Set;
16 import java.util.concurrent.atomic.AtomicReference;
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
25 /**
26  * @author Tuukka Lehtonen
27  */
28 public class ChildMaxIssueSeverity extends TernaryAsyncRead<Resource, Resource, Set<Resource>, Severity>{
29
30     public ChildMaxIssueSeverity(Resource resource, Resource childRelation, Set<Resource> typesToRecurse) {
31         super(resource, childRelation, typesToRecurse);
32     }
33
34 //    @Override
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);
41 //        }
42 //        //System.out.println("severityForChildren: " + NameUtils.getSafeName(graph, resource) + " : " + maxSeverity);
43 //        return maxSeverity;
44 //    }
45
46     @Override
47     public void perform(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure) {
48         //System.out.println(getClass().getSimpleName() + ": " + parameter);
49         
50         graph.forTypes(parameter, new AsyncProcedure<Set<Resource>>() {
51             @Override
52             public void execute(AsyncReadGraph graph, Set<Resource> result) {
53                 if (!Collections.disjoint(parameter3, result)) {
54                     checkChildren(graph, procedure);
55                 } else {
56                     procedure.execute(graph, null);
57                 }
58             }
59             @Override
60             public void exception(AsyncReadGraph graph, Throwable throwable) {
61                 procedure.exception(graph, throwable);
62             }
63         });
64     }
65
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>();
69             @Override
70             public void execute(AsyncReadGraph graph, Resource child) {
71                 graph.asyncRequest(new MaxIssueSeverityRecursive(child, parameter2, parameter3), new AsyncProcedure<Severity>() {
72                     @Override
73                     public void execute(AsyncReadGraph graph, Severity severity) {
74                         if (severity != null) {
75                             synchronized (maxSeverity) {
76                                 maxSeverity.set(Severity.moreSevere(maxSeverity.get(), severity));
77                             }
78                         }
79                     }
80                     @Override
81                     public void exception(AsyncReadGraph graph, Throwable throwable) {
82                         procedure.exception(graph, throwable);
83                     }
84                 });
85             }
86             @Override
87             public void finished(AsyncReadGraph graph) {
88                 procedure.execute(graph, maxSeverity.get());
89             }
90             @Override
91             public void exception(AsyncReadGraph graph, Throwable throwable) {
92                 procedure.exception(graph, throwable);
93             }
94         });
95     }
96
97 }