1 package org.simantics.export.core.manager;
\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
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
22 * Exports a single content file into a tmp file.
\r
24 * The tmp file value is written to the Content object.
\r
26 * @author toni.kalajainen@semantum.fi
\r
28 public class ExportSingleContent extends ExportAction {
\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
36 public ExportSingleContent(Content content) throws ExportException
\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
41 this.content = content;
\r
42 this.contentUri = content.url;
\r
43 this.formatId = content.formatId;
\r
44 this.contentTypeId = content.contentTypeId;
\r
47 public void execute(ExportContext ctx, IProgressMonitor monitor, Variant options)
\r
48 throws ExportException
\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
54 if ( exporters.length == 0 ) {
\r
55 throw new ExportException("No suitable exporter found for exporting "+contentType.label()+" to a "+format.label());
\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
66 Object writer = format.createFile(ctx, outputFile, options);
\r
68 for (Exporter exporter : exporters ) {
\r
69 exporter.exportAction().export(
\r
70 Collections.singletonList( content ),
\r
78 } catch (ExportException ee) {
\r
79 format.closeFile( ctx, writer );
\r
81 outputFile.delete();
\r
84 if (writer!=null) format.closeFile( ctx, writer );
\r
91 public String label(ExportContext ctx) {
\r
94 label = ctx.session.syncRequest( ExportQueries.label( contentUri ) );
\r
95 } catch (DatabaseException e) {
\r
96 label = "<error: "+e.getMessage()+">";
\r
99 if ( outputFile == null ) return label;
\r
100 return label + " export to "+outputFile.getName();
\r
104 public int work(ExportContext ctx) {
\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
113 if ( exporters.length == 0 ) {
\r
114 return Collections.singletonList( "No suitable exporter found for exporting "+contentType.label()+" to a "+format.label() );
\r
117 List<String> result = new ArrayList<String>(1);
\r
118 for (Exporter exporter : exporters) {
\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
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