]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/report/templates/CompleteStructureWriter.java
Externalize org.simantics.document.linking.ui
[simantics/platform.git] / bundles / org.simantics.document.linking.ui / src / org / simantics / document / linking / report / templates / CompleteStructureWriter.java
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.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;
21
22 /**
23  *  ? Configuration folder 1
24  *    \95 Diagram 1
25  *        o Link1: Document information
26  *        o Link2: Document information
27  *        o Link3: Document information
28  *        o Module 1
29  *            ? Link4: Document information
30  *            ? Link5: Document information
31  *            ? Attribute 1
32  *                ? Link6: Document information
33  *            ? Attribute 2
34  *                ? Link7: Document information
35  *                ? Link8: Document information
36  *        o Module 2
37  *            ? Link9: Document information
38  *            
39  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
40  *
41  */
42 public class CompleteStructureWriter extends IndexQueryReport {
43                 ReadGraph graph;
44                 Resource model;
45                 Map<Object, Object> context;
46                 DocumentLink sl;
47                 DocumentResource doc;
48                 ResourceNameComparator parentComparator;
49                 Comparator<Resource> hierarchyComparator;
50                 
51                 @Override
52                 public String getName() {
53                         return "Complete Structure"; //$NON-NLS-1$
54                 }
55                 
56                 @Override
57                 public void sort(List<Resource> list) throws DatabaseException{
58                         Collections.sort(list, parentComparator);
59                 }
60                 
61                 @Override
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);
69                         
70                         table.addColumn("Name", 0.2); //$NON-NLS-1$
71                         table.addColumn("Attribute", 0.2); //$NON-NLS-1$
72                         table.addColumn("Value", 0.15); //$NON-NLS-1$
73                         table.addColumn("Document", 0.2); //$NON-NLS-1$
74                         table.addColumn("Comment", 0.25); //$NON-NLS-1$
75                         
76                         //lineWriter.nextPage();
77                         
78                         this.graph = graph;
79                         this.sl = DocumentLink.getInstance(graph);
80                         this.doc = DocumentResource.getInstance(graph);
81                         this.model = model;
82                         
83                         
84                         
85                         parentComparator = new SourceParentHierarchyWithPropComparator(graph,model);
86                         hierarchyComparator = new SourceParentDiagramComparator(graph,model);
87                                 
88                         clearProviders();
89                         addLineProvider(new HierarchyProvider());
90                         addCellProvider(new LinkCellProvider());
91                         
92                 }
93                 
94                 private class HierarchyProvider implements RowContentProvider<Resource> {
95                         
96                         @Override
97                         public void setText(Document writer, Resource previous, Resource current, Resource next, TextItem[] row) throws Exception {
98                                 boolean writeHierarchy = false;
99                                 if (previous == null)
100                                         writeHierarchy = true;
101                                 else  {
102                                         if (hierarchyComparator.compare(previous, current) != 0)
103                                                 writeHierarchy = true;
104                                 }
105                                 if (writeHierarchy) {
106                                         Resource obj = graph.getPossibleObject(current, sl.hasSource_Inverse);
107                                         Table table = writer.getCurrentElement(Table.class);
108
109                                         if (writer.getCurrentLine() > 2) {
110                                                 writer.nextPage();
111                                                 table = writer.nextElement(Table.class,Document.TOC);
112                                         }
113                                         
114                                         if (obj != null) {
115                                                 String text = ""; //$NON-NLS-1$
116                                                 List<Resource> path = SourceLinkUtil.getDiagramPath(graph, model, obj);
117                                                 
118                                                 if (path == null) {
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)
124                                                                         text += "/"; //$NON-NLS-1$
125                                                         }
126                                                 } else {
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)
131                                                                         text += "/"; //$NON-NLS-1$
132                                                         }
133                                                 }
134                                                 //row[0] = text;
135                                                 table.setTitle(text);
136                                                 
137                                                 TableOfContents toc = writer.getCurrentElement(TableOfContents.class);
138                                                 toc.addTocElement(text, table);
139                                         } else {
140                                                 //row[0] =  "Hierarchy missing";
141                                                 table.setTitle("Hierarchy missing"); //$NON-NLS-1$
142                                         }
143                                 }
144                         }
145                 }
146                 
147                 private class LinkCellProvider implements RowContentProvider<Resource> {
148                         @Override
149                         public void setText(Document writer, Resource previous, Resource source, Resource next, TextItem[] text)
150                                         throws Exception {
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);
158                                 }
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);
168                                                 if (value != null) {
169                                                         text[2] = writer.newItem(TextItem.class);
170                                                         text[2].setText(SourceLinkUtil.getValueString(value));
171                                                 }
172                                         } else {
173                                                 text[1].setText(Messages.CompleteStructureWriter_ErrorInPropertyRefrence);
174                                         }
175                                 }
176                                 Resource document = SourceLinkUtil.getReferredDocument(graph, source);
177                                 if (document != null) {
178                                         text[3] = getDocumentItem(document);
179                                 } else {
180                                         text[3] = getNonExistingDocumentItem();
181                                 }
182                                 
183                                 String comment = graph.getPossibleRelatedValue(source, sl.hasSourceComment);
184                                 if (comment != null) {
185                                         text[4] = writer.newItem(TextItem.class);
186                                         text[4].setText(comment);
187                                 }
188
189                         }
190                 }
191         }