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