]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.export.core/src/org/simantics/export/core/manager/ExportSingleContent.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.export.core / src / org / simantics / export / core / manager / ExportSingleContent.java
diff --git a/bundles/org.simantics.export.core/src/org/simantics/export/core/manager/ExportSingleContent.java b/bundles/org.simantics.export.core/src/org/simantics/export/core/manager/ExportSingleContent.java
new file mode 100644 (file)
index 0000000..c1364d1
--- /dev/null
@@ -0,0 +1,137 @@
+package org.simantics.export.core.manager;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+import java.util.Collections;\r
+import java.util.List;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.simantics.Simantics;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.databoard.util.URIUtil;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.export.core.ExportContext;\r
+import org.simantics.export.core.error.ExportException;\r
+import org.simantics.export.core.intf.ContentType;\r
+import org.simantics.export.core.intf.Exporter;\r
+import org.simantics.export.core.intf.Format;\r
+import org.simantics.export.core.util.ExportQueries;\r
+\r
+/**\r
+ * Exports a single content file into a tmp file.\r
+ * \r
+ * The tmp file value is written to the Content object. \r
+ *\r
+ * @author toni.kalajainen@semantum.fi\r
+ */\r
+public class ExportSingleContent extends ExportAction {\r
+       \r
+       public String contentUri;\r
+       public String contentTypeId;\r
+       public String formatId;\r
+       public Content content;\r
+       public File outputFile;\r
+       \r
+       public ExportSingleContent(Content content) throws ExportException \r
+       {\r
+               if ( content.url == null || content.formatId == null || content.label == null || content.contentTypeId == null)\r
+                       throw new ExportException("Invalid input parameters in Content (null)");\r
+               \r
+               this.content = content;\r
+               this.contentUri = content.url;\r
+               this.formatId = content.formatId;\r
+               this.contentTypeId = content.contentTypeId;             \r
+       }\r
+       \r
+       public void execute(ExportContext ctx, IProgressMonitor monitor, Variant options) \r
+       throws ExportException\r
+       {\r
+               Format format = ctx.eep.getFormat( formatId );\r
+               ContentType contentType = ctx.eep.getContentType( contentTypeId );\r
+               Exporter[] exporters = ctx.eep.getExporters( formatId, contentTypeId );\r
+               \r
+               if ( exporters.length == 0 ) {\r
+                       throw new ExportException("No suitable exporter found for exporting "+contentType.label()+" to a "+format.label());\r
+               }\r
+\r
+               try {\r
+                       // Prefix must be at least 3 characters in length\r
+                       String prefix = "___" + URIUtil.encodeFilename(content.label);\r
+                       content.tmpFile = outputFile = File.createTempFile( prefix, URIUtil.encodeFilename(format.fileext()), Simantics.getTemporaryDirectory("export.core") );\r
+               } catch (IOException e) {\r
+                       throw new ExportException(e);\r
+               } \r
+               \r
+               Object writer = format.createFile(ctx, outputFile, options);\r
+               try {\r
+                       for (Exporter exporter : exporters ) {\r
+                               exporter.exportAction().export(\r
+                                               Collections.singletonList( content ),\r
+                                               writer, \r
+                                               ctx, \r
+                                               options,\r
+                                               monitor,\r
+                                               null);\r
+                       }\r
+                       \r
+               } catch (ExportException ee) {\r
+                       format.closeFile( ctx, writer );\r
+                       writer = null;\r
+                       outputFile.delete();\r
+                       throw ee;\r
+               } finally {\r
+                       if (writer!=null) format.closeFile( ctx, writer );\r
+               }\r
+               \r
+               monitor.worked(10);\r
+       }\r
+\r
+       @Override\r
+       public String label(ExportContext ctx) {\r
+               String label;\r
+               try {\r
+                       label = ctx.session.syncRequest( ExportQueries.label( contentUri ) );\r
+               } catch (DatabaseException e) {\r
+                       label = "<error: "+e.getMessage()+">";\r
+               }\r
+               \r
+               if ( outputFile == null ) return label;\r
+               return label + " export to "+outputFile.getName();\r
+       }\r
+\r
+       @Override\r
+       public int work(ExportContext ctx) {\r
+               return 10;\r
+       }\r
+\r
+       public List<String> validate(ExportContext context, Variant options) {\r
+               Format format = context.eep.getFormat( formatId );\r
+               ContentType contentType = context.eep.getContentType( contentTypeId );\r
+               Exporter[] exporters = context.eep.getExporters( formatId, contentTypeId );\r
+               \r
+               if ( exporters.length == 0 ) {\r
+                       return Collections.singletonList( "No suitable exporter found for exporting "+contentType.label()+" to a "+format.label() );\r
+               }\r
+\r
+               List<String> result = new ArrayList<String>(1);\r
+               for (Exporter exporter : exporters) {\r
+                       try {\r
+                               result.addAll( exporter.exportAction().validate(contentUri, context, options) ) ;\r
+                       } catch (ExportException e) {\r
+                               result.add( "Could not create export action for the "+exporter.formatId()+" exporter. "+e.getMessage() );\r
+                       }\r
+               }\r
+               \r
+               return result;\r
+       }\r
+\r
+       @Override\r
+       public void cleanup(ExportContext ctx, IProgressMonitor progress, Variant options) throws ExportException {\r
+               if ( this.outputFile != null ) { \r
+                       this.outputFile.delete();\r
+                       content.tmpFile = null;                 \r
+               }\r
+       }\r
+               \r
+}\r