1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.document;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
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;
45 import com.lowagie.text.Document;
46 import com.lowagie.text.DocumentException;
47 import com.lowagie.text.pdf.PdfWriter;
49 public class DocumentUtils {
51 static class DeepDocs extends ResourceRead<List<Resource>> {
53 protected DeepDocs(Resource resource) {
58 public List<Resource> perform(ReadGraph graph) throws DatabaseException {
60 DocumentResource DOC = DocumentResource.getInstance(graph);
61 Resource doc = graph.getPossibleObject(resource, DOC.HasDocument);
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();
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)));
80 public DocumentSettings getDocumentSettings(RequestProcessor processor, final Resource resource) throws DatabaseException {
82 return processor.syncRequest(new ResourceRead<DocumentSettings>(resource) {
85 public DocumentSettings perform(ReadGraph graph) throws DatabaseException {
87 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(resource));
88 if(indexRoot == null) return DocumentSettings.DEFAULT;
90 DocumentResource DOC = DocumentResource.getInstance(graph);
91 DocumentSettings result = graph.getPossibleRelatedValue(indexRoot, DOC.documentSettings, DocumentSettings.BINDING);
92 if(result == null) return DocumentSettings.DEFAULT;
101 public void getDocumentCSSText(RequestProcessor processor, final Resource resource, final StringBuilder css) throws DatabaseException {
103 css.append(processor.sync(new ResourceRead<String>(resource) {
106 public String perform(ReadGraph graph) throws DatabaseException {
108 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(resource));
109 if(indexRoot == null) return "";
111 DocumentResource DOC = DocumentResource.getInstance(graph);
112 String css = graph.getPossibleRelatedValue(indexRoot, DOC.cssDocument, Bindings.STRING);
113 if(css == null) return "";
123 public void getDocumentWikiTextRecursive(Session session, final Resource resource, final StringBuilder wiki, final StringBuilder css) throws DatabaseException {
125 List<Resource> rs = session.sync(new DeepDocs(resource));
126 for(Resource r : rs) {
127 getDocumentWikiText(session, r, wiki);
130 getDocumentCSSText(session, resource, css);
134 public void getDocumentWikiText(Session session, final Resource resource, final StringBuilder b) {
138 final ScenegraphLoaderProcess loader = new ScenegraphLoaderProcess(new Composite(), "CreatePDFAction");
140 final Variable context = session.sync(new WriteResultRequest<Variable>(session.getService(VirtualGraph.class)) {
143 public Variable perform(WriteGraph graph) throws DatabaseException {
145 DocumentResource DOC = DocumentResource.getInstance(graph);
146 Resource doc = graph.getPossibleObject(resource, DOC.HasDocument);
147 if(doc == null) return null;
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);
155 String uri = base.getURI(graph);
156 graph.claimLiteral(runtime, SG.Runtime_HasVariable, uri, Bindings.STRING);
158 return new ScenegraphVariable(base, doc, runtime, loader.getRoot());
164 if(context == null) return;
166 String wiki = session.sync(new UniqueRead<String>() {
169 public String perform(ReadGraph graph) throws DatabaseException {
171 DocumentResource DOC = DocumentResource.getInstance(graph);
172 Resource doc = graph.getSingleObject(resource, DOC.HasDocumentation);
173 Resource scenegraph = graph.getSingleObject(doc, DOC.ScenegraphDocument_scenegraph);
175 WikiDocumentNodeImpl node = loader.load(graph, scenegraph, ScenegraphLoaderUtils.getRuntime(graph, context));
177 StringBuilder b = new StringBuilder();
178 node.create(b, true);
180 String text = b.toString();
182 return SimanticsDialect.INSTANCE.apply(graph, Variables.getVariable(graph, resource), text);
192 } catch (DatabaseException e) {
197 public void print(RequestProcessor processor, Resource res, String wiki, String css, DocumentSettings settings, final PdfWriter writer, final Document document) throws DocumentException {
199 Exportable exp = processor.syncRequest(new UniqueRead<Exportable>() {
202 public Exportable perform(ReadGraph graph) throws DatabaseException {
203 return new Exportable(graph, res, wiki, css, settings, true);
207 exp.export(document, writer);
208 } catch (DocumentException e) {
209 Logger.defaultLogError(e);
210 } catch (DatabaseException e) {
211 Logger.defaultLogError(e);
215 public static String indexRootPath(ReadGraph graph, Variable selection) throws DatabaseException {
217 Variable possibleConfiguration = Variables.getPossibleConfigurationVariable(graph, selection);
218 if(possibleConfiguration != null) selection = possibleConfiguration;
220 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(selection.getRepresents(graph)));
221 if(indexRoot == null) return "";
223 String selectionURI = selection.getParent(graph).getURI(graph);
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("/", " / ") + " / ");
235 String rootURI = graph.getURI(indexRoot);
236 String result = selectionURI.replace(rootURI, "");
237 if(result.isEmpty()) result = "/";
239 return URIStringUtils.unescape(result);