]> gerrit.simantics Code Review - simantics/platform.git/blob
5891ccb268e1d69ef4c3e1eaed6cfccdafed62cc
[simantics/platform.git] /
1 package org.simantics.document.linking.report.templates;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashSet;
7 import java.util.List;
8 import java.util.Set;
9
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.adapter.Instances;
14 import org.simantics.document.DocumentResource;
15 import org.simantics.document.linking.ontology.DocumentLink;
16 import org.simantics.document.linking.utils.SourceLinkUtil;
17
18 /**
19  * Writes report of referred documents.
20  * 
21  * Model information
22     □ Document folder 1
23         • Document 1-1
24         • Document 1-2 rev1
25         • Document 1-2 rev2
26         • Document 1-2 rev3
27         • Document 1-3
28     □ Document folder 2
29         • Document folder 3
30             o Document 3-1
31             o Document 3-2
32         • Document folder 4
33             o Document 4-1
34             o Document 4-2
35  * 
36  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
37  *
38  */
39 public class ReferredDocumentWriter extends ModelDocumentWriter{
40
41         @Override
42         public String getName() {
43                 return "Referred Documents";
44         }
45         
46
47         @Override
48         public List<List<Resource>> getReportItems(ReadGraph graph)     throws DatabaseException {
49                 DocumentResource doc = DocumentResource.getInstance(graph);
50                 DocumentLink sl = DocumentLink.getInstance(graph);
51                 Instances instancesQuery = graph.adapt(sl.Source, Instances.class);
52         Collection<Resource> found = instancesQuery.find(graph, model);
53         Set<Resource> referred = new HashSet<Resource>();
54         for (Resource source : found) {
55                 Resource ref = SourceLinkUtil.getReferredDocument(graph, source);
56                 if (ref != null)
57                         referred.add(ref);
58         }
59         found = null;
60         List<List<Resource>> result = new ArrayList<List<Resource>>();
61         for (Resource ref : referred) {
62                 if (!graph.isInstanceOf(ref, doc.Document))
63                         continue;
64                 result.add(SourceLinkUtil.getPath(graph,model,ref));
65         }
66         referred = null;
67         Collections.sort(result, new PathComparator(graph));
68         return result;
69         }
70
71 }