]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.issues.common/src/org/simantics/issues/common/All.java
Allow overriding issue hidden-ness/hiding logic in inheriting ontologies
[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 import org.simantics.scl.runtime.function.Function1;
18
19 public class All {
20         
21     @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
22     public static String defaultDescription(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
23         return context.getParent(graph).getLabel(graph);
24     }
25     
26     @SCLValue(type = "a -> b -> c")
27     public static Object dependencyBaseRealizationFunction(Object _graph, Object _model) throws DatabaseException {
28 //        ReadGraph graph = (ReadGraph)_graph;
29         Resource model = (Resource)_model;
30         return model;
31 //        SimulationResource SIMU = SimulationResource.getInstance(graph);
32 //        if(graph.isInstanceOf(model, SIMU.Model))
33 //            return graph.getSingleObject(model, SimulationResource.getInstance(graph).HasConfiguration);
34 //        else return null;
35
36     }
37     
38     @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
39     public static String standardIssueSeverity(ReadGraph graph, Resource converter, Variable property) throws DatabaseException {
40         Layer0 L0 = Layer0.getInstance(graph);
41         IssueResource ISSUE = IssueResource.getInstance(graph);
42         Resource issue = property.getParent(graph).getRepresents(graph);
43         Resource severity = graph.getPossibleObject(issue, ISSUE.Issue_HasSeverity);
44         if (severity == null)
45                 return "Undefined";
46         return graph.getPossibleRelatedValue(severity, L0.HasName, Bindings.STRING);
47     }
48
49     @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
50     public static String standardIssueResource(ReadGraph graph, Resource converter, Variable property) throws DatabaseException {
51         Layer0 L0 = Layer0.getInstance(graph);
52         List<Resource> contexts = IssueUtils.getContextsForProperty(graph, property);
53         if(contexts.isEmpty()) return "";
54         Resource context = contexts.get(0);
55         String name = graph.getPossibleRelatedValue(context, L0.HasName, Bindings.STRING);
56         if((name != null && !name.isEmpty())) return name;
57         if(!graph.hasStatement(context)) return "Removed - please run batch validations";
58         return context.toString();
59     }
60
61     @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
62     public static String standardIssuePath(ReadGraph graph, Resource converter, Variable property) throws DatabaseException {
63         Layer0 L0 = Layer0.getInstance(graph);
64         List<Resource> contexts = IssueUtils.getContextsForProperty(graph, property);
65         if(contexts.isEmpty()) return "";
66         Resource parent = graph.getPossibleObject(contexts.get(0), L0.PartOf);
67         if(parent == null) return "";
68         Resource issueRoot = Variables.getPossibleIndexRoot(graph, property);
69         if(issueRoot == null) return "";
70         Resource contextRoot = graph.sync(new PossibleIndexRoot(parent));
71         if(contextRoot == null) return "";
72         if(issueRoot.equals(contextRoot)) {
73                 String uri = graph.getURI(parent);
74                 String contextURI = graph.getURI(contextRoot);
75                 if (uri.equals(contextURI)) return "";
76                 return IssueUtils.pathString(uri, contextURI.length()+1);
77         } else {
78                 String uri = graph.getURI(parent);
79                 String modelURI = graph.getURI(graph.getRootLibrary());
80                 return IssueUtils.pathString(uri, modelURI.length()+1);
81         }
82     }
83
84     @SCLValue(type = "ReadGraph -> Resource -> a -> [Resource]")
85     public static List<Resource> standardIssueContexts(ReadGraph graph, Resource converter, Object property) throws DatabaseException {
86         if (property instanceof Variable) {
87             return IssueUtils.getContextsForProperty(graph, (Variable) property);
88         } else if (property instanceof Resource) {
89             Resource issue = (Resource) property;
90             IssueResource ISSUE = IssueResource.getInstance(graph);
91             Resource list = graph.getPossibleObject(issue, ISSUE.Issue_HasContexts);
92             if(list != null)
93                 return ListUtils.toList(graph, list);
94             else
95                 return Collections.emptyList();
96         }
97         throw new IllegalArgumentException("Unsupported property type: " + property);
98     }
99
100     @SCLValue(type = "ReadGraph -> Resource -> a -> b")
101     public static Function1<Boolean, Boolean> standardIssueHider(ReadGraph graph, Resource converter, Object property) throws DatabaseException {
102         if (property instanceof Variable) {
103             Variable hider = (Variable) property;
104             Variable issue = hider.getParent(graph);
105             Resource r = issue.getPossibleRepresents(graph);
106             if (r != null)
107                 return new HideFunction(r);
108         }
109         return null;
110     }
111
112     @SCLValue(type = "ReadGraph -> Resource -> a -> Boolean")
113     public static Boolean standardIssueHidden(ReadGraph graph, Resource converter, Object property) throws DatabaseException {
114         if (property instanceof Variable) {
115             Variable hidden = (Variable) property;
116             Variable issue = hidden.getParent(graph);
117             Resource r = issue.getPossibleRepresents(graph);
118             if (r != null) {
119                 IssueResource ISSUE = IssueResource.getInstance(graph);
120                 return graph.hasStatement(r, ISSUE.Hidden);
121             }
122         }
123         return false;
124     }
125
126 }