1 package org.simantics.export.core.impl;
4 import java.util.ArrayList;
5 import java.util.HashMap;
8 import java.util.Map.Entry;
10 import org.eclipse.core.runtime.IProgressMonitor;
11 import org.osgi.service.prefs.Preferences;
12 import org.simantics.databoard.Accessors;
13 import org.simantics.databoard.Bindings;
14 import org.simantics.databoard.Datatypes;
15 import org.simantics.databoard.accessor.RecordAccessor;
16 import org.simantics.databoard.accessor.error.AccessorConstructionException;
17 import org.simantics.databoard.accessor.error.AccessorException;
18 import org.simantics.databoard.accessor.reference.ChildReference;
19 import org.simantics.databoard.accessor.reference.LabelReference;
20 import org.simantics.databoard.binding.mutable.Variant;
21 import org.simantics.databoard.forms.DataboardForm;
22 import org.simantics.databoard.type.Datatype;
23 import org.simantics.databoard.type.RecordType;
24 import org.simantics.databoard.type.StringType;
25 import org.simantics.export.core.ExportContext;
26 import org.simantics.export.core.error.ExportException;
27 import org.simantics.export.core.intf.Format;
28 import org.simantics.export.core.intf.PublisherClass;
29 import org.simantics.export.core.manager.Content;
30 import org.simantics.export.core.util.ExporterUtils;
31 import org.simantics.utils.datastructures.collections.CollectionUtils;
34 * There is one output field for each content:
35 * [ ] Overwrite file(s)
36 * file1.ext [________________________] (Select)
37 * file2.ext [________________________] (Select)
39 * fileN.ext [________________________] (Select)
41 * @author toni.kalajainen@semantum.fi
43 public class FilePublisher implements PublisherClass {
45 public static LabelReference P_ALLOW_OVERWRITE = new LabelReference("Overwrite file(s)");
48 public void publish(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions, IProgressMonitor monitor) throws ExportException {
50 Boolean canOverwrite = ExporterUtils.getBoolean(locationOptions, P_ALLOW_OVERWRITE);
52 for ( Content content : contents ) {
53 if ( content.tmpFile == null ) throw new ExportException("Internal error, tmpFile was null for "+content.label+content.formatExt );
55 String filePath = ExporterUtils.getString( locationOptions, new LabelReference( content.filename ) );
56 filePath = PublisherUtil.ensureEndsWith(true, content.formatExt, filePath);
57 File file = new File( filePath );
58 if ( file.exists() ) {
62 throw new ExportException("Would not overwrite " + file.getAbsolutePath());
66 content.tmpFile.renameTo(file);
71 public List<String> validate(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions) throws ExportException {
72 List<String> result = new ArrayList<String>();
74 Boolean canOverwrite = ExporterUtils.getBoolean(locationOptions, P_ALLOW_OVERWRITE);
75 if ( canOverwrite == null ) { result.add("CanOverwrite option missing?"); return result; }
77 List<String> missingPaths = new ArrayList<String>();
79 for ( Content content : contents ) {
80 String filePath = ExporterUtils.getString( locationOptions, new LabelReference( content.filename ) );
81 if ( filePath == null || filePath.isEmpty() ) {
82 missingPaths.add( content.filename );
84 filePath = PublisherUtil.ensureEndsWith(true, content.formatExt, filePath);
85 File file = new File( filePath );
86 if ( !canOverwrite && file.exists() ) {
87 result.add(file.getAbsolutePath()+" already exists.");
92 if ( !missingPaths.isEmpty() ) {
93 result.add(0, "Path for " + CollectionUtils.toString(missingPaths, ", ") + " is required.");
100 public RecordType locationOptions(ExportContext ctx, List<Content> contents) throws ExportException {
102 RecordType rt = new RecordType();
103 for ( Content content : contents ) {
104 Format format = ctx.eep.getFormat( content.formatId );
105 rt.addComponent(content.filename, DataboardForm.fileSaveDialog( format.label(), "*"+format.fileext() ) );
107 rt.addComponent(P_ALLOW_OVERWRITE.label, Datatypes.BOOLEAN);
113 public Variant createLocation(ExportContext ctx, Variant locationOptions) throws ExportException {
114 // Make Dirs to the path.
115 for ( File file : readLocations(locationOptions).values() ) {
116 File parentFile = file.getParentFile();
117 if ( parentFile == null ) continue;
118 if ( parentFile.exists() && !parentFile.isDirectory() ) throw new ExportException( parentFile+" is not directory.");
119 if ( parentFile.exists() ) continue;
120 if (!parentFile.mkdirs()) throw new ExportException( "Failed to create "+parentFile );
122 return locationOptions;
126 public boolean locationExists(ExportContext ctx, Variant locationOptions) throws ExportException {
128 boolean exists = true;
129 for ( File file : readLocations(locationOptions).values() ) {
130 File parentFile = file.getParentFile();
131 if ( parentFile == null ) continue;
132 exists &= parentFile.exists() && parentFile.isDirectory();
138 public void fillDefaultPrefs(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions) throws ExportException {
140 RecordAccessor ra = Accessors.getAccessor(locationOptions);
141 ra.setValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN, true);
143 // Fill defaults using dir location and content filename :D
144 String path = ExporterUtils.getString( options, new LabelReference("Dir", DirPublisher.P_EXPORT_LOCATION) );
145 if ( path != null ) {
146 RecordType rt = ra.type();
147 for ( int i=0; i<rt.getComponentCount(); i++ ) {
148 String label = rt.getComponent(i).name;
149 if ( label.equals(P_ALLOW_OVERWRITE.label)) continue;
150 ra.setFieldValue(i, Bindings.STRING, path+"/"+label);
154 } catch (AccessorConstructionException e) {
155 throw new ExportException(e);
156 } catch (AccessorException e) {
157 throw new ExportException(e);
162 public void savePref(Variant locationOptions, Preferences contentScopeNode, Preferences workspaceScopeNode) throws ExportException {
164 RecordAccessor ra = Accessors.getAccessor(locationOptions);
166 // File name specific
167 RecordType rt = (RecordType) locationOptions.type();
168 for (int i=0; i<rt.getComponentCount(); i++) {
169 if ( rt.getComponentType(i) instanceof StringType == false ) continue;
170 String fieldName = rt.getComponent(i).name;
171 String value = (String) ra.getFieldValue(i, Bindings.STRING);
173 workspaceScopeNode.put(fieldName, value);
174 contentScopeNode.put(fieldName, value);
178 Boolean b = (Boolean) ra.getValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN);
180 contentScopeNode.putBoolean(P_ALLOW_OVERWRITE.tail().toString(), b);
181 workspaceScopeNode.putBoolean(P_ALLOW_OVERWRITE.tail().toString(), b);
184 } catch (AccessorException e) {
185 throw new ExportException( e );
186 } catch (AccessorConstructionException e) {
187 throw new ExportException( e );
192 public void loadPref(Variant locationOptions, Preferences contentScopePrefs, Preferences workspaceScopePrefs) throws ExportException {
194 RecordAccessor ra = Accessors.getAccessor(locationOptions);
196 for ( Entry<String, File> entry : readLocations(locationOptions).entrySet() ) {
197 String key = entry.getKey();
198 String value = ExporterUtils.getPrefString(contentScopePrefs, workspaceScopePrefs, key);
199 if ( value != null ) {
200 ChildReference ref = new LabelReference( key );
201 ra.setValue( ref, Bindings.STRING, value );
205 String key = P_ALLOW_OVERWRITE.tail().toString();
206 Boolean b = ExporterUtils.getPrefBoolean(contentScopePrefs, workspaceScopePrefs, key);
207 if ( b!=null ) ra.setValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN, b);
209 } catch (AccessorConstructionException e) {
210 throw new ExportException( e );
211 } catch (AccessorException e) {
212 throw new ExportException( e );
216 Map<String, File> readLocations(Variant location) throws ExportException {
217 HashMap<String, File> result = new HashMap<String, File>();
220 RecordAccessor ra = Accessors.getAccessor(location);
221 RecordType rt = (RecordType) location.type();
222 for (int i=0; i<rt.getComponentCount(); i++)
224 Datatype ct = rt.getComponent(i).type;
225 if ( ct instanceof StringType == false ) continue;
226 String fieldName = rt.getComponent(i).name;
227 String value = (String) ra.getFieldValue(i, Bindings.STRING);
228 if ( fieldName.equals(P_ALLOW_OVERWRITE.label) ) continue;
229 File file = new File( value );
230 result.put(fieldName, file);
233 } catch (AccessorException e) {
234 throw new ExportException( e );
235 } catch (AccessorConstructionException e) {
236 throw new ExportException( e );