]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.export.core/src/org/simantics/export/core/impl/FilePublisher.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.export.core / src / org / simantics / export / core / impl / FilePublisher.java
1 package org.simantics.export.core.impl;\r
2 \r
3 import java.io.File;\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
9 \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
32 \r
33 /**\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
38  *    ...\r
39  *  fileN.ext [________________________] (Select)\r
40  * \r
41  * @author toni.kalajainen@semantum.fi\r
42  */\r
43 public class FilePublisher implements PublisherClass {\r
44 \r
45         public static LabelReference P_ALLOW_OVERWRITE = new LabelReference("Overwrite file(s)");\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                 \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
54                         \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
60                                         file.delete();\r
61                                 } else {\r
62                                         throw new ExportException("Would not overwrite " + file.getAbsolutePath());\r
63                                 }\r
64                         }\r
65                         \r
66                         content.tmpFile.renameTo(file);\r
67                 }\r
68         }\r
69 \r
70         @Override\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
73 \r
74                 Boolean canOverwrite = ExporterUtils.getBoolean(locationOptions, P_ALLOW_OVERWRITE);\r
75                 if ( canOverwrite == null ) { result.add("CanOverwrite option missing?"); return result; }\r
76 \r
77                 List<String> missingPaths = new ArrayList<String>();\r
78                 \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
83                         } else {\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
88                                 }\r
89                         }\r
90                 }\r
91 \r
92                 if ( !missingPaths.isEmpty() ) {\r
93                         result.add(0, "Path for " + CollectionUtils.toString(missingPaths, ", ") + " is required.");                    \r
94                 }\r
95                 \r
96                 return result;\r
97         }\r
98 \r
99         @Override\r
100         public RecordType locationOptions(ExportContext ctx, List<Content> contents) throws ExportException {\r
101                 \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
106                 }\r
107                 rt.addComponent(P_ALLOW_OVERWRITE.label, Datatypes.BOOLEAN);\r
108                 \r
109                 return rt;\r
110         }\r
111 \r
112         @Override\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
121                 }\r
122                 return locationOptions;\r
123         }\r
124         \r
125          @Override\r
126         public boolean locationExists(ExportContext ctx, Variant locationOptions) throws ExportException {\r
127                  \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
133                 }\r
134                 return exists;\r
135         }\r
136         \r
137         @Override\r
138         public void fillDefaultPrefs(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions) throws ExportException {\r
139                 try {\r
140                         RecordAccessor ra = Accessors.getAccessor(locationOptions);\r
141                 ra.setValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN, true);\r
142                 \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
151                         }\r
152                 }\r
153                 \r
154                 } catch (AccessorConstructionException e) {\r
155                         throw new ExportException(e);\r
156                 } catch (AccessorException e) {\r
157                         throw new ExportException(e);\r
158                 }               \r
159         }\r
160                 \r
161         @Override\r
162         public void savePref(Variant locationOptions, Preferences contentScopeNode, Preferences workspaceScopeNode) throws ExportException {\r
163                 try {\r
164                         RecordAccessor ra = Accessors.getAccessor(locationOptions);\r
165 \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
175                                 }\r
176                         }\r
177                         \r
178                         Boolean b = (Boolean) ra.getValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN);\r
179                         if ( b!=null ) {\r
180                                 contentScopeNode.putBoolean(P_ALLOW_OVERWRITE.tail().toString(), b);\r
181                                 workspaceScopeNode.putBoolean(P_ALLOW_OVERWRITE.tail().toString(), b);\r
182                         }\r
183                         \r
184                 } catch (AccessorException e) {\r
185                         throw new ExportException( e );\r
186                 } catch (AccessorConstructionException e) {\r
187                         throw new ExportException( e );\r
188                 }\r
189         }\r
190         \r
191         @Override\r
192         public void loadPref(Variant locationOptions, Preferences contentScopePrefs, Preferences workspaceScopePrefs) throws ExportException {\r
193                 try {\r
194                         RecordAccessor ra = Accessors.getAccessor(locationOptions);\r
195         \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
202                                 }\r
203                         }\r
204                         \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
208                         \r
209                 } catch (AccessorConstructionException e) {\r
210                         throw new ExportException( e );\r
211                 } catch (AccessorException e) {\r
212                         throw new ExportException( e );\r
213                 }               \r
214         }\r
215 \r
216         Map<String, File> readLocations(Variant location) throws ExportException {\r
217                 HashMap<String, File> result = new HashMap<String, File>();\r
218                 \r
219                 try {\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
223                         {\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
231                         }\r
232                         \r
233                 } catch (AccessorException e) {\r
234                         throw new ExportException( e );\r
235                 } catch (AccessorConstructionException e) {\r
236                         throw new ExportException( e );\r
237                 }\r
238                 \r
239                 return result;\r
240         }\r
241         \r
242         \r
243 }\r