]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.export.core/src/org/simantics/export/core/impl/FilePublisher.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.export.core / src / org / simantics / export / core / impl / FilePublisher.java
1 package org.simantics.export.core.impl;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Map.Entry;
9
10 import org.eclipse.core.runtime.IProgressMonitor;
11 import org.osgi.service.prefs.Preferences;
12 import org.simantics.databoard.Accessors;
13 import org.simantics.databoard.Bindings;
14 import org.simantics.databoard.Datatypes;
15 import org.simantics.databoard.accessor.RecordAccessor;
16 import org.simantics.databoard.accessor.error.AccessorConstructionException;
17 import org.simantics.databoard.accessor.error.AccessorException;
18 import org.simantics.databoard.accessor.reference.ChildReference;
19 import org.simantics.databoard.accessor.reference.LabelReference;
20 import org.simantics.databoard.binding.mutable.Variant;
21 import org.simantics.databoard.forms.DataboardForm;
22 import org.simantics.databoard.type.Datatype;
23 import org.simantics.databoard.type.RecordType;
24 import org.simantics.databoard.type.StringType;
25 import org.simantics.export.core.ExportContext;
26 import org.simantics.export.core.error.ExportException;
27 import org.simantics.export.core.intf.Format;
28 import org.simantics.export.core.intf.PublisherClass;
29 import org.simantics.export.core.manager.Content;
30 import org.simantics.export.core.util.ExporterUtils;
31 import org.simantics.utils.datastructures.collections.CollectionUtils;
32
33 /**
34  * There is one output field for each content: 
35  *  [ ] Overwrite file(s)
36  *  file1.ext [________________________] (Select)
37  *  file2.ext [________________________] (Select)
38  *    ...
39  *  fileN.ext [________________________] (Select)
40  * 
41  * @author toni.kalajainen@semantum.fi
42  */
43 public class FilePublisher implements PublisherClass {
44
45         public static LabelReference P_ALLOW_OVERWRITE = new LabelReference("Overwrite file(s)");
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                 
52                 for ( Content content : contents ) {
53                         if ( content.tmpFile == null ) throw new ExportException("Internal error, tmpFile was null for "+content.label+content.formatExt );
54                         
55                         String filePath = ExporterUtils.getString( locationOptions, new LabelReference( content.filename ) );
56                         filePath = PublisherUtil.ensureEndsWith(true, content.formatExt, filePath);
57                         File file = new File( filePath );
58                         if ( file.exists() ) {
59                                 if ( canOverwrite ) {
60                                         file.delete();
61                                 } else {
62                                         throw new ExportException("Would not overwrite " + file.getAbsolutePath());
63                                 }
64                         }
65                         
66                         content.tmpFile.renameTo(file);
67                 }
68         }
69
70         @Override
71         public List<String> validate(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions) throws ExportException {
72                 List<String> result = new ArrayList<String>();
73
74                 Boolean canOverwrite = ExporterUtils.getBoolean(locationOptions, P_ALLOW_OVERWRITE);
75                 if ( canOverwrite == null ) { result.add("CanOverwrite option missing?"); return result; }
76
77                 List<String> missingPaths = new ArrayList<String>();
78                 
79                 for ( Content content : contents ) {
80                         String filePath = ExporterUtils.getString( locationOptions, new LabelReference( content.filename ) );
81                         if ( filePath == null || filePath.isEmpty() ) {
82                                 missingPaths.add( content.filename );
83                         } else {
84                                 filePath = PublisherUtil.ensureEndsWith(true, content.formatExt, filePath);
85                                 File file = new File( filePath );
86                                 if ( !canOverwrite && file.exists() ) {
87                                         result.add(file.getAbsolutePath()+" already exists.");
88                                 }
89                         }
90                 }
91
92                 if ( !missingPaths.isEmpty() ) {
93                         result.add(0, "Path for " + CollectionUtils.toString(missingPaths, ", ") + " is required.");                    
94                 }
95                 
96                 return result;
97         }
98
99         @Override
100         public RecordType locationOptions(ExportContext ctx, List<Content> contents) throws ExportException {
101                 
102                 RecordType rt = new RecordType();                       
103                 for ( Content content : contents ) {
104                         Format format = ctx.eep.getFormat( content.formatId );                  
105                         rt.addComponent(content.filename, DataboardForm.fileSaveDialog( format.label(), "*"+format.fileext() ) );
106                 }
107                 rt.addComponent(P_ALLOW_OVERWRITE.label, Datatypes.BOOLEAN);
108                 
109                 return rt;
110         }
111
112         @Override
113         public Variant createLocation(ExportContext ctx, Variant locationOptions) throws ExportException {
114                 // Make Dirs to the path.
115                 for ( File file : readLocations(locationOptions).values() ) {
116                         File parentFile = file.getParentFile();
117                         if ( parentFile == null ) continue;
118                         if ( parentFile.exists() && !parentFile.isDirectory() ) throw new ExportException( parentFile+" is not directory.");
119                         if ( parentFile.exists() ) continue;
120                         if (!parentFile.mkdirs()) throw new ExportException( "Failed to create "+parentFile );
121                 }
122                 return locationOptions;
123         }
124         
125          @Override
126         public boolean locationExists(ExportContext ctx, Variant locationOptions) throws ExportException {
127                  
128                 boolean exists = true;
129                 for ( File file : readLocations(locationOptions).values() ) {
130                         File parentFile = file.getParentFile();
131                         if ( parentFile == null ) continue;                     
132                         exists &= parentFile.exists() && parentFile.isDirectory(); 
133                 }
134                 return exists;
135         }
136         
137         @Override
138         public void fillDefaultPrefs(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions) throws ExportException {
139                 try {
140                         RecordAccessor ra = Accessors.getAccessor(locationOptions);
141                 ra.setValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN, true);
142                 
143                 // Fill defaults using dir location and content filename :D
144                         String path = ExporterUtils.getString( options, new LabelReference("Dir", DirPublisher.P_EXPORT_LOCATION) );
145                 if ( path != null ) {
146                         RecordType rt = ra.type();
147                         for ( int i=0; i<rt.getComponentCount(); i++ ) {
148                                 String label = rt.getComponent(i).name;
149                                 if ( label.equals(P_ALLOW_OVERWRITE.label)) continue;
150                                 ra.setFieldValue(i, Bindings.STRING, path+"/"+label);
151                         }
152                 }
153                 
154                 } catch (AccessorConstructionException e) {
155                         throw new ExportException(e);
156                 } catch (AccessorException e) {
157                         throw new ExportException(e);
158                 }               
159         }
160                 
161         @Override
162         public void savePref(Variant locationOptions, Preferences contentScopeNode, Preferences workspaceScopeNode) throws ExportException {
163                 try {
164                         RecordAccessor ra = Accessors.getAccessor(locationOptions);
165
166                         // File name specific                   
167                         RecordType rt = (RecordType) locationOptions.type();
168                         for (int i=0; i<rt.getComponentCount(); i++) {
169                                 if ( rt.getComponentType(i) instanceof StringType == false ) continue;
170                                 String fieldName = rt.getComponent(i).name;
171                                 String value = (String) ra.getFieldValue(i, Bindings.STRING);                           
172                                 if ( value!=null ) {
173                                         workspaceScopeNode.put(fieldName, value);
174                                         contentScopeNode.put(fieldName, value);
175                                 }
176                         }
177                         
178                         Boolean b = (Boolean) ra.getValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN);
179                         if ( b!=null ) {
180                                 contentScopeNode.putBoolean(P_ALLOW_OVERWRITE.tail().toString(), b);
181                                 workspaceScopeNode.putBoolean(P_ALLOW_OVERWRITE.tail().toString(), b);
182                         }
183                         
184                 } catch (AccessorException e) {
185                         throw new ExportException( e );
186                 } catch (AccessorConstructionException e) {
187                         throw new ExportException( e );
188                 }
189         }
190         
191         @Override
192         public void loadPref(Variant locationOptions, Preferences contentScopePrefs, Preferences workspaceScopePrefs) throws ExportException {
193                 try {
194                         RecordAccessor ra = Accessors.getAccessor(locationOptions);
195         
196                         for ( Entry<String, File> entry : readLocations(locationOptions).entrySet() ) {
197                                 String key = entry.getKey();
198                                 String value = ExporterUtils.getPrefString(contentScopePrefs, workspaceScopePrefs, key);
199                                 if ( value != null ) {
200                                         ChildReference ref = new LabelReference( key ); 
201                                         ra.setValue( ref, Bindings.STRING, value );                                     
202                                 }
203                         }
204                         
205                         String key = P_ALLOW_OVERWRITE.tail().toString();
206                         Boolean b = ExporterUtils.getPrefBoolean(contentScopePrefs, workspaceScopePrefs, key);
207                         if ( b!=null ) ra.setValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN, b);
208                         
209                 } catch (AccessorConstructionException e) {
210                         throw new ExportException( e );
211                 } catch (AccessorException e) {
212                         throw new ExportException( e );
213                 }               
214         }
215
216         Map<String, File> readLocations(Variant location) throws ExportException {
217                 HashMap<String, File> result = new HashMap<String, File>();
218                 
219                 try {
220                         RecordAccessor ra = Accessors.getAccessor(location);
221                         RecordType rt = (RecordType) location.type();
222                         for (int i=0; i<rt.getComponentCount(); i++) 
223                         {
224                                 Datatype ct = rt.getComponent(i).type;
225                                 if ( ct instanceof StringType == false ) continue;
226                                 String fieldName = rt.getComponent(i).name;
227                                 String value = (String) ra.getFieldValue(i, Bindings.STRING);
228                                 if ( fieldName.equals(P_ALLOW_OVERWRITE.label) )  continue;
229                                 File file = new File( value );
230                                 result.put(fieldName, file);
231                         }
232                         
233                 } catch (AccessorException e) {
234                         throw new ExportException( e );
235                 } catch (AccessorConstructionException e) {
236                         throw new ExportException( e );
237                 }
238                 
239                 return result;
240         }
241         
242         
243 }