]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.export.core/src/org/simantics/export/core/impl/DirPublisher.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.export.core / src / org / simantics / export / core / impl / DirPublisher.java
1 package org.simantics.export.core.impl;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.List;
7
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;
26
27 /**
28  * There are two fields in this publisher:
29  *  [ ] Overwrite file(s)
30  *  [ ] Export Location
31  * 
32  * @author toni.kalajainen@semantum.fi
33  */
34 public class DirPublisher implements PublisherClass {
35
36         public static RecordType RT_DIR;
37
38         public static LabelReference P_ALLOW_OVERWRITE = new LabelReference("Overwrite file(s)");
39         public static LabelReference P_EXPORT_LOCATION = new LabelReference("Export location");
40         
41         static {
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);
45         }
46         
47         @Override
48         public void publish(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions, IProgressMonitor monitor) throws ExportException {
49                 
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");
54                 
55                 for ( Content content : contents ) {
56                         if ( content.tmpFile == null ) {
57                                 throw new ExportException("Internal error, tmpFile was null");
58                         }
59
60                         File file = new File( path, content.filename );
61                         if ( file.exists() ) {
62                                 if ( canOverwrite ) {
63                                         file.delete();
64                                 } else {
65                                         throw new ExportException("Would not overwrite " + file.getAbsolutePath());
66                                 }
67                         }
68                         
69                         if ( !content.tmpFile.exists() ) {
70                                 throw new ExportException("temporary file "+content.tmpFile.getAbsolutePath()+" did not exist?");
71                         }
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
75                                 try {
76                                         FileUtils.copyFile(content.tmpFile, file);
77                                         if (!content.tmpFile.delete()) {
78                                                 throw new IOException("Failed to delete " + content.tmpFile.getAbsolutePath() + " after copying it");
79                                         }
80                                 } catch (IOException e) {
81                                         throw new ExportException("Failed to move temporary file "+content.tmpFile.getAbsolutePath()+" to "+file.getAbsolutePath(), e);
82                                 }
83                         }
84                         if ( content.tmpFile.exists() ) {
85                                 throw new ExportException("Failed to move temporary file "+content.tmpFile.getAbsolutePath()+" to "+file.getAbsolutePath());
86                         }
87                 }
88                 
89         }
90
91         @Override
92         public List<String> validate(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions) throws ExportException {
93
94                 List<String> result = new ArrayList<String>();
95                 
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 );
102                 
103                 if ( path.exists() && !path.isDirectory() ) {
104                         result.add( pathName+" is not a directory.");
105                 }
106                 
107                 // We allow non-existing path to pass validator
108                 // The location is created with createLocation
109                 
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." );
115                                 }
116                         }                       
117                 }
118                 
119                 return result;
120         }
121         
122         @Override
123         public RecordType locationOptions(ExportContext ctx, List<Content> contents) throws ExportException {
124                 return RT_DIR;
125         }
126         
127         @Override
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;
136         }
137         
138          @Override
139         public boolean locationExists(ExportContext ctx, Variant locationOptions) throws ExportException {
140                 try {
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 );
150                 }               
151         }
152
153         @Override
154         public void fillDefaultPrefs(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions) throws ExportException {              
155                 try {
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);
163                 }               
164         }
165         
166         @Override
167         public void savePref(Variant locationOptions, Preferences contentScopeNode, Preferences workspaceScopeNode) throws ExportException {
168                 try {
169                         RecordAccessor ra = Accessors.getAccessor( locationOptions );
170
171                         Boolean b = (Boolean) ra.getValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN);
172                         if ( b!=null ) {
173                                 contentScopeNode.putBoolean(P_ALLOW_OVERWRITE.tail().toString(), b);
174                                 workspaceScopeNode.putBoolean(P_ALLOW_OVERWRITE.tail().toString(), b);
175                         }
176         
177                         String s = (String) ra.getValue(P_EXPORT_LOCATION, Bindings.STRING);
178                         if ( s!=null ) {
179                                 contentScopeNode.put(P_EXPORT_LOCATION.tail().toString(), s);
180                                 workspaceScopeNode.put(P_EXPORT_LOCATION.tail().toString(), s);
181                         }
182                         
183                 } catch (AccessorException e) {
184                         throw new ExportException( e );
185                 } catch (AccessorConstructionException e) {
186                         throw new ExportException( e );
187                 }
188         }
189         
190         @Override
191         public void loadPref(Variant locationOptions, Preferences contentScopePrefs, Preferences workspaceScopePrefs) throws ExportException {
192                 try {
193                         RecordAccessor ra = Accessors.getAccessor(locationOptions);
194                         
195                         Boolean b = ExporterUtils.getPrefBoolean( contentScopePrefs, workspaceScopePrefs, P_ALLOW_OVERWRITE.tail().toString() );
196                         if ( b!=null ) ra.setValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN, b);
197                         
198                         String s = ExporterUtils.getPrefString( contentScopePrefs, workspaceScopePrefs, P_EXPORT_LOCATION.tail().toString() );
199                         if ( s!=null ) ra.setValue(P_EXPORT_LOCATION, Bindings.STRING, s);
200                         
201                 } catch (AccessorConstructionException e) {
202                         throw new ExportException( e );
203                 } catch (AccessorException e) {
204                         throw new ExportException( e );
205                 }               
206         }
207
208         
209 }