]> gerrit.simantics Code Review - simantics/platform.git/blob
c7fa811e43fc27a89e97f312daec0317b5aab526
[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.List;
7 import java.util.Map;
8
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.layer0.adapter.Instances;
12 import org.simantics.document.DocumentResource;
13 import org.simantics.document.linking.ontology.DocumentLink;
14 import org.simantics.document.linking.report.Document;
15 import org.simantics.document.linking.report.DocumentTitlePage;
16 import org.simantics.document.linking.report.RowContentProvider;
17 import org.simantics.document.linking.report.Table;
18 import org.simantics.document.linking.report.TextItem;
19 import org.simantics.document.linking.utils.SourceLinkUtil;
20 import org.simantics.modeling.ModelingResources;
21
22 public class DiagramStructureWriter extends DocumentWriter<Resource>{
23         ReadGraph graph;
24         Resource model;
25         Map<Object, Object> context;
26         DocumentLink sl;
27         ModelingResources mod;
28         DocumentResource doc;
29         NestedComparator<Resource> diagramDocumentComparator;
30         ResourceNameComparator diagramComparator;
31         
32         @Override
33         public String getName() {
34                 return "Diagram structure";
35         }
36         
37         @Override
38         public void start(ReadGraph graph, Resource model, Document lineWriter, Map<Object, Object> context)
39                         throws Exception {
40                 super.start(graph, model, lineWriter, context);
41                 this.context = context;
42                 DocumentTitlePage titlePage = lineWriter.newElement(DocumentTitlePage.class);
43                 titlePage.writeTitle(graph, context);
44                 
45                 Table table = lineWriter.newElement(Table.class);
46                 table.addColumn("Name", 0.4);
47                 table.addColumn("Document", 0.6);
48                 
49                 //lineWriter.nextPage();
50                 
51                 this.graph = graph;
52                 this.sl = DocumentLink.getInstance(graph);
53                 this.mod = ModelingResources.getInstance(graph);
54                 this.doc = DocumentResource.getInstance(graph);
55                 this.model = model;
56                 
57                 diagramComparator = new SourceParentDiagramComparator(graph,model);
58                 diagramDocumentComparator = new NestedComparator<Resource>();
59                 diagramDocumentComparator.addComparator(diagramComparator);
60                 diagramDocumentComparator.addComparator(new SourceReferenceComparator(graph, model));
61                 
62                 clearProviders();
63                 addCellProvider(new HierarchyContentProvider());
64                 addCellProvider(new DocumentContentProvider());
65         }
66         
67         
68         @Override
69         public List<Resource> getReportItems(ReadGraph graph)   throws Exception {
70                 Instances instancesQuery = graph.adapt(sl.Source, Instances.class);
71         Collection<Resource> found = instancesQuery.find(graph, model);
72         List<Resource> list = new ArrayList<Resource>();
73         for (Resource source : found) {
74                 Resource parent = graph.getSingleObject(source, sl.hasSource_Inverse);
75                 if (graph.hasStatement(parent, mod.CompositeToDiagram))
76                         list.add(source);
77         }
78         Collections.sort(list, diagramDocumentComparator);
79         return list;
80         
81         }
82
83         private class HierarchyContentProvider implements RowContentProvider<Resource> {
84                 @Override
85                 public void setText(Document writer, Resource previous,
86                                 Resource current, Resource next, TextItem[] row) throws Exception {
87                         boolean writeHierarchy = false;
88                         if (previous == null)
89                                 writeHierarchy = true;
90                         else  {
91                                 if (diagramComparator.compare(previous, current) != 0)
92                                         writeHierarchy = true;
93                         }
94                         if (writeHierarchy) {
95 //                              if (lineWriter.getCurrentLine() > 4)
96 //                                      lineWriter.nextPage();
97                                 Resource obj = graph.getPossibleObject(current, sl.hasSource_Inverse);
98                                 row[0] = writer.newItem(TextItem.class);
99                                 if (obj != null) {
100                                         List<Resource> path = SourceLinkUtil.getDiagramPath(graph, model, obj);
101                                         if (writer.getAvailableLines() < 2)
102                                                 writer.nextPage();
103                                         String text = "";
104                                         for (int i = 0 ; i < path.size(); i++) {
105                                                 Resource r = path.get(i);
106                                                 text += diagramComparator.getText(r);
107                                                 if (i < path.size()-1)
108                                                         text += "/";
109                                         }
110                                         row[0].setText(text);
111                                 } else {
112                                         row[0].setText("Hierarchy missing");
113                                 }
114                         }
115                         
116                 }
117         }
118         
119         private class DocumentContentProvider implements RowContentProvider<Resource> {
120                 @Override
121                 public void setText(Document writer, Resource previous,
122                                 Resource current, Resource next, TextItem[] row) throws Exception {
123                         Resource document = SourceLinkUtil.getReferredDocument(graph, current);
124                         if (document != null) {
125                                 row[1] = getDocumentItem(document);
126                         } else {
127                                 row[1] = getNonExistingDocumentItem();
128                         }
129                         
130                 }
131         }
132         
133
134 }