]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/contribution/FinalLabelerContributionImpl.java
Multiple reader thread support for db client
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / contribution / FinalLabelerContributionImpl.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in 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.browsing.ui.graph.impl.contribution;
13
14 import java.util.Collections;
15 import java.util.Map;
16
17 import org.simantics.Simantics;
18 import org.simantics.browsing.ui.BuiltinKeys;
19 import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;
20 import org.simantics.browsing.ui.DataSource;
21 import org.simantics.browsing.ui.GraphExplorer.ModificationContext;
22 import org.simantics.browsing.ui.NodeContext;
23 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
24 import org.simantics.browsing.ui.common.labelers.LabelerContent;
25 import org.simantics.browsing.ui.common.labelers.LabelerStub;
26 import org.simantics.browsing.ui.common.node.IModifiableNode;
27 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
28 import org.simantics.db.AsyncReadGraph;
29 import org.simantics.db.ReadGraph;
30 import org.simantics.db.UndoContext;
31 import org.simantics.db.common.utils.Logger;
32 import org.simantics.db.exception.DatabaseException;
33 import org.simantics.db.layer0.exception.InvalidVariableException;
34 import org.simantics.db.layer0.exception.PendingVariableException;
35 import org.simantics.db.procedure.Listener;
36 import org.simantics.db.procedure.Procedure;
37 import org.simantics.db.request.Read;
38 import org.simantics.utils.ui.ErrorLogger;
39
40 public abstract class FinalLabelerContributionImpl extends LabelerStub {
41
42     protected final PrimitiveQueryUpdater       updater;
43     private final ResourceQuery<LabelerContent> labelQuery;
44
45     final protected NodeContext                context;
46     final private BuiltinKeys.LabelerKey    key;
47
48     public Object getIdentity(LabelerKey key) {
49         return key;
50     }
51
52     public FinalLabelerContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelerKey key) {
53
54         this.updater = updater;
55         this.context = context;
56         this.key = key;
57
58         labelQuery = new ResourceQuery<LabelerContent>(getIdentity(key), context) {
59
60             @Override
61             public LabelerContent perform(ReadGraph graph) throws DatabaseException {
62                 try {
63                     int cat = category(graph, context);
64                     Map<String, String> lbls = labels(graph, context);
65                     // Make sure that null is not returned.
66                     if (lbls == null)
67                         throw new NullPointerException("FinalLabelerContributionImpl.labels is not allowed to return null, but " + FinalLabelerContributionImpl.this.getClass() + " just did it");
68                     return new LabelerContent(cat, lbls);
69                 } catch (PendingVariableException e) {
70                     return LabelerContent.NO_CONTENT;
71                 } catch (InvalidVariableException e) {
72                     //ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery failed due to invalid variable.", e);
73                     return LabelerContent.NO_CONTENT;
74                 } catch (DatabaseException e) {
75                     throw e;
76                 } catch (Throwable t) {
77                     ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery produced unexpected exception.", t);
78                     return LabelerContent.NO_CONTENT;
79                 }
80             }
81
82             @Override
83             public String toString() {
84                 return FinalLabelerContributionImpl.this + " with context " + context;
85             }
86
87         };
88
89     }
90
91     protected Procedure<LabelerContent> createProcedure() {
92
93         return new Procedure<LabelerContent>() {
94
95             @Override
96             public void execute(LabelerContent result) {
97                 replaceResult(result);
98             }
99
100             @Override
101             public void exception(Throwable t) {
102                 ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery failed, see exception for details.", t);
103             }
104
105         };
106
107     }
108
109     protected void replaceResult(LabelerContent result) {
110         setContent(result);
111         updater.scheduleReplace(context, key, this);
112     }
113
114     @Override
115     public Map<String, String> getLabels() {
116
117         if (content == LabelerContent.NO_CONTENT) {
118
119             final DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
120             assert(source != null);
121
122             final Procedure<LabelerContent> procedure = createProcedure();
123             
124             source.schedule(graph -> {
125                 if(procedure instanceof Listener<?>)
126                     graph.asyncRequest(labelQuery, (Listener<LabelerContent>)procedure);
127                 else
128                     graph.asyncRequest(labelQuery, procedure);
129             });
130
131         }
132
133         if(content == null) return Collections.emptyMap();
134
135         return content.labels;
136
137     }
138
139     @Override
140     public Modifier getModifier(final ModificationContext modificationContext, final String columnKey) {
141         
142         Object obj = context.getConstant(BuiltinKeys.INPUT);
143         if (obj instanceof IModifiableNode) {
144             return ((IModifiableNode) obj).getModifier(columnKey);
145         }
146         try {
147                         return Simantics.getSession().syncRequest(new Read<Modifier>() {
148
149                                 @Override
150                                 public Modifier perform(ReadGraph graph) throws DatabaseException {
151                                     UndoContext undoContext = null == modificationContext ? null :
152                                         (UndoContext)modificationContext.getAdapter(UndoContext.class);
153                                         return FinalLabelerContributionImpl.this.getModifier(graph, undoContext, context, columnKey);
154                                 }
155                                 
156                         });
157                 } catch (DatabaseException e) {
158                         Logger.defaultLogError(e);
159                         return null;
160                 }
161         
162     }
163
164     // OVERRIDE
165
166     public Modifier getModifier(ReadGraph graph, UndoContext undoContext, NodeContext context, String columnKey) throws DatabaseException {
167         return null;
168     }
169
170     public abstract Map<String, String> labels(ReadGraph graph, NodeContext context) throws DatabaseException;
171
172     public abstract int category(ReadGraph graph, NodeContext context) throws DatabaseException;
173
174 }