]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 package org.simantics.export.core.manager;\r
2 \r
3 import java.io.File;\r
4 import java.io.IOException;\r
5 import java.util.ArrayList;\r
6 import java.util.Collections;\r
7 import java.util.List;\r
8 \r
9 import org.eclipse.core.runtime.IProgressMonitor;\r
10 import org.simantics.Simantics;\r
11 import org.simantics.databoard.binding.mutable.Variant;\r
12 import org.simantics.databoard.util.URIUtil;\r
13 import org.simantics.db.exception.DatabaseException;\r
14 import org.simantics.export.core.ExportContext;\r
15 import org.simantics.export.core.error.ExportException;\r
16 import org.simantics.export.core.intf.ContentType;\r
17 import org.simantics.export.core.intf.Exporter;\r
18 import org.simantics.export.core.intf.Format;\r
19 import org.simantics.export.core.util.ExportQueries;\r
20 \r
21 /**\r
22  * Exports a single content file into a tmp file.\r
23  * \r
24  * The tmp file value is written to the Content object. \r
25  *\r
26  * @author toni.kalajainen@semantum.fi\r
27  */\r
28 public class ExportSingleContent extends ExportAction {\r
29         \r
30         public String contentUri;\r
31         public String contentTypeId;\r
32         public String formatId;\r
33         public Content content;\r
34         public File outputFile;\r
35         \r
36         public ExportSingleContent(Content content) throws ExportException \r
37         {\r
38                 if ( content.url == null || content.formatId == null || content.label == null || content.contentTypeId == null)\r
39                         throw new ExportException("Invalid input parameters in Content (null)");\r
40                 \r
41                 this.content = content;\r
42                 this.contentUri = content.url;\r
43                 this.formatId = content.formatId;\r
44                 this.contentTypeId = content.contentTypeId;             \r
45         }\r
46         \r
47         public void execute(ExportContext ctx, IProgressMonitor monitor, Variant options) \r
48         throws ExportException\r
49         {\r
50                 Format format = ctx.eep.getFormat( formatId );\r
51                 ContentType contentType = ctx.eep.getContentType( contentTypeId );\r
52                 Exporter[] exporters = ctx.eep.getExporters( formatId, contentTypeId );\r
53                 \r
54                 if ( exporters.length == 0 ) {\r
55                         throw new ExportException("No suitable exporter found for exporting "+contentType.label()+" to a "+format.label());\r
56                 }\r
57 \r
58                 try {\r
59                         // Prefix must be at least 3 characters in length\r
60                         String prefix = "___" + URIUtil.encodeFilename(content.label);\r
61                         content.tmpFile = outputFile = File.createTempFile( prefix, URIUtil.encodeFilename(format.fileext()), Simantics.getTemporaryDirectory("export.core") );\r
62                 } catch (IOException e) {\r
63                         throw new ExportException(e);\r
64                 } \r
65                 \r
66                 Object writer = format.createFile(ctx, outputFile, options);\r
67                 try {\r
68                         for (Exporter exporter : exporters ) {\r
69                                 exporter.exportAction().export(\r
70                                                 Collections.singletonList( content ),\r
71                                                 writer, \r
72                                                 ctx, \r
73                                                 options,\r
74                                                 monitor,\r
75                                                 null);\r
76                         }\r
77                         \r
78                 } catch (ExportException ee) {\r
79                         format.closeFile( ctx, writer );\r
80                         writer = null;\r
81                         outputFile.delete();\r
82                         throw ee;\r
83                 } finally {\r
84                         if (writer!=null) format.closeFile( ctx, writer );\r
85                 }\r
86                 \r
87                 monitor.worked(10);\r
88         }\r
89 \r
90         @Override\r
91         public String label(ExportContext ctx) {\r
92                 String label;\r
93                 try {\r
94                         label = ctx.session.syncRequest( ExportQueries.label( contentUri ) );\r
95                 } catch (DatabaseException e) {\r
96                         label = "<error: "+e.getMessage()+">";\r
97                 }\r
98                 \r
99                 if ( outputFile == null ) return label;\r
100                 return label + " export to "+outputFile.getName();\r
101         }\r
102 \r
103         @Override\r
104         public int work(ExportContext ctx) {\r
105                 return 10;\r
106         }\r
107 \r
108         public List<String> validate(ExportContext context, Variant options) {\r
109                 Format format = context.eep.getFormat( formatId );\r
110                 ContentType contentType = context.eep.getContentType( contentTypeId );\r
111                 Exporter[] exporters = context.eep.getExporters( formatId, contentTypeId );\r
112                 \r
113                 if ( exporters.length == 0 ) {\r
114                         return Collections.singletonList( "No suitable exporter found for exporting "+contentType.label()+" to a "+format.label() );\r
115                 }\r
116 \r
117                 List<String> result = new ArrayList<String>(1);\r
118                 for (Exporter exporter : exporters) {\r
119                         try {\r
120                                 result.addAll( exporter.exportAction().validate(contentUri, context, options) ) ;\r
121                         } catch (ExportException e) {\r
122                                 result.add( "Could not create export action for the "+exporter.formatId()+" exporter. "+e.getMessage() );\r
123                         }\r
124                 }\r
125                 \r
126                 return result;\r
127         }\r
128 \r
129         @Override\r
130         public void cleanup(ExportContext ctx, IProgressMonitor progress, Variant options) throws ExportException {\r
131                 if ( this.outputFile != null ) { \r
132                         this.outputFile.delete();\r
133                         content.tmpFile = null;                 \r
134                 }\r
135         }\r
136                 \r
137 }\r