Salome HOME
Fix for mantis #0022111: Internal error during logging out after cancel the import...
[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          * Action modes enumeration.
51          */
52         private enum ToDo {
53                 /**
54                  * Cancel the operation.
55                  */
56                 cancel,
57                 /**
58                  * Perform uploading.
59                  */
60                 upload
61         };
62
63         // ==============================================================================================================================
64         // Action methods
65         // ==============================================================================================================================
66
67         /**
68          * Prepare form for the upload documents to study action.
69          * 
70          * @return SUCCESS
71          */
72         public String doInitializeStudy() {
73                 initializationFullScreenContext(Constants.STUDY_MENU,
74                                 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
75                                 Constants.STUDY_MENU);
76
77                 return SUCCESS;
78         }
79         
80         /**
81          * Prepare form for the upload users action.
82          * @return SUCCESS
83          */
84         public String doInitializeSysAdmin() {
85                 initializationFullScreenContext(Constants.SYSADMIN_MENU,
86                                 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
87                                 Constants.OPEN);
88
89                 return SUCCESS;
90         }
91
92         /**
93          * Store uploaded file into the user's download directory.
94          * 
95          * @return next action if ok, "cancel" if the operation is cancelled by user, "outofmemory" if there is no enough memory to upload the
96          *         file or ERROR otherwise
97          */
98         public String doUpload() {
99                 initializationScreenContext(getMenuProperty(), Constants.STUDY_MENU,
100                                 Constants.TRUE);
101
102                 String res;
103                 if (_action == ToDo.cancel) {
104                         res = "cancel";
105                 } else {
106                         try {
107                                 File udir = getRepositoryService().getDownloadDirectory(
108                                                 getConnectedUser());
109                                 String path = udir.getPath() + "/" + _uploadFileName;
110                                 File file = new File(path);
111
112                                 if (!udir.exists()) {
113                                         udir.mkdir();
114                                 }
115                                 if (file.exists()) {
116                                         file.delete();
117                                 }
118                                 Do.copy(_upload, file);
119                                 LOG.info("Uploading \"" + _uploadFileName + "\" "
120                                                 + _uploadMimeType + " file.");
121                                 /*
122                                  * if (next == null || next.isEmpty()) { next = "import"; }
123                                  */
124
125                                 res = _nextAction;
126                         } catch (OutOfMemoryError error) {
127
128                                 initializationFullScreenContext(Constants.NONE,
129                                                 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
130                                                 Constants.STUDY_MENU);
131
132                                 setErrorCode("message.error.outofmemory");
133
134                                 res = "outofmemory";
135                         } catch (Exception error) {
136                                 LOG.error("Reason: ", error);
137                                 res = ERROR;
138                         }
139                 }
140                 return res;
141         }
142
143         // ==============================================================================================================================
144         // Getters and setters
145         // ==============================================================================================================================
146
147         /**
148          * Get the document index to which the action applies.
149          * 
150          * @return the document index to which the action applies
151          */
152         public String getIndex() {
153                 return _index;
154         }
155
156         /**
157          * Get the uploaded file name.
158          * 
159          * @return the uploaded file name
160          */
161         public String getFileName() {
162                 return _uploadFileName;
163         }
164
165         /**
166          * Get the action to which the uploaded file is passed.
167          * 
168          * @return the action to which the uploaded file is passed.
169          */
170         public String getNextAction() {
171                 return _nextAction;
172         }
173
174         /**
175          * Cancel the uploading.
176          * 
177          * @param back
178          */
179         public void setCancel(final boolean back) {
180                 this._action = ToDo.cancel;
181         }
182
183         /**
184          * Set the flag to perform uploading.
185          * 
186          * @param upload
187          */
188         public void setDoIt(final boolean upload) {
189                 this._action = ToDo.upload;
190         }
191
192         /**
193          * Set the document index to which the action applies.
194          * 
195          * @param index
196          *            the document index to which the action applies.
197          */
198         public void setIndex(final String index) {
199                 this._index = index;
200         }
201
202         /**
203          * Set the action to which the uploaded file is passed.
204          * 
205          * @param next
206          *            the action to which the uploaded file is passed.
207          */
208         public void setNextAction(final String next) {
209                 this._nextAction = next;
210         }
211
212         /**
213          * Set the uploaded file.
214          * 
215          * @param upload
216          *            the file
217          */
218         public void setUpload(final File upload) {
219                 this._upload = upload;
220         }
221
222         /**
223          * Set the uploaded file name.
224          * 
225          * @param name
226          *            the uploaded file name
227          */
228         public void setUploadFileName(final String name) {
229                 this._uploadFileName = name;
230         }
231
232         /**
233          * Set the mime type of the uploaded file.
234          * 
235          * @param mime
236          *            the mime type of the uploaded file
237          */
238         public void setUploadContentType(final String mime) {
239                 this._uploadMimeType = mime;
240         }
241
242         /**
243          * Get the repositoryService.
244          * 
245          * @return the repositoryService
246          */
247         public RepositoryService getRepositoryService() {
248                 return _repositoryService;
249         }
250
251         /**
252          * Set the repositoryService.
253          * 
254          * @param repositoryService
255          *            the repositoryService to set
256          */
257         public void setRepositoryService(final RepositoryService repositoryService) {
258                 _repositoryService = repositoryService;
259         }
260 }