]> gerrit.simantics Code Review - simantics/platform.git/blob
6b8ae70a221ff387ad8591423ceaf757932731d5
[simantics/platform.git] /
1 package org.simantics.document.linking.report.templates;
2
3 import java.util.Collections;
4 import java.util.Comparator;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.common.utils.NameUtils;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.document.linking.ontology.DocumentLink;
13 import org.simantics.document.linking.report.Document;
14 import org.simantics.document.linking.report.Document.TextSize;
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.TableOfContents;
19 import org.simantics.document.linking.report.TextItem;
20 import org.simantics.document.linking.utils.SourceLinkUtil;
21
22 /**
23  * Model information
24  * ? Document 1 rev1
25  *    \95 Link1: Diagram, module, attribute information
26  *    \95 Link2: Diagram, module, attribute information
27  * ? Document 1 rev2
28  *    \95 Link3: Diagram, module, attribute information
29  * ? Document 1 rev3
30  *    \95 Link4: Diagram, module, attribute information
31  * ? Document 2
32  *    \95 Link5: Diagram, module, attribute information
33  *   
34  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
35  *
36  */
37 public class DocumentStructureWriter extends IndexQueryReport {
38         ReadGraph graph;
39         Resource model;
40         Map<Object, Object> context;
41         DocumentLink sl;
42         Comparator<Resource> referenceComparator;
43         Comparator<Resource> parentComparator;
44         SourceParentDiagramComparator diagramComparator;
45         @Override
46         public void sort(List<Resource> list) throws DatabaseException{
47                 NestedComparator<Resource> comp = new NestedComparator<Resource>();
48                 comp.addComparator(referenceComparator);
49                 comp.addComparator(parentComparator);
50                 Collections.sort(list, comp);
51         }
52         
53         @Override
54         public String getName() {
55                 return "Document Structure";
56         }
57         
58         @Override
59         public void start(ReadGraph graph, Resource model,Document writer, Map<Object, Object> context) throws Exception{
60                 super.start(graph, model, writer, context);
61                 this.context = context;
62                 DocumentTitlePage titlePage = writer.newElement(DocumentTitlePage.class);
63                 titlePage.writeTitle(graph, context);
64                 writer.newElement(TableOfContents.class);
65                 Table table = writer.newElement(Table.class,Document.TOC);
66                 table.addColumn("Name", 0.2);
67                 table.addColumn("Attribute", 0.35);
68                 table.addColumn("Value", 0.15);
69                 table.addColumn("Comment", 0.3);
70                 
71                 //lineWriter.nextPage();
72                 this.graph = graph;
73                 this.sl = DocumentLink.getInstance(graph);
74                 this.model = model;
75                 referenceComparator = new SourceReferenceComparator(graph,model);
76                 parentComparator = new SourceParentHierarchyComparator(graph,model);
77                 diagramComparator = new SourceParentDiagramComparator(graph, model);
78                 clearProviders();
79                 LinkContentProvider linkContentProvider = new LinkContentProvider();
80                 HierarchyContentProvider hierarchyContentProvider = new HierarchyContentProvider(linkContentProvider);
81                 addLineProvider(hierarchyContentProvider);
82                 addCellProvider(linkContentProvider);
83         }
84
85         private class HierarchyContentProvider implements RowContentProvider<Resource> {
86                 
87                 private LinkContentProvider linkContentProvider;
88                 
89                 
90                 
91                 public HierarchyContentProvider(LinkContentProvider linkContentProvider) {
92                         super();
93                         this.linkContentProvider = linkContentProvider;
94                 }
95
96
97
98                 @Override
99                 public void setText(Document writer, Resource previous,
100                                         Resource current, Resource next, TextItem[] row) throws Exception {
101                         boolean writeDoc = false;
102                         boolean writeDiag = false;
103                         boolean writeToc = false;
104                         Table table = writer.getCurrentElement(Table.class);
105                         if (previous == null) {
106                                 writeDoc = true;
107                                 writeDiag = true;
108                                 writeToc = true;
109                         } else if (writer.getCurrentLine() == 1) {
110                                 writeDoc = true;
111                                 writeDiag = true;
112                                 writeToc = referenceComparator.compare(previous, current) != 0;
113                         } else  {
114                                 if (referenceComparator.compare(previous, current) != 0) {
115                                         writeDoc = true;
116                                         writeDiag = true;
117                                         writeToc = true;
118                                         linkContentProvider.forceLabel = true;
119                                 } else {
120                                         if (diagramComparator.compare(previous, current) != 0) {
121                                                 writeDiag = true;
122                                                 linkContentProvider.forceLabel = true;
123                                         }
124                                 }
125                         }
126                         
127                         
128                         if (writeDoc) {
129                                 if (writer.getAvailableLines() < 8) {
130                                         writer.nextPage();
131                                         table = writer.nextElement(Table.class,Document.TOC);
132                                 }
133                                 else if (writer.getCurrentLine() > 2)
134                                         table = writer.nextElement(Table.class,Document.TOC);
135                                 
136                                 
137                                 Resource doc2 = SourceLinkUtil.getReferredDocument(graph, current);
138 //                              TextSize size = table.getTextSize();
139 //                              table.setTextSize(TextSize.MEDIUM);
140                                 TextItem label = null;
141                                 if (doc2 != null) {
142                                         label = getDocumentItem(doc2);
143                                 } else {
144                                         label = getNonExistingDocumentItem();
145                                 }
146                                 if (writeToc) {
147                                         TableOfContents toc = writer.getCurrentElement(TableOfContents.class);
148                                         toc.addTocElement(label.getText(), table);
149                                 }
150                                 //table.writeRow(label);
151                                 table.setTitle(label);
152 //                              table.setTextSize(size);
153                         }
154                         if (writeDiag) {
155                                 if (writer.getAvailableLines() < 3)
156                                         writer.nextPage();
157                                 Resource parent = graph.getSingleObject(current, sl.hasSource_Inverse);
158                                 List<Resource> path = SourceLinkUtil.getDiagramPath(graph, model, parent);
159                                 if (path != null) {
160                                         TextSize size = table.getTextSize();
161                                         table.setTextSize(TextSize.MEDIUM);
162                                         table.writeRow("  " + diagramComparator.getText(path.get(path.size()-1)));
163                                         table.setTextSize(size);
164                                 }
165                         }
166                                 
167                 }
168         }
169         
170         
171         private class LinkContentProvider implements RowContentProvider<Resource> {
172                 
173                 private boolean forceLabel = false;
174                 
175                 @Override
176                 public void setText(Document writer, Resource previous,
177                                 Resource source, Resource next, TextItem[] text) throws Exception {
178                         Resource holder = graph.getSingleObject(source, sl.hasSource_Inverse);
179                         if (forceLabel || previous == null || !graph.getSingleObject(previous, sl.hasSource_Inverse).equals(graph.getSingleObject(source, sl.hasSource_Inverse))) {
180                                 String holderName = NameUtils.getSafeLabel(graph, holder);
181                                 if (holderName.length() == 0)
182                                         holderName = NameUtils.getSafeName(graph, holder);
183                                 text[0] = writer.newItem(TextItem.class);
184                                 text[0].setText(holderName);
185                                 forceLabel = false;
186                         }
187                         if (graph.isInstanceOf(source, sl.FunctionalSource)) {
188                                 Resource relation = graph.getPossibleObject(source, sl.consernsRelation);
189                                 text[1] = writer.newItem(TextItem.class);
190                                 if (relation != null) {
191                                         String relationName = NameUtils.getSafeLabel(graph, relation);
192                                         if (relationName.length() == 0)
193                                                 relationName = NameUtils.getSafeName(graph, relation);
194                                         Object value = graph.getPossibleRelatedValue(holder, relation);
195                                         text[1].setText(relationName);
196                                         if (value != null) {
197                                                 text[2] = writer.newItem(TextItem.class);
198                                                 text[2].setText(SourceLinkUtil.getValueString(value));
199                                         }
200                                 } else {
201                                         text[1].setText("Error in property reference ");
202                                 }
203                         }
204                         
205                         String comment = graph.getPossibleRelatedValue(source, sl.hasSourceComment);
206                         if (comment != null) {
207                                 text[3] = writer.newItem(TextItem.class);
208                                 text[3].setText(comment);
209                         }
210                         
211                 }
212                 
213         }
214 }