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.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.TableOfContents;
19 import org.simantics.document.linking.report.TextItem;
20 import org.simantics.document.linking.utils.SourceLinkUtil;
23 * ? Configuration folder 1
25 * o Link1: Document information
26 * o Link2: Document information
27 * o Link3: Document information
29 * ? Link4: Document information
30 * ? Link5: Document information
32 * ? Link6: Document information
34 * ? Link7: Document information
35 * ? Link8: Document information
37 * ? Link9: Document information
39 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
42 public class CompleteStructureWriter extends IndexQueryReport {
45 Map<Object, Object> context;
48 ResourceNameComparator parentComparator;
49 Comparator<Resource> hierarchyComparator;
52 public String getName() {
53 return "Complete Structure";
57 public void sort(List<Resource> list) throws DatabaseException{
58 Collections.sort(list, parentComparator);
62 public void start(final ReadGraph graph, final Resource model, Document writer, Map<Object, Object> context) throws Exception{
63 super.start(graph, model, writer, context);
64 this.context = context;
65 DocumentTitlePage titlePage = writer.newElement(DocumentTitlePage.class);
66 titlePage.writeTitle(graph, context);
67 writer.newElement(TableOfContents.class);
68 Table table = writer.newElement(Table.class,Document.TOC);
70 table.addColumn("Name", 0.2);
71 table.addColumn("Attribute", 0.2);
72 table.addColumn("Value", 0.15);
73 table.addColumn("Document", 0.2);
74 table.addColumn("Comment", 0.25);
76 //lineWriter.nextPage();
79 this.sl = DocumentLink.getInstance(graph);
80 this.doc = DocumentResource.getInstance(graph);
85 parentComparator = new SourceParentHierarchyWithPropComparator(graph,model);
86 hierarchyComparator = new SourceParentDiagramComparator(graph,model);
89 addLineProvider(new HierarchyProvider());
90 addCellProvider(new LinkCellProvider());
94 private class HierarchyProvider implements RowContentProvider<Resource> {
97 public void setText(Document writer, Resource previous, Resource current, Resource next, TextItem[] row) throws Exception {
98 boolean writeHierarchy = false;
100 writeHierarchy = true;
102 if (hierarchyComparator.compare(previous, current) != 0)
103 writeHierarchy = true;
105 if (writeHierarchy) {
106 Resource obj = graph.getPossibleObject(current, sl.hasSource_Inverse);
107 Table table = writer.getCurrentElement(Table.class);
109 if (writer.getCurrentLine() > 2) {
111 table = writer.nextElement(Table.class,Document.TOC);
116 List<Resource> path = SourceLinkUtil.getDiagramPath(graph, model, obj);
119 path = parentComparator.getPath(obj);
120 for (int i = 0 ; i < path.size()-1; i++) {
121 Resource r = path.get(i);
122 text += parentComparator.getText(r);
123 if (i < path.size()-2)
127 for (int i = 0 ; i < path.size(); i++) {
128 Resource r = path.get(i);
129 text += parentComparator.getText(r);
130 if (i < path.size()-1)
135 table.setTitle(text);
137 TableOfContents toc = writer.getCurrentElement(TableOfContents.class);
138 toc.addTocElement(text, table);
140 //row[0] = "Hierarchy missing";
141 table.setTitle("Hierarchy missing");
147 private class LinkCellProvider implements RowContentProvider<Resource> {
149 public void setText(Document writer, Resource previous, Resource source, Resource next, TextItem[] text)
151 Resource holder = graph.getSingleObject(source, sl.hasSource_Inverse);
152 String holderName = NameUtils.getSafeLabel(graph, holder);
153 if (holderName.length() == 0)
154 holderName = NameUtils.getSafeName(graph, holder);
155 if (previous == null || !graph.getSingleObject(previous, sl.hasSource_Inverse).equals(graph.getSingleObject(source, sl.hasSource_Inverse))) {
156 text[0] = writer.newItem(TextItem.class);
157 text[0].setText(holderName);
159 if (graph.isInstanceOf(source, sl.FunctionalSource)) {
160 Resource relation = graph.getPossibleObject(source, sl.consernsRelation);
161 text[1] = writer.newItem(TextItem.class);
162 if (relation != null) {
163 String relationName = NameUtils.getSafeLabel(graph, relation);
164 if (relationName.length() == 0)
165 relationName = NameUtils.getSafeName(graph, relation);
166 Object value = graph.getPossibleRelatedValue(holder, relation);
167 text[1].setText(relationName);
169 text[2] = writer.newItem(TextItem.class);
170 text[2].setText(SourceLinkUtil.getValueString(value));
173 text[1].setText("Error in property reference");
176 Resource document = SourceLinkUtil.getReferredDocument(graph, source);
177 if (document != null) {
178 text[3] = getDocumentItem(document);
180 text[3] = getNonExistingDocumentItem();
183 String comment = graph.getPossibleRelatedValue(source, sl.hasSourceComment);
184 if (comment != null) {
185 text[4] = writer.newItem(TextItem.class);
186 text[4].setText(comment);