1 package org.simantics.document.linking.report.templates;
3 import java.util.Collections;
4 import java.util.Comparator;
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;
25 *
\95 Link1: Diagram, module, attribute information
26 *
\95 Link2: Diagram, module, attribute information
28 *
\95 Link3: Diagram, module, attribute information
30 *
\95 Link4: Diagram, module, attribute information
32 *
\95 Link5: Diagram, module, attribute information
34 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
37 public class DocumentStructureWriter extends IndexQueryReport {
40 Map<Object, Object> context;
42 Comparator<Resource> referenceComparator;
43 Comparator<Resource> parentComparator;
44 SourceParentDiagramComparator diagramComparator;
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);
54 public String getName() {
55 return "Document Structure";
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);
71 //lineWriter.nextPage();
73 this.sl = DocumentLink.getInstance(graph);
75 referenceComparator = new SourceReferenceComparator(graph,model);
76 parentComparator = new SourceParentHierarchyComparator(graph,model);
77 diagramComparator = new SourceParentDiagramComparator(graph, model);
79 LinkContentProvider linkContentProvider = new LinkContentProvider();
80 HierarchyContentProvider hierarchyContentProvider = new HierarchyContentProvider(linkContentProvider);
81 addLineProvider(hierarchyContentProvider);
82 addCellProvider(linkContentProvider);
85 private class HierarchyContentProvider implements RowContentProvider<Resource> {
87 private LinkContentProvider linkContentProvider;
91 public HierarchyContentProvider(LinkContentProvider linkContentProvider) {
93 this.linkContentProvider = linkContentProvider;
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) {
109 } else if (writer.getCurrentLine() == 1) {
112 writeToc = referenceComparator.compare(previous, current) != 0;
114 if (referenceComparator.compare(previous, current) != 0) {
118 linkContentProvider.forceLabel = true;
120 if (diagramComparator.compare(previous, current) != 0) {
122 linkContentProvider.forceLabel = true;
129 if (writer.getAvailableLines() < 8) {
131 table = writer.nextElement(Table.class,Document.TOC);
133 else if (writer.getCurrentLine() > 2)
134 table = writer.nextElement(Table.class,Document.TOC);
137 Resource doc2 = SourceLinkUtil.getReferredDocument(graph, current);
138 // TextSize size = table.getTextSize();
139 // table.setTextSize(TextSize.MEDIUM);
140 TextItem label = null;
142 label = getDocumentItem(doc2);
144 label = getNonExistingDocumentItem();
147 TableOfContents toc = writer.getCurrentElement(TableOfContents.class);
148 toc.addTocElement(label.getText(), table);
150 //table.writeRow(label);
151 table.setTitle(label);
152 // table.setTextSize(size);
155 if (writer.getAvailableLines() < 3)
157 Resource parent = graph.getSingleObject(current, sl.hasSource_Inverse);
158 List<Resource> path = SourceLinkUtil.getDiagramPath(graph, model, parent);
160 TextSize size = table.getTextSize();
161 table.setTextSize(TextSize.MEDIUM);
162 table.writeRow(" " + diagramComparator.getText(path.get(path.size()-1)));
163 table.setTextSize(size);
171 private class LinkContentProvider implements RowContentProvider<Resource> {
173 private boolean forceLabel = false;
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);
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);
197 text[2] = writer.newItem(TextItem.class);
198 text[2].setText(SourceLinkUtil.getValueString(value));
201 text[1].setText("Error in property reference ");
205 String comment = graph.getPossibleRelatedValue(source, sl.hasSourceComment);
206 if (comment != null) {
207 text[3] = writer.newItem(TextItem.class);
208 text[3].setText(comment);