]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document/src/org/simantics/document/DocumentUtils.java
Make Write-interfaces as @FunctionalInterface for lambdas
[simantics/platform.git] / bundles / org.simantics.document / src / org / simantics / document / DocumentUtils.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.document;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
18
19 import org.simantics.databoard.Bindings;
20 import org.simantics.databoard.util.URIStringUtils;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.RequestProcessor;
23 import org.simantics.db.Resource;
24 import org.simantics.db.Session;
25 import org.simantics.db.VirtualGraph;
26 import org.simantics.db.WriteGraph;
27 import org.simantics.db.common.request.PossibleIndexRoot;
28 import org.simantics.db.common.request.ResourceRead;
29 import org.simantics.db.common.request.UniqueRead;
30 import org.simantics.db.common.request.WriteResultRequest;
31 import org.simantics.db.common.utils.Logger;
32 import org.simantics.db.exception.DatabaseException;
33 import org.simantics.db.layer0.variable.Variable;
34 import org.simantics.db.layer0.variable.Variables;
35 import org.simantics.document.function.WikiDocumentNodeImpl;
36 import org.simantics.document.node.Composite;
37 import org.simantics.layer0.Layer0;
38 import org.simantics.scenegraph.loader.ScenegraphLoaderProcess;
39 import org.simantics.scenegraph.loader.ScenegraphLoaderUtils;
40 import org.simantics.scenegraph.loader.ScenegraphVariable;
41 import org.simantics.scenegraph.ontology.ScenegraphResources;
42 import org.simantics.simulation.ontology.SimulationResource;
43 import org.simantics.wiki.ui.SimanticsDialect;
44
45 import com.lowagie.text.Document;
46 import com.lowagie.text.DocumentException;
47 import com.lowagie.text.pdf.PdfWriter;
48
49 public class DocumentUtils {
50
51         static class DeepDocs extends ResourceRead<List<Resource>> {
52
53                 protected DeepDocs(Resource resource) {
54                         super(resource);
55                 }
56
57                 @Override
58                 public List<Resource> perform(ReadGraph graph) throws DatabaseException {
59
60                         DocumentResource DOC = DocumentResource.getInstance(graph);
61                         Resource doc = graph.getPossibleObject(resource, DOC.HasDocument);
62
63                         Layer0 L0 = Layer0.getInstance(graph);
64                         Collection<Resource> children = graph.getObjects(resource, L0.ConsistsOf);
65                         if(children.isEmpty()) {
66                                 if(doc != null) return Collections.singletonList(resource);
67                                 else return Collections.emptyList();
68                         } else {
69                                 ArrayList<Resource> result = new ArrayList<Resource>();
70                                 if(doc != null) result.add(resource);
71                                 for(Resource child : children)
72                                         result.addAll(graph.syncRequest(new DeepDocs(child)));
73                                 return result;
74                         }
75                         
76                 }
77          
78         }
79         
80         public DocumentSettings getDocumentSettings(RequestProcessor processor, final Resource resource) throws DatabaseException {
81
82                 return processor.syncRequest(new ResourceRead<DocumentSettings>(resource) {
83
84                         @Override
85                         public DocumentSettings perform(ReadGraph graph) throws DatabaseException {
86
87                                 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(resource));
88                                 if(indexRoot == null) return DocumentSettings.DEFAULT;
89                                 
90                                 DocumentResource DOC = DocumentResource.getInstance(graph);
91                                 DocumentSettings result = graph.getPossibleRelatedValue(indexRoot, DOC.documentSettings, DocumentSettings.BINDING);
92                                 if(result == null) return DocumentSettings.DEFAULT;
93                                 return result;
94                                 
95                         }
96                         
97                 });
98
99         }
100
101         public void getDocumentCSSText(RequestProcessor processor, final Resource resource, final StringBuilder css) throws DatabaseException {
102
103                 css.append(processor.sync(new ResourceRead<String>(resource) {
104
105                         @Override
106                         public String perform(ReadGraph graph) throws DatabaseException {
107
108                                 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(resource));
109                                 if(indexRoot == null) return "";
110                                 
111                                 DocumentResource DOC = DocumentResource.getInstance(graph);
112                                 String css = graph.getPossibleRelatedValue(indexRoot, DOC.cssDocument, Bindings.STRING);
113                                 if(css == null) return "";
114                                 
115                                 return css;
116                                 
117                         }
118                         
119                 }));
120
121         }
122
123         public void getDocumentWikiTextRecursive(Session session, final Resource resource, final StringBuilder wiki, final StringBuilder css) throws DatabaseException {
124
125                 List<Resource> rs = session.sync(new DeepDocs(resource));
126                 for(Resource r : rs) {
127                         getDocumentWikiText(session, r, wiki);
128                 }
129                 
130                 getDocumentCSSText(session, resource, css);
131                 
132         }
133
134         public void getDocumentWikiText(Session session, final Resource resource, final StringBuilder b) {
135                 
136                 try {
137
138                         final ScenegraphLoaderProcess loader = new ScenegraphLoaderProcess(new Composite(), "CreatePDFAction");
139                         
140                         final Variable context = session.sync(new WriteResultRequest<Variable>(session.getService(VirtualGraph.class)) {
141
142                                 @Override
143                                 public Variable perform(WriteGraph graph) throws DatabaseException {
144
145                                         DocumentResource DOC = DocumentResource.getInstance(graph);
146                                         Resource doc = graph.getPossibleObject(resource, DOC.HasDocument);
147                                         if(doc == null) return null;
148
149                                         Layer0 L0 = Layer0.getInstance(graph);
150                                         ScenegraphResources SG = ScenegraphResources.getInstance(graph);
151                                         Resource runtime = graph.newResource();
152                                         graph.claim(runtime, L0.InstanceOf, null, SG.Runtime);
153                                         Variable base = Variables.getVariable(graph, resource);
154
155                                         String uri = base.getURI(graph);
156                                         graph.claimLiteral(runtime, SG.Runtime_HasVariable, uri, Bindings.STRING);
157
158                                         return new ScenegraphVariable(base, doc, runtime, loader.getRoot());
159
160                                 }
161
162                         });
163                         
164                         if(context == null) return;
165
166                         String wiki = session.sync(new UniqueRead<String>() {
167
168                                 @Override
169                                 public String perform(ReadGraph graph) throws DatabaseException {
170
171                                         DocumentResource DOC = DocumentResource.getInstance(graph);
172                                         Resource doc = graph.getSingleObject(resource, DOC.HasDocumentation);
173                                         Resource scenegraph = graph.getSingleObject(doc, DOC.ScenegraphDocument_scenegraph);
174                                         
175                                         WikiDocumentNodeImpl node = loader.load(graph, scenegraph, ScenegraphLoaderUtils.getRuntime(graph, context));
176
177                                         StringBuilder b = new StringBuilder();
178                                         node.create(b, true);
179                                         
180                                         String text = b.toString();
181                                         
182                                         return SimanticsDialect.INSTANCE.apply(graph, Variables.getVariable(graph, resource), text);
183                                         
184                                 }
185
186                         });
187                         
188                         loader.dispose();
189
190                         b.append(wiki);
191                         
192                 } catch (DatabaseException e) {
193                         e.printStackTrace();
194                 }
195         }       
196
197         public void print(RequestProcessor processor, Resource res, String wiki, String css, DocumentSettings settings, final PdfWriter writer, final Document document) throws DocumentException {
198                 try {
199                         Exportable exp = processor.syncRequest(new UniqueRead<Exportable>() {
200
201                                 @Override
202                                 public Exportable perform(ReadGraph graph) throws DatabaseException {
203                                         return new Exportable(graph, res, wiki, css, settings, true);
204                                 }
205                                 
206                         });
207                         exp.export(document, writer);
208                 } catch (DocumentException e) {
209                         Logger.defaultLogError(e);
210                 } catch (DatabaseException e) {
211                         Logger.defaultLogError(e);
212                 }
213         }
214
215     public static String indexRootPath(ReadGraph graph, Variable selection) throws DatabaseException {
216         
217         Variable possibleConfiguration = Variables.getPossibleConfigurationVariable(graph, selection);
218         if(possibleConfiguration != null) selection = possibleConfiguration;
219         
220         Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(selection.getRepresents(graph)));
221         if(indexRoot == null) return "";
222
223         String selectionURI = selection.getParent(graph).getURI(graph);
224
225         SimulationResource SIMU = SimulationResource.getInstance(graph);
226         Resource configuration = graph.getPossibleObject(indexRoot, SIMU.HasConfiguration);
227         if(configuration != null) {
228                 String configurationURI = graph.getURI(configuration);
229                 if(selectionURI.startsWith(configurationURI)) {
230                         if(selectionURI.equals(configurationURI)) return "Configuration / ";
231                         return URIStringUtils.unescape(selectionURI.substring(configurationURI.length() + 1).replace("/", " / ") + " / ");
232                 }
233         }
234         
235         String rootURI = graph.getURI(indexRoot);
236         String result = selectionURI.replace(rootURI, "");
237         if(result.isEmpty()) result = "/";
238         
239         return URIStringUtils.unescape(result);
240         
241     }
242         
243 }