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