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