Salome HOME
4b9432bb8cb311bddbb00a3d3e873f3a85383e42
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / UploadAction.java
1 package org.splat.simer;
2
3 import java.io.File;
4
5 import org.splat.kernel.Do;
6 import org.splat.service.technical.RepositoryService;
7 import org.splat.wapp.Constants;
8
9 /**
10  * Action for uploading a file into the user's download directory.
11  */
12 public class UploadAction extends Action {
13
14         /**
15          * Serialization version id.
16          */
17         private static final long serialVersionUID = 6003880772275115923L;
18
19         /**
20          * Uploaded file.
21          */
22         private transient File _upload = null;
23         /**
24          * Mime type of the uploaded file.
25          */
26         private transient String _uploadMimeType = null;
27         /**
28          * Uploaded file name.
29          */
30         private transient String _uploadFileName = null;
31
32         /**
33          * Action to do.
34          */
35         private transient ToDo _action;
36         /**
37          * Action to which the uploaded file is passed.
38          */
39         private String _nextAction = null;
40         /**
41          * Depending on the next action, document index to which the action applies.
42          */
43         private String _index = null;
44         /**
45          * Injected repository service.
46          */
47         private RepositoryService _repositoryService;
48
49         /**
50          * Value of the menu property. It can be: none, create, open, study, knowledge, sysadmin, help.
51          */
52         private String _menuProperty;
53
54         /**
55          * Value of the title bar property. It can be: study, knowledge.
56          */
57         private String _titleProperty;
58
59         /**
60          * Value of the tool bar property. It can be: none, standard, study, back.
61          */
62         private String _toolProperty;
63
64         /**
65          * Value of the left menu property. It can be: open, study, knowledge, scenario.
66          */
67         private String _leftMenuProperty;
68
69         /**
70          * Property that indicates whether the current open study is editable or not. On the screen it looks like pen on the status icon, pop-up
71          * menu also can be called. It is necessary for correct building the title bar.
72          */
73         private String _editDisabledProperty = "false";
74
75         /**
76          * Action modes enumeration.
77          */
78         private enum ToDo {
79                 /**
80                  * Cancel the operation.
81                  */
82                 cancel,
83                 /**
84                  * Perform uploading.
85                  */
86                 upload
87         };
88
89         // ==============================================================================================================================
90         // Action methods
91         // ==============================================================================================================================
92
93         /**
94          * Prepare form for the upload action.
95          * 
96          * @return SUCCESS
97          */
98         public String doInitialize() {
99                 setMenuProperty(Constants.STUDY_MENU);
100                 setToolProperty(Constants.NONE);
101                 setLeftMenuProperty("open");
102                 initializationFullScreenContext(_menuProperty, _toolProperty,
103                                 _leftMenuProperty);
104
105                 return SUCCESS;
106         }
107
108         /**
109          * Store uploaded file into the user's download directory.
110          * 
111          * @return next action if ok, "cancel" if the operation is cancelled by user, "outofmemory" if there is no enough memory to upload the
112          *         file or ERROR otherwise
113          */
114         public String doUpload() {
115                 setMenuProperty(Constants.STUDY_MENU);
116                 setTitleProperty(Constants.STUDY_MENU);
117                 setEditDisabledProperty("true");
118                 initializationScreenContext(_menuProperty, _titleProperty,
119                                 _editDisabledProperty);
120
121                 String res;
122                 if (_action == ToDo.cancel) {
123                         res = "cancel";
124                 } else {
125                         try {
126                                 File udir = getRepositoryService().getDownloadDirectory(
127                                                 getConnectedUser());
128                                 String path = udir.getPath() + "/" + _uploadFileName;
129                                 File file = new File(path);
130
131                                 if (!udir.exists()) {
132                                         udir.mkdir();
133                                 }
134                                 if (file.exists()) {
135                                         file.delete();
136                                 }
137                                 Do.copy(_upload, file);
138                                 LOG.info("Uploading \"" + _uploadFileName + "\" "
139                                                 + _uploadMimeType + " file.");
140                                 /*
141                                  * if (next == null || next.isEmpty()) { next = "import"; }
142                                  */
143
144                                 res = _nextAction;
145                         } catch (OutOfMemoryError error) {
146
147                                 setMenuProperty(Constants.NONE);
148                                 setTitleProperty(Constants.STUDY_MENU);
149                                 setEditDisabledProperty("true");
150                                 setToolProperty(Constants.NONE);
151                                 setLeftMenuProperty(Constants.STUDY_MENU);
152                                 initializationFullScreenContext(_menuProperty, _titleProperty,
153                                                 _editDisabledProperty, _toolProperty, _leftMenuProperty);
154
155                                 setErrorCode("message.error.outofmemory");
156
157                                 res = "outofmemory";
158                         } catch (Exception error) {
159                                 LOG.error("Reason: ", error);
160                                 res = ERROR;
161                         }
162                 }
163                 return res;
164         }
165
166         // ==============================================================================================================================
167         // Getters and setters
168         // ==============================================================================================================================
169
170         /**
171          * Get the document index to which the action applies.
172          * 
173          * @return the document index to which the action applies
174          */
175         public String getIndex() {
176                 return _index;
177         }
178
179         /**
180          * Get the uploaded file name.
181          * 
182          * @return the uploaded file name
183          */
184         public String getFileName() {
185                 return _uploadFileName;
186         }
187
188         /**
189          * Get the action to which the uploaded file is passed.
190          * 
191          * @return the action to which the uploaded file is passed.
192          */
193         public String getNextAction() {
194                 return _nextAction;
195         }
196
197         /**
198          * Cancel the uploading.
199          * 
200          * @param back
201          */
202         public void setCancel(final boolean back) {
203                 this._action = ToDo.cancel;
204         }
205
206         /**
207          * Set the flag to perform uploading.
208          * 
209          * @param upload
210          */
211         public void setDoIt(final boolean upload) {
212                 this._action = ToDo.upload;
213         }
214
215         /**
216          * Set the document index to which the action applies.
217          * 
218          * @param index
219          *            the document index to which the action applies.
220          */
221         public void setIndex(final String index) {
222                 this._index = index;
223         }
224
225         /**
226          * Set the action to which the uploaded file is passed.
227          * 
228          * @param next
229          *            the action to which the uploaded file is passed.
230          */
231         public void setNextAction(final String next) {
232                 this._nextAction = next;
233         }
234
235         /**
236          * Set the uploaded file.
237          * 
238          * @param upload
239          *            the file
240          */
241         public void setUpload(final File upload) {
242                 this._upload = upload;
243         }
244
245         /**
246          * Set the uploaded file name.
247          * 
248          * @param name
249          *            the uploaded file name
250          */
251         public void setUploadFileName(final String name) {
252                 this._uploadFileName = name;
253         }
254
255         /**
256          * Set the mime type of the uploaded file.
257          * 
258          * @param mime
259          *            the mime type of the uploaded file
260          */
261         public void setUploadContentType(final String mime) {
262                 this._uploadMimeType = mime;
263         }
264
265         /**
266          * Get the repositoryService.
267          * 
268          * @return the repositoryService
269          */
270         public RepositoryService getRepositoryService() {
271                 return _repositoryService;
272         }
273
274         /**
275          * Set the repositoryService.
276          * 
277          * @param repositoryService
278          *            the repositoryService to set
279          */
280         public void setRepositoryService(final RepositoryService repositoryService) {
281                 _repositoryService = repositoryService;
282         }
283
284         /**
285          * Get the menuProperty.
286          * 
287          * @return the menuProperty
288          */
289         public String getMenuProperty() {
290                 return _menuProperty;
291         }
292
293         /**
294          * Set the menuProperty.
295          * 
296          * @param menuProperty
297          *            the menuProperty to set
298          */
299         public void setMenuProperty(final String menuProperty) {
300                 this._menuProperty = menuProperty;
301         }
302
303         /**
304          * Get the _titleProperty.
305          * 
306          * @return the _titleProperty
307          */
308         public String getTitleProperty() {
309                 return _titleProperty;
310         }
311
312         /**
313          * Set the _titleProperty.
314          * 
315          * @param titleProperty
316          *            the titleProperty to set
317          */
318         public void setTitleProperty(final String titleProperty) {
319                 _titleProperty = titleProperty;
320         }
321
322         /**
323          * Get the _editDisabledProperty.
324          * 
325          * @return the _editDisabledProperty
326          */
327         public String getEditDisabledProperty() {
328                 return _editDisabledProperty;
329         }
330
331         /**
332          * Set the editDisabledProperty.
333          * 
334          * @param editDisabledProperty
335          *            the editDisabledProperty to set
336          */
337         public void setEditDisabledProperty(final String editDisabledProperty) {
338                 this._editDisabledProperty = editDisabledProperty;
339         }
340
341         /**
342          * Get the toolProperty.
343          * 
344          * @return the toolProperty
345          */
346         public String getToolProperty() {
347                 return _toolProperty;
348         }
349
350         /**
351          * Set the toolProperty.
352          * 
353          * @param toolProperty
354          *            the toolProperty to set
355          */
356         public void setToolProperty(final String toolProperty) {
357                 _toolProperty = toolProperty;
358         }
359
360         /**
361          * Get the leftMenuProperty.
362          * 
363          * @return the leftMenuProperty
364          */
365         public String getLeftMenuProperty() {
366                 return _leftMenuProperty;
367         }
368
369         /**
370          * Set the leftMenuProperty.
371          * 
372          * @param leftMenuProperty
373          *            the leftMenuProperty to set
374          */
375         public void setLeftMenuProperty(final String leftMenuProperty) {
376                 _leftMenuProperty = leftMenuProperty;
377         }
378 }