]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.issues.common/src/org/simantics/issues/common/All.java
6eccb1755f22c746f9f91666e57b097a543467a8
[simantics/platform.git] / bundles / org.simantics.issues.common / src / org / simantics / issues / common / All.java
1 package org.simantics.issues.common;
2
3 import java.util.Collections;
4 import java.util.List;
5
6 import org.simantics.databoard.Bindings;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.request.PossibleIndexRoot;
10 import org.simantics.db.common.utils.ListUtils;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.variable.Variable;
13 import org.simantics.db.layer0.variable.Variables;
14 import org.simantics.issues.ontology.IssueResource;
15 import org.simantics.layer0.Layer0;
16 import org.simantics.scl.reflection.annotations.SCLValue;
17
18 public class All {
19         
20     @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
21     public static String defaultDescription(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
22         return context.getParent(graph).getLabel(graph);
23     }
24     
25     @SCLValue(type = "a -> b -> c")
26     public static Object dependencyBaseRealizationFunction(Object _graph, Object _model) throws DatabaseException {
27 //        ReadGraph graph = (ReadGraph)_graph;
28         Resource model = (Resource)_model;
29         return model;
30 //        SimulationResource SIMU = SimulationResource.getInstance(graph);
31 //        if(graph.isInstanceOf(model, SIMU.Model))
32 //            return graph.getSingleObject(model, SimulationResource.getInstance(graph).HasConfiguration);
33 //        else return null;
34
35     }
36     
37     @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
38     public static String standardIssueSeverity(ReadGraph graph, Resource converter, Variable property) throws DatabaseException {
39         Layer0 L0 = Layer0.getInstance(graph);
40         IssueResource ISSUE = IssueResource.getInstance(graph);
41         Resource issue = property.getParent(graph).getRepresents(graph);
42         Resource severity = graph.getPossibleObject(issue, ISSUE.Issue_HasSeverity);
43         if (severity == null)
44                 return "Undefined";
45         return graph.getPossibleRelatedValue(severity, L0.HasName, Bindings.STRING);
46     }
47
48     @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
49     public static String standardIssueResource(ReadGraph graph, Resource converter, Variable property) throws DatabaseException {
50         Layer0 L0 = Layer0.getInstance(graph);
51         List<Resource> contexts = IssueUtils.getContextsForProperty(graph, property);
52         if(contexts.isEmpty()) return "";
53         Resource context = contexts.get(0);
54         String name = graph.getPossibleRelatedValue(context, L0.HasName, Bindings.STRING);
55         if((name != null && !name.isEmpty())) return name;
56         if(!graph.hasStatement(context)) return "Removed - please run batch validations";
57         return context.toString();
58     }
59
60     @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
61     public static String standardIssuePath(ReadGraph graph, Resource converter, Variable property) throws DatabaseException {
62         Layer0 L0 = Layer0.getInstance(graph);
63         List<Resource> contexts = IssueUtils.getContextsForProperty(graph, property);
64         if(contexts.isEmpty()) return "";
65         Resource parent = graph.getPossibleObject(contexts.get(0), L0.PartOf);
66         if(parent == null) return "";
67         Resource issueRoot = Variables.getPossibleIndexRoot(graph, property);
68         if(issueRoot == null) return "";
69         Resource contextRoot = graph.sync(new PossibleIndexRoot(parent));
70         if(contextRoot == null) return "";
71         if(issueRoot.equals(contextRoot)) {
72                 String uri = graph.getURI(parent);
73                 String contextURI = graph.getURI(contextRoot);
74                 if (uri.equals(contextURI)) return "";
75                 return IssueUtils.pathString(uri, contextURI.length()+1);
76         } else {
77                 String uri = graph.getURI(parent);
78                 String modelURI = graph.getURI(graph.getRootLibrary());
79                 return IssueUtils.pathString(uri, modelURI.length()+1);
80         }
81     }
82
83     @SCLValue(type = "ReadGraph -> Resource -> a -> [Resource]")
84     public static List<Resource> standardIssueContexts(ReadGraph graph, Resource converter, Object property) throws DatabaseException {
85         if (property instanceof Variable) {
86             return IssueUtils.getContextsForProperty(graph, (Variable) property);
87         } else if (property instanceof Resource) {
88             Resource issue = (Resource) property;
89             IssueResource ISSUE = IssueResource.getInstance(graph);
90             Resource list = graph.getPossibleObject(issue, ISSUE.Issue_HasContexts);
91             if(list != null)
92                 return ListUtils.toList(graph, list);
93             else
94                 return Collections.emptyList();
95         }
96         throw new IllegalArgumentException("Unsupported property type: " + property);
97     }
98
99 }