]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.issues.common/src/org/simantics/issues/common/ChildMaxIssueSeverity.java
Multiple readers in db client
[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.Collection;
15 import java.util.Collections;
16 import java.util.Set;
17 import java.util.concurrent.atomic.AtomicInteger;
18 import java.util.concurrent.atomic.AtomicReference;
19
20 import org.simantics.db.AsyncReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.common.request.TernaryAsyncRead;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.procedure.AsyncProcedure;
25 import org.simantics.issues.Severity;
26
27 /**
28  * @author Tuukka Lehtonen
29  */
30 public class ChildMaxIssueSeverity extends TernaryAsyncRead<Resource, Resource, Set<Resource>, Severity> {
31
32     static class AsyncReadResult<T> {
33         private AtomicReference<T> resultRef;
34         private Throwable throwable;
35         private AtomicInteger counter = new AtomicInteger(1);
36         private AsyncProcedure<T> procedure;
37         AsyncReadResult(AsyncProcedure<T> procedure, AtomicReference<T> resultRef) {
38             this.procedure = procedure;
39             this.resultRef = resultRef;
40         }
41         void except(AsyncReadGraph graph, Throwable throwable) {
42             this.throwable = throwable;
43             dec(graph);
44         }
45         void set(AsyncReadGraph graph, T result) {
46             resultRef.set(result);
47             dec(graph);
48         }
49         void inc() {
50             counter.incrementAndGet();
51         }
52         void dec(AsyncReadGraph graph) {
53             if(counter.decrementAndGet() == 0) {
54                 if(throwable != null)
55                     procedure.exception(graph, throwable);
56                 else
57                     procedure.execute(graph, resultRef.get());
58             }
59         }
60         
61     }
62     
63     public ChildMaxIssueSeverity(Resource resource, Resource childRelation, Set<Resource> typesToRecurse) {
64         super(resource, childRelation, typesToRecurse);
65     }
66
67     @Override
68     public void perform(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure) {
69         
70         try {
71             Set<Resource> types = graph.getTypes(parameter);
72             if (!Collections.disjoint(parameter3, types)) {
73                 checkChildren(graph, procedure);
74             } else {
75                 procedure.execute(graph, null);
76             }
77         } catch (DatabaseException e) {
78             procedure.exception(graph, e);
79         }
80         
81     }
82
83     protected void checkChildren(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure) {
84         
85         AsyncReadResult<Severity> maxSeverity = new AsyncReadResult<Severity>(procedure, new AtomicReference<Severity>());
86         
87         try {
88             Collection<Resource> children = graph.getObjects(parameter, parameter2);
89             for(Resource child : children) {
90                 maxSeverity.inc();
91                 graph.asyncRequest(new MaxIssueSeverityRecursive(child, parameter2, parameter3), new AsyncProcedure<Severity>() {
92                     @Override
93                     public void execute(AsyncReadGraph graph, Severity severity) {
94                         if (severity != null) {
95                             synchronized (maxSeverity) {
96                                 maxSeverity.set(graph, Severity.moreSevere(maxSeverity.resultRef.get(), severity));
97                             }
98                         } else {
99                             maxSeverity.dec(graph);
100                         }
101                     }
102                     @Override
103                     public void exception(AsyncReadGraph graph, Throwable throwable) {
104                         maxSeverity.except(graph, throwable);
105                     }
106                 });
107             }
108             maxSeverity.dec(graph);
109         } catch (DatabaseException e) {
110             maxSeverity.except(graph, e);
111             return;
112         }
113         
114     }
115
116 }