]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/Entry.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / genericrelation / Entry.java
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/Entry.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/Entry.java
new file mode 100644 (file)
index 0000000..733a805
--- /dev/null
@@ -0,0 +1,66 @@
+package org.simantics.db.layer0.genericrelation;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.datatypes.literal.GUID;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.TypeString;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+\r
+class Entry implements Comparable<Entry> {\r
+\r
+       Entry(Resource parent, Resource resource, String name, String types, String id) {\r
+               this.parent = parent;\r
+               this.resource = resource;\r
+               this.name = name;\r
+               this.types = types;\r
+               this.id = id;\r
+       }\r
+\r
+       Entry(ReadGraph graph, Layer0 L0, Resource resource) throws DatabaseException {\r
+               this.parent = graph.getPossibleObject(resource, L0.PartOf);\r
+               this.resource = resource;\r
+               this.name = graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING);\r
+               this.types = graph.syncRequest(new TypeString(L0, graph.getTypes(resource)));\r
+               this.id = idFromGUID(graph.getPossibleRelatedValue(resource, L0.identifier, GUID.BINDING));\r
+       }\r
+       \r
+       private static String idFromGUID(GUID guid) {\r
+               if(guid == null) return "";\r
+               return guid.indexString();\r
+       }\r
+\r
+       Entry(ReadGraph graph, Resource resource) throws DatabaseException {\r
+               this(graph, Layer0.getInstance(graph), resource);\r
+       }\r
+\r
+       final Resource parent;\r
+       final Resource resource;\r
+\r
+       String types;\r
+       String name;\r
+       String id;\r
+       Resource principalType;\r
+\r
+       @Override\r
+       public int compareTo(Entry o) {\r
+               int names = name.compareTo(o.name);\r
+               if(names != 0) return names;\r
+               int resources = resource.compareTo(o.resource);\r
+               if(resources != 0) return resources;\r
+               return id.compareTo(o.id);\r
+       }\r
+\r
+       @Override\r
+       public int hashCode() {\r
+               return resource.hashCode();\r
+       }\r
+\r
+       @Override\r
+       public boolean equals(Object obj) {\r
+               Entry other = (Entry)obj;\r
+               return resource.equals(other.resource);\r
+       }\r
+\r
+}
\ No newline at end of file