]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/UploadAction.java
Salome HOME
ce5b166e28246b332a94d77c4980b512bb2ccae6
[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                 setTitleProperty(Constants.STUDY_MENU);
103                 setEditDisabledProperty("true");
104                 initializationFullScreenContext(_menuProperty, _titleProperty,
105                                 _editDisabledProperty, _toolProperty, _leftMenuProperty);
106
107                 return SUCCESS;
108         }
109
110         /**
111          * Store uploaded file into the user's download directory.
112          * 
113          * @return next action if ok, "cancel" if the operation is cancelled by user, "outofmemory" if there is no enough memory to upload the
114          *         file or ERROR otherwise
115          */
116         public String doUpload() {
117                 setMenuProperty(Constants.STUDY_MENU);
118                 setTitleProperty(Constants.STUDY_MENU);
119                 setEditDisabledProperty("true");
120                 initializationScreenContext(_menuProperty, _titleProperty,
121                                 _editDisabledProperty);
122
123                 String res;
124                 if (_action == ToDo.cancel) {
125                         res = "cancel";
126                 } else {
127                         try {
128                                 File udir = getRepositoryService().getDownloadDirectory(
129                                                 getConnectedUser());
130                                 String path = udir.getPath() + "/" + _uploadFileName;
131                                 File file = new File(path);
132
133                                 if (!udir.exists()) {
134                                         udir.mkdir();
135                                 }
136                                 if (file.exists()) {
137                                         file.delete();
138                                 }
139                                 Do.copy(_upload, file);
140                                 LOG.info("Uploading \"" + _uploadFileName + "\" "
141                                                 + _uploadMimeType + " file.");
142                                 /*
143                                  * if (next == null || next.isEmpty()) { next = "import"; }
144                                  */
145
146                                 res = _nextAction;
147                         } catch (OutOfMemoryError error) {
148
149                                 setMenuProperty(Constants.NONE);
150                                 setTitleProperty(Constants.STUDY_MENU);
151                                 setEditDisabledProperty("true");
152                                 setToolProperty(Constants.NONE);
153                                 setLeftMenuProperty(Constants.STUDY_MENU);
154                                 initializationFullScreenContext(_menuProperty, _titleProperty,
155                                                 _editDisabledProperty, _toolProperty, _leftMenuProperty);
156
157                                 setErrorCode("message.error.outofmemory");
158
159                                 res = "outofmemory";
160                         } catch (Exception error) {
161                                 LOG.error("Reason: ", error);
162                                 res = ERROR;
163                         }
164                 }
165                 return res;
166         }
167
168         // ==============================================================================================================================
169         // Getters and setters
170         // ==============================================================================================================================
171
172         /**
173          * Get the document index to which the action applies.
174          * 
175          * @return the document index to which the action applies
176          */
177         public String getIndex() {
178                 return _index;
179         }
180
181         /**
182          * Get the uploaded file name.
183          * 
184          * @return the uploaded file name
185          */
186         public String getFileName() {
187                 return _uploadFileName;
188         }
189
190         /**
191          * Get the action to which the uploaded file is passed.
192          * 
193          * @return the action to which the uploaded file is passed.
194          */
195         public String getNextAction() {
196                 return _nextAction;
197         }
198
199         /**
200          * Cancel the uploading.
201          * 
202          * @param back
203          */
204         public void setCancel(final boolean back) {
205                 this._action = ToDo.cancel;
206         }
207
208         /**
209          * Set the flag to perform uploading.
210          * 
211          * @param upload
212          */
213         public void setDoIt(final boolean upload) {
214                 this._action = ToDo.upload;
215         }
216
217         /**
218          * Set the document index to which the action applies.
219          * 
220          * @param index
221          *            the document index to which the action applies.
222          */
223         public void setIndex(final String index) {
224                 this._index = index;
225         }
226
227         /**
228          * Set the action to which the uploaded file is passed.
229          * 
230          * @param next
231          *            the action to which the uploaded file is passed.
232          */
233         public void setNextAction(final String next) {
234                 this._nextAction = next;
235         }
236
237         /**
238          * Set the uploaded file.
239          * 
240          * @param upload
241          *            the file
242          */
243         public void setUpload(final File upload) {
244                 this._upload = upload;
245         }
246
247         /**
248          * Set the uploaded file name.
249          * 
250          * @param name
251          *            the uploaded file name
252          */
253         public void setUploadFileName(final String name) {
254                 this._uploadFileName = name;
255         }
256
257         /**
258          * Set the mime type of the uploaded file.
259          * 
260          * @param mime
261          *            the mime type of the uploaded file
262          */
263         public void setUploadContentType(final String mime) {
264                 this._uploadMimeType = mime;
265         }
266
267         /**
268          * Get the repositoryService.
269          * 
270          * @return the repositoryService
271          */
272         public RepositoryService getRepositoryService() {
273                 return _repositoryService;
274         }
275
276         /**
277          * Set the repositoryService.
278          * 
279          * @param repositoryService
280          *            the repositoryService to set
281          */
282         public void setRepositoryService(final RepositoryService repositoryService) {
283                 _repositoryService = repositoryService;
284         }
285
286         /**
287          * Get the menuProperty.
288          * 
289          * @return the menuProperty
290          */
291         public String getMenuProperty() {
292                 return _menuProperty;
293         }
294
295         /**
296          * Set the menuProperty.
297          * 
298          * @param menuProperty
299          *            the menuProperty to set
300          */
301         public void setMenuProperty(final String menuProperty) {
302                 this._menuProperty = menuProperty;
303         }
304
305         /**
306          * Get the _titleProperty.
307          * 
308          * @return the _titleProperty
309          */
310         public String getTitleProperty() {
311                 return _titleProperty;
312         }
313
314         /**
315          * Set the _titleProperty.
316          * 
317          * @param titleProperty
318          *            the titleProperty to set
319          */
320         public void setTitleProperty(final String titleProperty) {
321                 _titleProperty = titleProperty;
322         }
323
324         /**
325          * Get the _editDisabledProperty.
326          * 
327          * @return the _editDisabledProperty
328          */
329         public String getEditDisabledProperty() {
330                 return _editDisabledProperty;
331         }
332
333         /**
334          * Set the editDisabledProperty.
335          * 
336          * @param editDisabledProperty
337          *            the editDisabledProperty to set
338          */
339         public void setEditDisabledProperty(final String editDisabledProperty) {
340                 this._editDisabledProperty = editDisabledProperty;
341         }
342
343         /**
344          * Get the toolProperty.
345          * 
346          * @return the toolProperty
347          */
348         public String getToolProperty() {
349                 return _toolProperty;
350         }
351
352         /**
353          * Set the toolProperty.
354          * 
355          * @param toolProperty
356          *            the toolProperty to set
357          */
358         public void setToolProperty(final String toolProperty) {
359                 _toolProperty = toolProperty;
360         }
361
362         /**
363          * Get the leftMenuProperty.
364          * 
365          * @return the leftMenuProperty
366          */
367         public String getLeftMenuProperty() {
368                 return _leftMenuProperty;
369         }
370
371         /**
372          * Set the leftMenuProperty.
373          * 
374          * @param leftMenuProperty
375          *            the leftMenuProperty to set
376          */
377         public void setLeftMenuProperty(final String leftMenuProperty) {
378                 _leftMenuProperty = leftMenuProperty;
379         }
380 }