]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.export.core/src/org/simantics/export/core/manager/ExportSingleContent.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.export.core / src / org / simantics / export / core / manager / ExportSingleContent.java
1 package org.simantics.export.core.manager;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.Collections;
7 import java.util.List;
8
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;
20
21 /**
22  * Exports a single content file into a tmp file.
23  * 
24  * The tmp file value is written to the Content object. 
25  *
26  * @author toni.kalajainen@semantum.fi
27  */
28 public class ExportSingleContent extends ExportAction {
29         
30         public String contentUri;
31         public String contentTypeId;
32         public String formatId;
33         public Content content;
34         public File outputFile;
35         
36         public ExportSingleContent(Content content) throws ExportException 
37         {
38                 if ( content.url == null || content.formatId == null || content.label == null || content.contentTypeId == null)
39                         throw new ExportException("Invalid input parameters in Content (null)");
40                 
41                 this.content = content;
42                 this.contentUri = content.url;
43                 this.formatId = content.formatId;
44                 this.contentTypeId = content.contentTypeId;             
45         }
46         
47         public void execute(ExportContext ctx, IProgressMonitor monitor, Variant options) 
48         throws ExportException
49         {
50                 Format format = ctx.eep.getFormat( formatId );
51                 ContentType contentType = ctx.eep.getContentType( contentTypeId );
52                 Exporter[] exporters = ctx.eep.getExporters( formatId, contentTypeId );
53                 
54                 if ( exporters.length == 0 ) {
55                         throw new ExportException("No suitable exporter found for exporting "+contentType.label()+" to a "+format.label());
56                 }
57
58                 try {
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);
64                 } 
65                 
66                 Object writer = format.createFile(ctx, outputFile, options);
67                 try {
68                         for (Exporter exporter : exporters ) {
69                                 exporter.exportAction().export(
70                                                 Collections.singletonList( content ),
71                                                 writer, 
72                                                 ctx, 
73                                                 options,
74                                                 monitor,
75                                                 null);
76                         }
77                         
78                 } catch (ExportException ee) {
79                         format.closeFile( ctx, writer );
80                         writer = null;
81                         outputFile.delete();
82                         throw ee;
83                 } finally {
84                         if (writer!=null) format.closeFile( ctx, writer );
85                 }
86                 
87                 monitor.worked(10);
88         }
89
90         @Override
91         public String label(ExportContext ctx) {
92                 String label;
93                 try {
94                         label = ctx.session.syncRequest( ExportQueries.label( contentUri ) );
95                 } catch (DatabaseException e) {
96                         label = "<error: "+e.getMessage()+">";
97                 }
98                 
99                 if ( outputFile == null ) return label;
100                 return label + " export to "+outputFile.getName();
101         }
102
103         @Override
104         public int work(ExportContext ctx) {
105                 return 10;
106         }
107
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 );
112                 
113                 if ( exporters.length == 0 ) {
114                         return Collections.singletonList( "No suitable exporter found for exporting "+contentType.label()+" to a "+format.label() );
115                 }
116
117                 List<String> result = new ArrayList<String>(1);
118                 for (Exporter exporter : exporters) {
119                         try {
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() );
123                         }
124                 }
125                 
126                 return result;
127         }
128
129         @Override
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;                 
134                 }
135         }
136                 
137 }