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.exception.DatabaseException;
32 import org.simantics.db.layer0.variable.Variable;
33 import org.simantics.db.layer0.variable.Variables;
34 import org.simantics.document.function.WikiDocumentNodeImpl;
35 import org.simantics.document.node.Composite;
36 import org.simantics.layer0.Layer0;
37 import org.simantics.scenegraph.loader.ScenegraphLoaderProcess;
38 import org.simantics.scenegraph.loader.ScenegraphLoaderUtils;
39 import org.simantics.scenegraph.loader.ScenegraphVariable;
40 import org.simantics.scenegraph.ontology.ScenegraphResources;
41 import org.simantics.simulation.ontology.SimulationResource;
42 import org.simantics.wiki.ui.SimanticsDialect;
44 import com.lowagie.text.Document;
45 import com.lowagie.text.DocumentException;
46 import com.lowagie.text.pdf.PdfWriter;
48 public class DocumentUtils {
50 static class DeepDocs extends ResourceRead<List<Resource>> {
52 protected DeepDocs(Resource resource) {
57 public List<Resource> perform(ReadGraph graph) throws DatabaseException {
59 DocumentResource DOC = DocumentResource.getInstance(graph);
60 Resource doc = graph.getPossibleObject(resource, DOC.HasDocument);
62 Layer0 L0 = Layer0.getInstance(graph);
63 Collection<Resource> children = graph.getObjects(resource, L0.ConsistsOf);
64 if(children.isEmpty()) {
65 if(doc != null) return Collections.singletonList(resource);
66 else return Collections.emptyList();
68 ArrayList<Resource> result = new ArrayList<Resource>();
69 if(doc != null) result.add(resource);
70 for(Resource child : children)
71 result.addAll(graph.syncRequest(new DeepDocs(child)));
79 public DocumentSettings getDocumentSettings(RequestProcessor processor, final Resource resource) throws DatabaseException {
81 return processor.syncRequest(new ResourceRead<DocumentSettings>(resource) {
84 public DocumentSettings perform(ReadGraph graph) throws DatabaseException {
86 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(resource));
87 if(indexRoot == null) return DocumentSettings.DEFAULT;
89 DocumentResource DOC = DocumentResource.getInstance(graph);
90 DocumentSettings result = graph.getPossibleRelatedValue(indexRoot, DOC.documentSettings, DocumentSettings.BINDING);
91 if(result == null) return DocumentSettings.DEFAULT;
100 public void getDocumentCSSText(RequestProcessor processor, final Resource resource, final StringBuilder css) throws DatabaseException {
102 css.append(processor.sync(new ResourceRead<String>(resource) {
105 public String perform(ReadGraph graph) throws DatabaseException {
107 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(resource));
108 if(indexRoot == null) return "";
110 DocumentResource DOC = DocumentResource.getInstance(graph);
111 String css = graph.getPossibleRelatedValue(indexRoot, DOC.cssDocument, Bindings.STRING);
112 if(css == null) return "";
122 public void getDocumentWikiTextRecursive(Session session, final Resource resource, final StringBuilder wiki, final StringBuilder css) throws DatabaseException {
124 List<Resource> rs = session.sync(new DeepDocs(resource));
125 for(Resource r : rs) {
126 getDocumentWikiText(session, r, wiki);
129 getDocumentCSSText(session, resource, css);
133 public void getDocumentWikiText(Session session, final Resource resource, final StringBuilder b) {
137 final ScenegraphLoaderProcess loader = new ScenegraphLoaderProcess(new Composite(), "CreatePDFAction");
139 final Variable context = session.sync(new WriteResultRequest<Variable>(session.getService(VirtualGraph.class)) {
142 public Variable perform(WriteGraph graph) throws DatabaseException {
144 DocumentResource DOC = DocumentResource.getInstance(graph);
145 Resource doc = graph.getPossibleObject(resource, DOC.HasDocument);
146 if(doc == null) return null;
148 Layer0 L0 = Layer0.getInstance(graph);
149 ScenegraphResources SG = ScenegraphResources.getInstance(graph);
150 Resource runtime = graph.newResource();
151 graph.claim(runtime, L0.InstanceOf, null, SG.Runtime);
152 Variable base = Variables.getVariable(graph, resource);
154 String uri = base.getURI(graph);
155 graph.claimLiteral(runtime, SG.Runtime_HasVariable, uri, Bindings.STRING);
157 return new ScenegraphVariable(base, doc, runtime, loader.getRoot());
163 if(context == null) return;
165 String wiki = session.sync(new UniqueRead<String>() {
168 public String perform(ReadGraph graph) throws DatabaseException {
170 DocumentResource DOC = DocumentResource.getInstance(graph);
171 Resource doc = graph.getSingleObject(resource, DOC.HasDocumentation);
172 Resource scenegraph = graph.getSingleObject(doc, DOC.ScenegraphDocument_scenegraph);
174 WikiDocumentNodeImpl node = loader.load(graph, scenegraph, ScenegraphLoaderUtils.getRuntime(graph, context));
176 StringBuilder b = new StringBuilder();
177 node.create(b, true);
179 String text = b.toString();
181 return SimanticsDialect.INSTANCE.apply(graph, Variables.getVariable(graph, resource), text);
191 } catch (DatabaseException e) {
196 public int print(RequestProcessor processor, Resource res, String wiki, String css, DocumentSettings settings, final PdfWriter writer, final Document document)
197 throws DocumentException, DatabaseException {
198 Exportable exp = processor.syncRequest(new UniqueRead<Exportable>() {
200 public Exportable perform(ReadGraph graph) throws DatabaseException {
201 return new Exportable(graph, res, wiki, css, settings, true);
204 return exp.export(document, writer);
207 public static String indexRootPath(ReadGraph graph, Variable selection) throws DatabaseException {
209 Variable possibleConfiguration = Variables.getPossibleConfigurationVariable(graph, selection);
210 if(possibleConfiguration != null) selection = possibleConfiguration;
212 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(selection.getRepresents(graph)));
213 if(indexRoot == null) return "";
215 String selectionURI = selection.getParent(graph).getURI(graph);
217 SimulationResource SIMU = SimulationResource.getInstance(graph);
218 Resource configuration = graph.getPossibleObject(indexRoot, SIMU.HasConfiguration);
219 if(configuration != null) {
220 String configurationURI = graph.getURI(configuration);
221 if(selectionURI.startsWith(configurationURI)) {
222 if(selectionURI.equals(configurationURI)) return "Configuration / ";
223 return URIStringUtils.unescape(selectionURI.substring(configurationURI.length() + 1).replace("/", " / ") + " / ");
227 String rootURI = graph.getURI(indexRoot);
228 String result = selectionURI.replace(rootURI, "");
229 if(result.isEmpty()) result = "/";
231 return URIStringUtils.unescape(result);