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