-package org.simantics.modeling.adapters;\r
-\r
-import java.util.Collections;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-\r
-import org.simantics.browsing.ui.model.labels.LabelRule;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.variable.Variable;\r
-import org.simantics.db.layer0.variable.Variables;\r
-import org.simantics.modeling.ModelingResources;\r
-import org.simantics.scl.runtime.SCLContext;\r
-import org.simantics.scl.runtime.function.Function1;\r
-\r
-public class SCLLabelRule implements LabelRule {\r
-\r
- private Resource rule;\r
- \r
- public SCLLabelRule(ReadGraph graph, Resource rule) {\r
- this.rule = rule;\r
- }\r
- \r
- @Override\r
- public boolean isCompatible(Class<?> contentType) {\r
- return contentType.equals(Resource.class) || contentType.equals(Variable.class);\r
- }\r
-\r
- @Override\r
- public Map<String, String> getLabel(ReadGraph graph, Object content) throws DatabaseException {\r
- ModelingResources MOD = ModelingResources.getInstance(graph);\r
- \r
- Variable ruleVariable = Variables.getVariable(graph, rule);\r
-\r
- Function1<Object,List<String>> getLabels = ruleVariable.getPossiblePropertyValue(graph, MOD.SCLLabelRule_getLabels);\r
- if(getLabels == null) return Collections.emptyMap();\r
-\r
- SCLContext sclContext = SCLContext.getCurrent();\r
- Object oldGraph = sclContext.get("graph");\r
- try {\r
- sclContext.put("graph", graph);\r
- List<String> value = getLabels.apply(content);\r
- Map<String,String> result = new HashMap<>();\r
- for(int i=0;i<value.size();i+=2) {\r
- result.put(value.get(i), value.get(i+1));\r
- }\r
- return result;\r
- } catch (Throwable t) {\r
- throw new DatabaseException(t);\r
- } finally {\r
- sclContext.put("graph", oldGraph);\r
- }\r
- }\r
-\r
-}\r
+package org.simantics.modeling.adapters;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.simantics.browsing.ui.model.labels.LabelRule;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.layer0.variable.Variables;
+import org.simantics.modeling.ModelingResources;
+import org.simantics.scl.runtime.SCLContext;
+import org.simantics.scl.runtime.function.Function1;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class SCLLabelRule implements LabelRule {
+ private static final Logger LOGGER = LoggerFactory.getLogger(SCLLabelRule.class);
+
+ private Resource rule;
+
+ public SCLLabelRule(ReadGraph graph, Resource rule) {
+ this.rule = rule;
+ }
+
+ @Override
+ public boolean isCompatible(Class<?> contentType) {
+ return contentType.equals(Resource.class) || contentType.equals(Variable.class);
+ }
+
+ @Override
+ public Map<String, String> getLabel(ReadGraph graph, Object content) throws DatabaseException {
+ ModelingResources MOD = ModelingResources.getInstance(graph);
+
+ Variable ruleVariable = Variables.getVariable(graph, rule);
+
+ Function1<Object,List<String>> getLabels = ruleVariable.getPossiblePropertyValue(graph, MOD.SCLLabelRule_getLabels);
+ if(getLabels == null) {
+ LOGGER.warn("Didn't find value for subject={}, predicate={}.", rule, ModelingResources.URIs.SCLLabelRule_getLabels);
+ return Collections.emptyMap();
+ }
+
+ SCLContext sclContext = SCLContext.getCurrent();
+ Object oldGraph = sclContext.get("graph");
+ try {
+ sclContext.put("graph", graph);
+ List<String> value = getLabels.apply(content);
+ Map<String,String> result = new HashMap<>();
+ for(int i=0;i<value.size();i+=2) {
+ result.put(value.get(i), value.get(i+1));
+ }
+ return result;
+ } catch (Throwable t) {
+ LOGGER.error("Calculating label failed.", t);
+ throw new DatabaseException(t);
+ } finally {
+ sclContext.put("graph", oldGraph);
+ }
+ }
+
+}