]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ExtendedUris.java
Merge "Added resourceId and GUID to diagram DnD content" into release/1.35.1
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / ExtendedUris.java
1 package org.simantics.db.layer0.util;
2
3 import java.util.Collection;
4
5 import org.simantics.databoard.util.URIStringUtils;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.Statement;
9 import org.simantics.db.common.request.FunctionalStatementMapOfResource;
10 import org.simantics.db.common.uri.UnescapedChildMapOfResource;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.layer0.Layer0;
13 import org.simantics.scl.runtime.tuple.Tuple2;
14
15 public class ExtendedUris {
16
17     public static Resource resolveAbsoluteUri(ReadGraph graph, String uri) throws DatabaseException {
18         if(!uri.startsWith("http:/"))
19             throw new IllegalArgumentException("Invalid absolute URI '" + uri + "'.");
20         return resolveRelativeUri(graph, graph.getRootLibrary(), uri.substring(6));
21     }
22
23     public static Resource resolveRelativeUri(ReadGraph graph, Resource parent, String uri) throws DatabaseException {
24         int beginPos = 0;
25         while(beginPos < uri.length()) {
26             int endPos;
27             for(endPos = beginPos+1; endPos < uri.length(); ++endPos) {
28                 char c = uri.charAt(endPos);
29                 if(c == '/' || c == '#')
30                     break;
31             }
32
33             char c = uri.charAt(beginPos);
34             if(c == '/') {
35                 String escapedName = uri.substring(beginPos+1, endPos);
36                 String name = URIStringUtils.unescape(escapedName);
37                 parent = graph.syncRequest(new UnescapedChildMapOfResource(parent)).get(name);
38                 if(parent == null)
39                     throw new DatabaseException("Didn't find a child " + name + " for " + uri.substring(0, beginPos));
40             }
41             else if(c == '#') {
42                 String escapedName = uri.substring(beginPos+1, endPos);
43                 String name = URIStringUtils.unescape(escapedName);
44                 parent = graph.syncRequest(new FunctionalStatementMapOfResource(parent)).get(name);
45                 if(parent == null)
46                     throw new DatabaseException("Didn't find a property " + name);
47             }
48             else
49                 throw new IllegalArgumentException("Invalid relative URI '" + uri + "'.");
50             beginPos = endPos;
51         }
52         return parent;
53     }
54     
55     private static boolean initialized = false;
56     private static Resource ElementToComponent;
57     private static void initialize(ReadGraph graph) throws DatabaseException {
58         ElementToComponent = graph.getResource("http://www.simantics.org/Modeling-1.2/ElementToComponent");
59         initialized = true;
60     }
61     
62     public static Tuple2 getPrimaryFunctionalReference(ReadGraph graph, Resource r) throws DatabaseException {
63         if(!initialized)
64             initialize(graph);
65         Statement stat = graph.getPossibleStatement(r, ElementToComponent);
66         if(stat != null)
67             return statementToResult(graph, stat);
68         return null;
69     }
70     
71     public static Tuple2 getSecondaryFunctionalReference(ReadGraph graph, Resource r) throws DatabaseException {
72         Layer0 L0 = Layer0.getInstance(graph);
73         Collection<Statement> statements = graph.getStatements(r, L0.PropertyOf);
74         if(statements.size() == 1) 
75             for(Statement stat : statements)
76                 return statementToResult(graph, stat);
77         return null;
78     }
79     
80     private static Tuple2 statementToResult(ReadGraph graph, Statement stat) throws DatabaseException {
81         return new Tuple2(stat.getObject(), graph.getInverse(stat.getPredicate()));
82     }
83
84 }