]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document/src/org/simantics/document/DocumentUtils.java
Filter also history items in resource search dialog
[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.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;
43
44 import com.lowagie.text.Document;
45 import com.lowagie.text.DocumentException;
46 import com.lowagie.text.pdf.PdfWriter;
47
48 public class DocumentUtils {
49
50         static class DeepDocs extends ResourceRead<List<Resource>> {
51
52                 protected DeepDocs(Resource resource) {
53                         super(resource);
54                 }
55
56                 @Override
57                 public List<Resource> perform(ReadGraph graph) throws DatabaseException {
58
59                         DocumentResource DOC = DocumentResource.getInstance(graph);
60                         Resource doc = graph.getPossibleObject(resource, DOC.HasDocument);
61
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();
67                         } else {
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)));
72                                 return result;
73                         }
74                         
75                 }
76          
77         }
78         
79         public DocumentSettings getDocumentSettings(RequestProcessor processor, final Resource resource) throws DatabaseException {
80
81                 return processor.syncRequest(new ResourceRead<DocumentSettings>(resource) {
82
83                         @Override
84                         public DocumentSettings perform(ReadGraph graph) throws DatabaseException {
85
86                                 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(resource));
87                                 if(indexRoot == null) return DocumentSettings.DEFAULT;
88                                 
89                                 DocumentResource DOC = DocumentResource.getInstance(graph);
90                                 DocumentSettings result = graph.getPossibleRelatedValue(indexRoot, DOC.documentSettings, DocumentSettings.BINDING);
91                                 if(result == null) return DocumentSettings.DEFAULT;
92                                 return result;
93                                 
94                         }
95                         
96                 });
97
98         }
99
100         public void getDocumentCSSText(RequestProcessor processor, final Resource resource, final StringBuilder css) throws DatabaseException {
101
102                 css.append(processor.sync(new ResourceRead<String>(resource) {
103
104                         @Override
105                         public String perform(ReadGraph graph) throws DatabaseException {
106
107                                 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(resource));
108                                 if(indexRoot == null) return "";
109                                 
110                                 DocumentResource DOC = DocumentResource.getInstance(graph);
111                                 String css = graph.getPossibleRelatedValue(indexRoot, DOC.cssDocument, Bindings.STRING);
112                                 if(css == null) return "";
113                                 
114                                 return css;
115                                 
116                         }
117                         
118                 }));
119
120         }
121
122         public void getDocumentWikiTextRecursive(Session session, final Resource resource, final StringBuilder wiki, final StringBuilder css) throws DatabaseException {
123
124                 List<Resource> rs = session.sync(new DeepDocs(resource));
125                 for(Resource r : rs) {
126                         getDocumentWikiText(session, r, wiki);
127                 }
128                 
129                 getDocumentCSSText(session, resource, css);
130                 
131         }
132
133         public void getDocumentWikiText(Session session, final Resource resource, final StringBuilder b) {
134                 
135                 try {
136
137                         final ScenegraphLoaderProcess loader = new ScenegraphLoaderProcess(new Composite(), "CreatePDFAction");
138                         
139                         final Variable context = session.sync(new WriteResultRequest<Variable>(session.getService(VirtualGraph.class)) {
140
141                                 @Override
142                                 public Variable perform(WriteGraph graph) throws DatabaseException {
143
144                                         DocumentResource DOC = DocumentResource.getInstance(graph);
145                                         Resource doc = graph.getPossibleObject(resource, DOC.HasDocument);
146                                         if(doc == null) return null;
147
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);
153
154                                         String uri = base.getURI(graph);
155                                         graph.claimLiteral(runtime, SG.Runtime_HasVariable, uri, Bindings.STRING);
156
157                                         return new ScenegraphVariable(base, doc, runtime, loader.getRoot());
158
159                                 }
160
161                         });
162                         
163                         if(context == null) return;
164
165                         String wiki = session.sync(new UniqueRead<String>() {
166
167                                 @Override
168                                 public String perform(ReadGraph graph) throws DatabaseException {
169
170                                         DocumentResource DOC = DocumentResource.getInstance(graph);
171                                         Resource doc = graph.getSingleObject(resource, DOC.HasDocumentation);
172                                         Resource scenegraph = graph.getSingleObject(doc, DOC.ScenegraphDocument_scenegraph);
173                                         
174                                         WikiDocumentNodeImpl node = loader.load(graph, scenegraph, ScenegraphLoaderUtils.getRuntime(graph, context));
175
176                                         StringBuilder b = new StringBuilder();
177                                         node.create(b, true);
178                                         
179                                         String text = b.toString();
180                                         
181                                         return SimanticsDialect.INSTANCE.apply(graph, Variables.getVariable(graph, resource), text);
182                                         
183                                 }
184
185                         });
186                         
187                         loader.dispose();
188
189                         b.append(wiki);
190                         
191                 } catch (DatabaseException e) {
192                         e.printStackTrace();
193                 }
194         }       
195
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>() {
199                         @Override
200                         public Exportable perform(ReadGraph graph) throws DatabaseException {
201                                 return new Exportable(graph, res, wiki, css, settings, true);
202                         }
203                 });
204                 return exp.export(document, writer);
205         }
206
207     public static String indexRootPath(ReadGraph graph, Variable selection) throws DatabaseException {
208         
209         Variable possibleConfiguration = Variables.getPossibleConfigurationVariable(graph, selection);
210         if(possibleConfiguration != null) selection = possibleConfiguration;
211         
212         Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(selection.getRepresents(graph)));
213         if(indexRoot == null) return "";
214
215         String selectionURI = selection.getParent(graph).getURI(graph);
216
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("/", " / ") + " / ");
224                 }
225         }
226         
227         String rootURI = graph.getURI(indexRoot);
228         String result = selectionURI.replace(rootURI, "");
229         if(result.isEmpty()) result = "/";
230         
231         return URIStringUtils.unescape(result);
232         
233     }
234         
235 }