]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 package org.simantics.db.layer0.genericrelation;\r
2 \r
3 import org.simantics.databoard.Bindings;\r
4 import org.simantics.datatypes.literal.GUID;\r
5 import org.simantics.db.ReadGraph;\r
6 import org.simantics.db.Resource;\r
7 import org.simantics.db.common.request.TypeString;\r
8 import org.simantics.db.exception.DatabaseException;\r
9 import org.simantics.layer0.Layer0;\r
10 \r
11 class Entry implements Comparable<Entry> {\r
12 \r
13         Entry(Resource parent, Resource resource, String name, String types, String id) {\r
14                 this.parent = parent;\r
15                 this.resource = resource;\r
16                 this.name = name;\r
17                 this.types = types;\r
18                 this.id = id;\r
19         }\r
20 \r
21         Entry(ReadGraph graph, Layer0 L0, Resource resource) throws DatabaseException {\r
22                 this.parent = graph.getPossibleObject(resource, L0.PartOf);\r
23                 this.resource = resource;\r
24                 this.name = graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING);\r
25                 this.types = graph.syncRequest(new TypeString(L0, graph.getTypes(resource)));\r
26                 this.id = idFromGUID(graph.getPossibleRelatedValue(resource, L0.identifier, GUID.BINDING));\r
27         }\r
28         \r
29         private static String idFromGUID(GUID guid) {\r
30                 if(guid == null) return "";\r
31                 return guid.indexString();\r
32         }\r
33 \r
34         Entry(ReadGraph graph, Resource resource) throws DatabaseException {\r
35                 this(graph, Layer0.getInstance(graph), resource);\r
36         }\r
37 \r
38         final Resource parent;\r
39         final Resource resource;\r
40 \r
41         String types;\r
42         String name;\r
43         String id;\r
44         Resource principalType;\r
45 \r
46         @Override\r
47         public int compareTo(Entry o) {\r
48                 int names = name.compareTo(o.name);\r
49                 if(names != 0) return names;\r
50                 int resources = resource.compareTo(o.resource);\r
51                 if(resources != 0) return resources;\r
52                 return id.compareTo(o.id);\r
53         }\r
54 \r
55         @Override\r
56         public int hashCode() {\r
57                 return resource.hashCode();\r
58         }\r
59 \r
60         @Override\r
61         public boolean equals(Object obj) {\r
62                 Entry other = (Entry)obj;\r
63                 return resource.equals(other.resource);\r
64         }\r
65 \r
66 }