X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.document%2Fsrc%2Forg%2Fsimantics%2Fdocument%2FDocumentUtils.java;fp=bundles%2Forg.simantics.document%2Fsrc%2Forg%2Fsimantics%2Fdocument%2FDocumentUtils.java;h=726d43a9b47acec10d2b880fc78a520b0cebf081;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.document/src/org/simantics/document/DocumentUtils.java b/bundles/org.simantics.document/src/org/simantics/document/DocumentUtils.java new file mode 100644 index 000000000..726d43a9b --- /dev/null +++ b/bundles/org.simantics.document/src/org/simantics/document/DocumentUtils.java @@ -0,0 +1,243 @@ +/******************************************************************************* + * Copyright (c) 2012 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.document; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import org.simantics.databoard.Bindings; +import org.simantics.databoard.util.URIStringUtils; +import org.simantics.db.ReadGraph; +import org.simantics.db.RequestProcessor; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.VirtualGraph; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.PossibleIndexRoot; +import org.simantics.db.common.request.ResourceRead; +import org.simantics.db.common.request.UniqueRead; +import org.simantics.db.common.request.WriteResultRequest; +import org.simantics.db.common.utils.Logger; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; +import org.simantics.document.function.WikiDocumentNodeImpl; +import org.simantics.document.node.Composite; +import org.simantics.layer0.Layer0; +import org.simantics.scenegraph.loader.ScenegraphLoaderProcess; +import org.simantics.scenegraph.loader.ScenegraphLoaderUtils; +import org.simantics.scenegraph.loader.ScenegraphVariable; +import org.simantics.scenegraph.ontology.ScenegraphResources; +import org.simantics.simulation.ontology.SimulationResource; +import org.simantics.wiki.ui.SimanticsDialect; + +import com.lowagie.text.Document; +import com.lowagie.text.DocumentException; +import com.lowagie.text.pdf.PdfWriter; + +public class DocumentUtils { + + static class DeepDocs extends ResourceRead> { + + protected DeepDocs(Resource resource) { + super(resource); + } + + @Override + public List perform(ReadGraph graph) throws DatabaseException { + + DocumentResource DOC = DocumentResource.getInstance(graph); + Resource doc = graph.getPossibleObject(resource, DOC.HasDocument); + + Layer0 L0 = Layer0.getInstance(graph); + Collection children = graph.getObjects(resource, L0.ConsistsOf); + if(children.isEmpty()) { + if(doc != null) return Collections.singletonList(resource); + else return Collections.emptyList(); + } else { + ArrayList result = new ArrayList(); + if(doc != null) result.add(resource); + for(Resource child : children) + result.addAll(graph.syncRequest(new DeepDocs(child))); + return result; + } + + } + + } + + public DocumentSettings getDocumentSettings(RequestProcessor processor, final Resource resource) throws DatabaseException { + + return processor.syncRequest(new ResourceRead(resource) { + + @Override + public DocumentSettings perform(ReadGraph graph) throws DatabaseException { + + Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(resource)); + if(indexRoot == null) return DocumentSettings.DEFAULT; + + DocumentResource DOC = DocumentResource.getInstance(graph); + DocumentSettings result = graph.getPossibleRelatedValue(indexRoot, DOC.documentSettings, DocumentSettings.BINDING); + if(result == null) return DocumentSettings.DEFAULT; + return result; + + } + + }); + + } + + public void getDocumentCSSText(RequestProcessor processor, final Resource resource, final StringBuilder css) throws DatabaseException { + + css.append(processor.sync(new ResourceRead(resource) { + + @Override + public String perform(ReadGraph graph) throws DatabaseException { + + Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(resource)); + if(indexRoot == null) return ""; + + DocumentResource DOC = DocumentResource.getInstance(graph); + String css = graph.getPossibleRelatedValue(indexRoot, DOC.cssDocument, Bindings.STRING); + if(css == null) return ""; + + return css; + + } + + })); + + } + + public void getDocumentWikiTextRecursive(Session session, final Resource resource, final StringBuilder wiki, final StringBuilder css) throws DatabaseException { + + List rs = session.sync(new DeepDocs(resource)); + for(Resource r : rs) { + getDocumentWikiText(session, r, wiki); + } + + getDocumentCSSText(session, resource, css); + + } + + public void getDocumentWikiText(Session session, final Resource resource, final StringBuilder b) { + + try { + + final ScenegraphLoaderProcess loader = new ScenegraphLoaderProcess(new Composite(), "CreatePDFAction"); + + final Variable context = session.sync(new WriteResultRequest(session.getService(VirtualGraph.class)) { + + @Override + public Variable perform(WriteGraph graph) throws DatabaseException { + + DocumentResource DOC = DocumentResource.getInstance(graph); + Resource doc = graph.getPossibleObject(resource, DOC.HasDocument); + if(doc == null) return null; + + Layer0 L0 = Layer0.getInstance(graph); + ScenegraphResources SG = ScenegraphResources.getInstance(graph); + Resource runtime = graph.newResource(); + graph.claim(runtime, L0.InstanceOf, null, SG.Runtime); + Variable base = Variables.getVariable(graph, resource); + + String uri = base.getURI(graph); + graph.claimLiteral(runtime, SG.Runtime_HasVariable, uri, Bindings.STRING); + + return new ScenegraphVariable(base, doc, runtime, loader.getRoot()); + + } + + }); + + if(context == null) return; + + String wiki = session.sync(new UniqueRead() { + + @Override + public String perform(ReadGraph graph) throws DatabaseException { + + DocumentResource DOC = DocumentResource.getInstance(graph); + Resource doc = graph.getSingleObject(resource, DOC.HasDocumentation); + Resource scenegraph = graph.getSingleObject(doc, DOC.ScenegraphDocument_scenegraph); + + WikiDocumentNodeImpl node = loader.load(graph, scenegraph, ScenegraphLoaderUtils.getRuntime(graph, context)); + + StringBuilder b = new StringBuilder(); + node.create(b, true); + + String text = b.toString(); + + return SimanticsDialect.INSTANCE.apply(graph, Variables.getVariable(graph, resource), text); + + } + + }); + + loader.dispose(); + + b.append(wiki); + + } catch (DatabaseException e) { + e.printStackTrace(); + } + } + + public void print(RequestProcessor processor, Resource res, String wiki, String css, DocumentSettings settings, final PdfWriter writer, final Document document) throws DocumentException { + try { + Exportable exp = processor.syncRequest(new UniqueRead() { + + @Override + public Exportable perform(ReadGraph graph) throws DatabaseException { + return new Exportable(graph, res, wiki, css, settings, true); + } + + }); + exp.export(document, writer); + } catch (DocumentException e) { + Logger.defaultLogError(e); + } catch (DatabaseException e) { + Logger.defaultLogError(e); + } + } + + public static String indexRootPath(ReadGraph graph, Variable selection) throws DatabaseException { + + Variable possibleConfiguration = Variables.getPossibleConfigurationVariable(graph, selection); + if(possibleConfiguration != null) selection = possibleConfiguration; + + Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(selection.getRepresents(graph))); + if(indexRoot == null) return ""; + + String selectionURI = selection.getParent(graph).getURI(graph); + + SimulationResource SIMU = SimulationResource.getInstance(graph); + Resource configuration = graph.getPossibleObject(indexRoot, SIMU.HasConfiguration); + if(configuration != null) { + String configurationURI = graph.getURI(configuration); + if(selectionURI.startsWith(configurationURI)) { + if(selectionURI.equals(configurationURI)) return "Configuration / "; + return URIStringUtils.unescape(selectionURI.substring(configurationURI.length() + 1).replace("/", " / ") + " / "); + } + } + + String rootURI = graph.getURI(indexRoot); + String result = selectionURI.replace(rootURI, ""); + if(result.isEmpty()) result = "/"; + + return URIStringUtils.unescape(result); + + } + +}