]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.issues.common/src/org/simantics/issues/common/ChildMaxIssueSeverity.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.issues.common / src / org / simantics / issues / common / ChildMaxIssueSeverity.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.issues.common;\r
13 \r
14 import java.util.Collections;\r
15 import java.util.Set;\r
16 import java.util.concurrent.atomic.AtomicReference;\r
17 \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
24 \r
25 /**\r
26  * @author Tuukka Lehtonen\r
27  */\r
28 public class ChildMaxIssueSeverity extends TernaryAsyncRead<Resource, Resource, Set<Resource>, Severity>{\r
29 \r
30     public ChildMaxIssueSeverity(Resource resource, Resource childRelation, Set<Resource> typesToRecurse) {\r
31         super(resource, childRelation, typesToRecurse);\r
32     }\r
33 \r
34 //    @Override\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
41 //        }\r
42 //        //System.out.println("severityForChildren: " + NameUtils.getSafeName(graph, resource) + " : " + maxSeverity);\r
43 //        return maxSeverity;\r
44 //    }\r
45 \r
46     @Override\r
47     public void perform(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure) {\r
48         //System.out.println(getClass().getSimpleName() + ": " + parameter);\r
49         \r
50         graph.forTypes(parameter, new AsyncProcedure<Set<Resource>>() {\r
51             @Override\r
52             public void execute(AsyncReadGraph graph, Set<Resource> result) {\r
53                 if (!Collections.disjoint(parameter3, result)) {\r
54                     checkChildren(graph, procedure);\r
55                 } else {\r
56                     procedure.execute(graph, null);\r
57                 }\r
58             }\r
59             @Override\r
60             public void exception(AsyncReadGraph graph, Throwable throwable) {\r
61                 procedure.exception(graph, throwable);\r
62             }\r
63         });\r
64     }\r
65 \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
69             @Override\r
70             public void execute(AsyncReadGraph graph, Resource child) {\r
71                 graph.asyncRequest(new MaxIssueSeverityRecursive(child, parameter2, parameter3), new AsyncProcedure<Severity>() {\r
72                     @Override\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
77                             }\r
78                         }\r
79                     }\r
80                     @Override\r
81                     public void exception(AsyncReadGraph graph, Throwable throwable) {\r
82                         procedure.exception(graph, throwable);\r
83                     }\r
84                 });\r
85             }\r
86             @Override\r
87             public void finished(AsyncReadGraph graph) {\r
88                 procedure.execute(graph, maxSeverity.get());\r
89             }\r
90             @Override\r
91             public void exception(AsyncReadGraph graph, Throwable throwable) {\r
92                 procedure.exception(graph, throwable);\r
93             }\r
94         });\r
95     }\r
96 \r
97 }\r