1 package org.simantics.export.core.manager;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.Collections;
9 import org.eclipse.core.runtime.IProgressMonitor;
10 import org.simantics.Simantics;
11 import org.simantics.databoard.binding.mutable.Variant;
12 import org.simantics.databoard.util.URIUtil;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.export.core.ExportContext;
15 import org.simantics.export.core.error.ExportException;
16 import org.simantics.export.core.intf.ContentType;
17 import org.simantics.export.core.intf.Exporter;
18 import org.simantics.export.core.intf.Format;
19 import org.simantics.export.core.util.ExportQueries;
22 * Exports a single content file into a tmp file.
24 * The tmp file value is written to the Content object.
26 * @author toni.kalajainen@semantum.fi
28 public class ExportSingleContent extends ExportAction {
30 public String contentUri;
31 public String contentTypeId;
32 public String formatId;
33 public Content content;
34 public File outputFile;
36 public ExportSingleContent(Content content) throws ExportException
38 if ( content.url == null || content.formatId == null || content.label == null || content.contentTypeId == null)
39 throw new ExportException("Invalid input parameters in Content (null)");
41 this.content = content;
42 this.contentUri = content.url;
43 this.formatId = content.formatId;
44 this.contentTypeId = content.contentTypeId;
47 public void execute(ExportContext ctx, IProgressMonitor monitor, Variant options)
48 throws ExportException
50 Format format = ctx.eep.getFormat( formatId );
51 ContentType contentType = ctx.eep.getContentType( contentTypeId );
52 Exporter[] exporters = ctx.eep.getExporters( formatId, contentTypeId );
54 if ( exporters.length == 0 ) {
55 throw new ExportException("No suitable exporter found for exporting "+contentType.label()+" to a "+format.label());
59 // Prefix must be at least 3 characters in length
60 String prefix = "___" + URIUtil.encodeFilename(content.label);
61 content.tmpFile = outputFile = File.createTempFile( prefix, URIUtil.encodeFilename(format.fileext()), Simantics.getTemporaryDirectory("export.core") );
62 } catch (IOException e) {
63 throw new ExportException(e);
66 Object writer = format.createFile(ctx, outputFile, options);
68 for (Exporter exporter : exporters ) {
69 exporter.exportAction().export(
70 Collections.singletonList( content ),
78 } catch (ExportException ee) {
79 format.closeFile( ctx, writer );
84 if (writer!=null) format.closeFile( ctx, writer );
91 public String label(ExportContext ctx) {
94 label = ctx.session.syncRequest( ExportQueries.label( contentUri ) );
95 } catch (DatabaseException e) {
96 label = "<error: "+e.getMessage()+">";
99 if ( outputFile == null ) return label;
100 return label + " export to "+outputFile.getName();
104 public int work(ExportContext ctx) {
108 public List<String> validate(ExportContext context, Variant options) {
109 Format format = context.eep.getFormat( formatId );
110 ContentType contentType = context.eep.getContentType( contentTypeId );
111 Exporter[] exporters = context.eep.getExporters( formatId, contentTypeId );
113 if ( exporters.length == 0 ) {
114 return Collections.singletonList( "No suitable exporter found for exporting "+contentType.label()+" to a "+format.label() );
117 List<String> result = new ArrayList<String>(1);
118 for (Exporter exporter : exporters) {
120 result.addAll( exporter.exportAction().validate(contentUri, context, options) ) ;
121 } catch (ExportException e) {
122 result.add( "Could not create export action for the "+exporter.formatId()+" exporter. "+e.getMessage() );
130 public void cleanup(ExportContext ctx, IProgressMonitor progress, Variant options) throws ExportException {
131 if ( this.outputFile != null ) {
132 this.outputFile.delete();
133 content.tmpFile = null;