1 package org.simantics.db.layer0.adapter;
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;
15 public class LiteralVariableStringAdapter implements Adapter<String, Variable> {
18 public void adapt(AsyncReadGraph g, final Resource source, final Variable variable, final AsyncProcedure<String> procedure) {
19 g.asyncRequest(new Read<String>() {
22 public String perform(ReadGraph graph) throws DatabaseException {
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;
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);
35 return value == null ? "<no value>" : value.toString();