]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/report/templates/SourceParentHierarchyWithPropComparator.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.document.linking.ui / src / org / simantics / document / linking / report / templates / SourceParentHierarchyWithPropComparator.java
1 package org.simantics.document.linking.report.templates;
2
3 import java.util.List;
4
5 import org.simantics.db.ReadGraph;
6 import org.simantics.db.Resource;
7 import org.simantics.document.linking.ontology.DocumentLink;
8
9 class SourceParentHierarchyWithPropComparator extends ResourceNameComparator {
10         DocumentLink sl;
11         
12         public SourceParentHierarchyWithPropComparator(ReadGraph graph, Resource model) {
13                 super(graph,model);
14                 sl = DocumentLink.getInstance(graph);
15         }
16         @Override
17         public int compare(Resource o1, Resource o2) {
18                 try {
19                         Resource r1 = graph.getSingleObject(o1, sl.hasSource_Inverse);
20                         Resource r2 = graph.getSingleObject(o2, sl.hasSource_Inverse);
21                         List<Resource> path1 = getPath(r1);
22                         List<Resource> path2 = getPath(r2);
23                         int count = Math.min(path1.size(), path2.size());
24                         int i = 0;
25                         int comp = 0;
26                         for (; i < count; i++) {
27                                 comp = super.compare(path1.get(i), path2.get(i));
28                                 if (comp != 0)
29                                         return comp;
30                         }
31                         if (path1.size() < path2.size())
32                                 return -1;
33                         else if (path2.size() > path1.size())
34                                 return 1;
35                         
36                         Resource rel1 = graph.getPossibleObject(o1, sl.consernsRelation);
37                         Resource rel2 = graph.getPossibleObject(o2, sl.consernsRelation);
38                         if (rel1 != null) {
39                                 if (rel2 != null)
40                                         return super.compare(rel1,rel2);
41                                 return 1;
42                         } else if (rel2 != null)
43                                 return -1;
44                         return 0;
45                         
46                 } catch (Exception e) {
47                         return 0;
48                 }
49                 
50         }
51 }