1 package org.simantics.document.linking.report.templates;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
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;
22 public class DiagramStructureWriter extends DocumentWriter<Resource>{
25 Map<Object, Object> context;
27 ModelingResources mod;
29 NestedComparator<Resource> diagramDocumentComparator;
30 ResourceNameComparator diagramComparator;
33 public String getName() {
34 return "Diagram structure";
38 public void start(ReadGraph graph, Resource model, Document lineWriter, Map<Object, Object> context)
40 super.start(graph, model, lineWriter, context);
41 this.context = context;
42 DocumentTitlePage titlePage = lineWriter.newElement(DocumentTitlePage.class);
43 titlePage.writeTitle(graph, context);
45 Table table = lineWriter.newElement(Table.class);
46 table.addColumn("Name", 0.4);
47 table.addColumn("Document", 0.6);
49 //lineWriter.nextPage();
52 this.sl = DocumentLink.getInstance(graph);
53 this.mod = ModelingResources.getInstance(graph);
54 this.doc = DocumentResource.getInstance(graph);
57 diagramComparator = new SourceParentDiagramComparator(graph,model);
58 diagramDocumentComparator = new NestedComparator<Resource>();
59 diagramDocumentComparator.addComparator(diagramComparator);
60 diagramDocumentComparator.addComparator(new SourceReferenceComparator(graph, model));
63 addCellProvider(new HierarchyContentProvider());
64 addCellProvider(new DocumentContentProvider());
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))
78 Collections.sort(list, diagramDocumentComparator);
83 private class HierarchyContentProvider implements RowContentProvider<Resource> {
85 public void setText(Document writer, Resource previous,
86 Resource current, Resource next, TextItem[] row) throws Exception {
87 boolean writeHierarchy = false;
89 writeHierarchy = true;
91 if (diagramComparator.compare(previous, current) != 0)
92 writeHierarchy = true;
95 // if (lineWriter.getCurrentLine() > 4)
96 // lineWriter.nextPage();
97 Resource obj = graph.getPossibleObject(current, sl.hasSource_Inverse);
98 row[0] = writer.newItem(TextItem.class);
100 List<Resource> path = SourceLinkUtil.getDiagramPath(graph, model, obj);
101 if (writer.getAvailableLines() < 2)
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)
110 row[0].setText(text);
112 row[0].setText("Hierarchy missing");
119 private class DocumentContentProvider implements RowContentProvider<Resource> {
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);
127 row[1] = getNonExistingDocumentItem();