1 package org.simantics.export.core.impl;
4 import java.io.IOException;
5 import java.util.ArrayList;
8 import org.eclipse.core.runtime.IProgressMonitor;
9 import org.osgi.service.prefs.Preferences;
10 import org.simantics.databoard.Accessors;
11 import org.simantics.databoard.Bindings;
12 import org.simantics.databoard.Datatypes;
13 import org.simantics.databoard.accessor.RecordAccessor;
14 import org.simantics.databoard.accessor.error.AccessorConstructionException;
15 import org.simantics.databoard.accessor.error.AccessorException;
16 import org.simantics.databoard.accessor.reference.LabelReference;
17 import org.simantics.databoard.binding.mutable.Variant;
18 import org.simantics.databoard.forms.DataboardForm;
19 import org.simantics.databoard.type.RecordType;
20 import org.simantics.export.core.ExportContext;
21 import org.simantics.export.core.error.ExportException;
22 import org.simantics.export.core.intf.PublisherClass;
23 import org.simantics.export.core.manager.Content;
24 import org.simantics.export.core.util.ExporterUtils;
25 import org.simantics.utils.FileUtils;
28 * There are two fields in this publisher:
29 * [ ] Overwrite file(s)
32 * @author toni.kalajainen@semantum.fi
34 public class DirPublisher implements PublisherClass {
36 public static RecordType RT_DIR;
38 public static LabelReference P_ALLOW_OVERWRITE = new LabelReference("Overwrite file(s)");
39 public static LabelReference P_EXPORT_LOCATION = new LabelReference("Export location");
42 RT_DIR = new RecordType();
43 RT_DIR.addComponent(P_EXPORT_LOCATION.label, DataboardForm.directoryDialog());
44 RT_DIR.addComponent(P_ALLOW_OVERWRITE.label, Datatypes.BOOLEAN);
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);
51 String path = ExporterUtils.getString( locationOptions, P_EXPORT_LOCATION );
52 if ( path == null ) throw new ExportException("Path option missing");
53 if ( canOverwrite == null ) throw new ExportException("CanOverwrite option missing");
55 for ( Content content : contents ) {
56 if ( content.tmpFile == null ) {
57 throw new ExportException("Internal error, tmpFile was null");
60 File file = new File( path, content.filename );
61 if ( file.exists() ) {
65 throw new ExportException("Would not overwrite " + file.getAbsolutePath());
69 if ( !content.tmpFile.exists() ) {
70 throw new ExportException("temporary file "+content.tmpFile.getAbsolutePath()+" did not exist?");
72 if ( !content.tmpFile.renameTo(file) ) {
73 // File.renameTo is not guaranteed to work between file systems.
74 // In that case, move by copying and deleting
76 FileUtils.copyFile(content.tmpFile, file);
77 if (!content.tmpFile.delete()) {
78 throw new IOException("Failed to delete " + content.tmpFile.getAbsolutePath() + " after copying it");
80 } catch (IOException e) {
81 throw new ExportException("Failed to move temporary file "+content.tmpFile.getAbsolutePath()+" to "+file.getAbsolutePath(), e);
84 if ( content.tmpFile.exists() ) {
85 throw new ExportException("Failed to move temporary file "+content.tmpFile.getAbsolutePath()+" to "+file.getAbsolutePath());
92 public List<String> validate(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions) throws ExportException {
94 List<String> result = new ArrayList<String>();
96 Boolean canOverwrite = ExporterUtils.getBoolean(locationOptions, P_ALLOW_OVERWRITE);
97 String pathName = ExporterUtils.getString( locationOptions, P_EXPORT_LOCATION );
98 if ( pathName == null ) { result.add("Location option missing?"); return result; }
99 if ( canOverwrite == null ) { result.add("CanOverwrite option missing?"); return result; }
100 if ( pathName.isEmpty() ) { result.add("Location must be entered."); return result; }
101 File path = new File( pathName );
103 if ( path.exists() && !path.isDirectory() ) {
104 result.add( pathName+" is not a directory.");
107 // We allow non-existing path to pass validator
108 // The location is created with createLocation
110 if ( !canOverwrite && path.exists() ) {
111 for ( Content content : contents ) {
112 File file = new File( path, content.filename );
113 if ( file.exists() ) {
114 result.add( file.getAbsolutePath()+ " already exists." );
123 public RecordType locationOptions(ExportContext ctx, List<Content> contents) throws ExportException {
128 public Variant createLocation(ExportContext ctx, Variant locationOptions) throws ExportException {
129 // Make Dirs to the path.
130 String pathName = ExporterUtils.getString( locationOptions, P_EXPORT_LOCATION );
131 if ( pathName == null ) throw new ExportException("Location option not found?");
132 File path = new File( pathName );
133 if ( path.exists() && !path.isDirectory()) throw new ExportException(pathName+" exists but is not a directory.");
134 if ( !path.mkdirs() ) throw new ExportException( "Failed to create "+pathName);
135 return locationOptions;
139 public boolean locationExists(ExportContext ctx, Variant locationOptions) throws ExportException {
141 RecordAccessor ra = Accessors.getAccessor(locationOptions);
142 String location = (String) ra.getValue(P_EXPORT_LOCATION, Bindings.STRING);
143 if ( location == null ) return false;
144 File path = new File( location );
145 return path.exists() && path.isDirectory();
146 } catch (AccessorConstructionException e) {
147 throw new ExportException( e );
148 } catch (AccessorException e) {
149 throw new ExportException( e );
154 public void fillDefaultPrefs(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions) throws ExportException {
156 RecordAccessor ra = Accessors.getAccessor(locationOptions);
157 ra.setValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN, true);
158 ra.setValue(P_EXPORT_LOCATION, Bindings.STRING, "");
159 } catch (AccessorConstructionException e) {
160 throw new ExportException(e);
161 } catch (AccessorException e) {
162 throw new ExportException(e);
167 public void savePref(Variant locationOptions, Preferences contentScopeNode, Preferences workspaceScopeNode) throws ExportException {
169 RecordAccessor ra = Accessors.getAccessor( locationOptions );
171 Boolean b = (Boolean) ra.getValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN);
173 contentScopeNode.putBoolean(P_ALLOW_OVERWRITE.tail().toString(), b);
174 workspaceScopeNode.putBoolean(P_ALLOW_OVERWRITE.tail().toString(), b);
177 String s = (String) ra.getValue(P_EXPORT_LOCATION, Bindings.STRING);
179 contentScopeNode.put(P_EXPORT_LOCATION.tail().toString(), s);
180 workspaceScopeNode.put(P_EXPORT_LOCATION.tail().toString(), s);
183 } catch (AccessorException e) {
184 throw new ExportException( e );
185 } catch (AccessorConstructionException e) {
186 throw new ExportException( e );
191 public void loadPref(Variant locationOptions, Preferences contentScopePrefs, Preferences workspaceScopePrefs) throws ExportException {
193 RecordAccessor ra = Accessors.getAccessor(locationOptions);
195 Boolean b = ExporterUtils.getPrefBoolean( contentScopePrefs, workspaceScopePrefs, P_ALLOW_OVERWRITE.tail().toString() );
196 if ( b!=null ) ra.setValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN, b);
198 String s = ExporterUtils.getPrefString( contentScopePrefs, workspaceScopePrefs, P_EXPORT_LOCATION.tail().toString() );
199 if ( s!=null ) ra.setValue(P_EXPORT_LOCATION, Bindings.STRING, s);
201 } catch (AccessorConstructionException e) {
202 throw new ExportException( e );
203 } catch (AccessorException e) {
204 throw new ExportException( e );