]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/UploadAction.java
Salome HOME
Actions menu properties are refactored, unnecessary code is removed.
[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 action.
69          * 
70          * @return SUCCESS
71          */
72         public String doInitialize() {
73                 initializationFullScreenContext(Constants.STUDY_MENU,
74                                 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
75                                 Constants.OPEN);
76
77                 return SUCCESS;
78         }
79
80         /**
81          * Store uploaded file into the user's download directory.
82          * 
83          * @return next action if ok, "cancel" if the operation is cancelled by user, "outofmemory" if there is no enough memory to upload the
84          *         file or ERROR otherwise
85          */
86         public String doUpload() {
87                 initializationScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
88                                 Constants.TRUE);
89
90                 String res;
91                 if (_action == ToDo.cancel) {
92                         res = "cancel";
93                 } else {
94                         try {
95                                 File udir = getRepositoryService().getDownloadDirectory(
96                                                 getConnectedUser());
97                                 String path = udir.getPath() + "/" + _uploadFileName;
98                                 File file = new File(path);
99
100                                 if (!udir.exists()) {
101                                         udir.mkdir();
102                                 }
103                                 if (file.exists()) {
104                                         file.delete();
105                                 }
106                                 Do.copy(_upload, file);
107                                 LOG.info("Uploading \"" + _uploadFileName + "\" "
108                                                 + _uploadMimeType + " file.");
109                                 /*
110                                  * if (next == null || next.isEmpty()) { next = "import"; }
111                                  */
112
113                                 res = _nextAction;
114                         } catch (OutOfMemoryError error) {
115
116                                 initializationFullScreenContext(Constants.NONE,
117                                                 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
118                                                 Constants.STUDY_MENU);
119
120                                 setErrorCode("message.error.outofmemory");
121
122                                 res = "outofmemory";
123                         } catch (Exception error) {
124                                 LOG.error("Reason: ", error);
125                                 res = ERROR;
126                         }
127                 }
128                 return res;
129         }
130
131         // ==============================================================================================================================
132         // Getters and setters
133         // ==============================================================================================================================
134
135         /**
136          * Get the document index to which the action applies.
137          * 
138          * @return the document index to which the action applies
139          */
140         public String getIndex() {
141                 return _index;
142         }
143
144         /**
145          * Get the uploaded file name.
146          * 
147          * @return the uploaded file name
148          */
149         public String getFileName() {
150                 return _uploadFileName;
151         }
152
153         /**
154          * Get the action to which the uploaded file is passed.
155          * 
156          * @return the action to which the uploaded file is passed.
157          */
158         public String getNextAction() {
159                 return _nextAction;
160         }
161
162         /**
163          * Cancel the uploading.
164          * 
165          * @param back
166          */
167         public void setCancel(final boolean back) {
168                 this._action = ToDo.cancel;
169         }
170
171         /**
172          * Set the flag to perform uploading.
173          * 
174          * @param upload
175          */
176         public void setDoIt(final boolean upload) {
177                 this._action = ToDo.upload;
178         }
179
180         /**
181          * Set the document index to which the action applies.
182          * 
183          * @param index
184          *            the document index to which the action applies.
185          */
186         public void setIndex(final String index) {
187                 this._index = index;
188         }
189
190         /**
191          * Set the action to which the uploaded file is passed.
192          * 
193          * @param next
194          *            the action to which the uploaded file is passed.
195          */
196         public void setNextAction(final String next) {
197                 this._nextAction = next;
198         }
199
200         /**
201          * Set the uploaded file.
202          * 
203          * @param upload
204          *            the file
205          */
206         public void setUpload(final File upload) {
207                 this._upload = upload;
208         }
209
210         /**
211          * Set the uploaded file name.
212          * 
213          * @param name
214          *            the uploaded file name
215          */
216         public void setUploadFileName(final String name) {
217                 this._uploadFileName = name;
218         }
219
220         /**
221          * Set the mime type of the uploaded file.
222          * 
223          * @param mime
224          *            the mime type of the uploaded file
225          */
226         public void setUploadContentType(final String mime) {
227                 this._uploadMimeType = mime;
228         }
229
230         /**
231          * Get the repositoryService.
232          * 
233          * @return the repositoryService
234          */
235         public RepositoryService getRepositoryService() {
236                 return _repositoryService;
237         }
238
239         /**
240          * Set the repositoryService.
241          * 
242          * @param repositoryService
243          *            the repositoryService to set
244          */
245         public void setRepositoryService(final RepositoryService repositoryService) {
246                 _repositoryService = repositoryService;
247         }
248 }