]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/LiteralVariableStringAdapter.java
Merge "Removed extra modules OntologyModule imported to namespace" into release/1...
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / LiteralVariableStringAdapter.java
1 package org.simantics.db.layer0.adapter;
2
3 import org.simantics.common.format.Formatter;
4 import org.simantics.databoard.Bindings;
5 import org.simantics.db.AsyncReadGraph;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.adaption.Adapter;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.layer0.variable.Variable;
11 import org.simantics.db.procedure.AsyncProcedure;
12 import org.simantics.db.request.Read;
13 import org.simantics.layer0.Layer0;
14
15 public class LiteralVariableStringAdapter implements Adapter<String, Variable> {
16
17     @Override
18     public void adapt(AsyncReadGraph g, final Resource source, final Variable variable, final AsyncProcedure<String> procedure) {
19         g.asyncRequest(new Read<String>() {
20
21                         @Override
22                         public String perform(ReadGraph graph) throws DatabaseException {
23                                 
24                                 Resource literal = variable.getPossibleRepresents(graph);
25                                 String label = graph.getPossibleRelatedAdapter(literal, graph.getService(Layer0.class).HasLabel, String.class);
26                                 if(label != null) return label;
27                                 String name = graph.getPossibleRelatedValue(literal, graph.getService(Layer0.class).HasName, Bindings.STRING);
28                                 if(name != null) return name;
29                                 
30                                 Object value = variable.getValue(graph);
31                                 Formatter formatter = graph.getPossibleContextualAdapter(literal, variable, Variable.class, Formatter.class);
32                                 if(formatter != null) {
33                                         return formatter.format(value);
34                                 } else {
35                                         return value == null ? "<no value>" : value.toString();
36                                 }
37                                 
38                         }
39                 
40         }, procedure);
41     }
42     
43 }