Salome HOME
comment is updated.
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / UploadAction.java
1 package org.splat.simer;
2
3 import java.io.File;
4 import java.io.UnsupportedEncodingException;
5 import java.net.URLEncoder;
6
7 import org.splat.kernel.Do;
8 import org.splat.service.technical.RepositoryService;
9 import org.splat.wapp.Constants;
10
11 /**
12  * Action for uploading a file into the user's download directory.
13  */
14 public class UploadAction extends Action {
15
16         /**
17          * Serialization version id.
18          */
19         private static final long serialVersionUID = 6003880772275115923L;
20
21         /**
22          * Uploaded file.
23          */
24         private transient File _upload = null;
25         /**
26          * Mime type of the uploaded file.
27          */
28         private transient String _uploadMimeType = null;
29         /**
30          * Uploaded file name.
31          */
32         private transient String _uploadFileName = null;
33
34         /**
35          * Action to do.
36          */
37         private transient ToDo _action;
38         /**
39          * Action to which the uploaded file is passed.
40          */
41         private String _nextAction = null;
42         /**
43          * Depending on the next action, document index to which the action applies.
44          */
45         private String _index = null;
46         /**
47          * Injected repository service.
48          */
49         private RepositoryService _repositoryService;
50
51         /**
52          * Action modes enumeration.
53          */
54         private enum ToDo {
55                 /**
56                  * Cancel the operation.
57                  */
58                 cancel,
59                 /**
60                  * Perform uploading.
61                  */
62                 upload
63         };
64
65         // ==============================================================================================================================
66         // Action methods
67         // ==============================================================================================================================
68
69         /**
70          * Prepare form for the upload documents to study action.
71          * 
72          * @return SUCCESS
73          */
74         public String doInitializeStudy() {
75                 initializationFullScreenContext(Constants.STUDY_MENU,
76                                 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
77                                 Constants.STUDY_MENU);
78
79                 return SUCCESS;
80         }
81         
82         /**
83          * Prepare form for the upload users action.
84          * @return SUCCESS
85          */
86         public String doInitializeSysAdmin() {
87                 initializationFullScreenContext(Constants.SYSADMIN_MENU,
88                                 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
89                                 Constants.OPEN);
90
91                 return SUCCESS;
92         }
93
94         /**
95          * Store uploaded file into the user's download directory.
96          * 
97          * @return next action if ok, "cancel" if the operation is cancelled by user, "outofmemory" if there is no enough memory to upload the
98          *         file or ERROR otherwise
99          */
100         public String doUpload() {
101                 initializationScreenContext(getMenuProperty(), Constants.STUDY_MENU,
102                                 Constants.TRUE);
103
104                 String res;
105                 if (_action == ToDo.cancel) {
106                         res = "cancel";
107                 } else {
108                         try {
109                                 File udir = getRepositoryService().getDownloadDirectory(
110                                                 getConnectedUser());
111                                 String path = udir.getPath() + "/" + _uploadFileName;
112                                 File file = new File(path);
113
114                                 if (!udir.exists()) {
115                                         udir.mkdir();
116                                 }
117                                 if (file.exists()) {
118                                         file.delete();
119                                 }
120                                 Do.copy(_upload, file);
121                                 LOG.info("Uploading \"" + _uploadFileName + "\" "
122                                                 + _uploadMimeType + " file.");
123                                 /*
124                                  * if (next == null || next.isEmpty()) { next = "import"; }
125                                  */
126
127                                 res = _nextAction;
128                         } catch (OutOfMemoryError error) {
129
130                                 initializationFullScreenContext(Constants.NONE,
131                                                 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
132                                                 Constants.STUDY_MENU);
133
134                                 setErrorCode("message.error.outofmemory");
135
136                                 res = "outofmemory";
137                         } catch (Exception error) {
138                                 LOG.error("Reason: ", error);
139                                 res = ERROR;
140                         }
141                 }
142                 return res;
143         }
144
145         // ==============================================================================================================================
146         // Getters and setters
147         // ==============================================================================================================================
148
149         /**
150          * Get the document index to which the action applies.
151          * 
152          * @return the document index to which the action applies
153          */
154         public String getIndex() {
155                 return _index;
156         }
157
158         /**
159          * Get the uploaded file name.
160          * 
161          * @return the uploaded file name
162          */
163         public String getFileName() {
164                 return _uploadFileName;
165         }
166
167         /**
168          * Get fileName with url special symbols canceled for usage in struts.xml.
169          * @return the encoded uploaded file name.
170          */
171         public String getCanceledFileName() {
172                 String res = _uploadFileName;
173                 try {
174                         res = URLEncoder.encode(res, "UTF-8");
175                 } catch (UnsupportedEncodingException e) {
176                         LOG.error("Reason: ", e);
177                 }
178                 return res;
179         }
180         
181         /**
182          * Get the action to which the uploaded file is passed.
183          * 
184          * @return the action to which the uploaded file is passed.
185          */
186         public String getNextAction() {
187                 return _nextAction;
188         }
189
190         /**
191          * Cancel the uploading.
192          * 
193          * @param back
194          */
195         public void setCancel(final boolean back) {
196                 this._action = ToDo.cancel;
197         }
198
199         /**
200          * Set the flag to perform uploading.
201          * 
202          * @param upload
203          */
204         public void setDoIt(final boolean upload) {
205                 this._action = ToDo.upload;
206         }
207
208         /**
209          * Set the document index to which the action applies.
210          * 
211          * @param index
212          *            the document index to which the action applies.
213          */
214         public void setIndex(final String index) {
215                 this._index = index;
216         }
217
218         /**
219          * Set the action to which the uploaded file is passed.
220          * 
221          * @param next
222          *            the action to which the uploaded file is passed.
223          */
224         public void setNextAction(final String next) {
225                 this._nextAction = next;
226         }
227
228         /**
229          * Set the uploaded file.
230          * 
231          * @param upload
232          *            the file
233          */
234         public void setUpload(final File upload) {
235                 this._upload = upload;
236         }
237
238         /**
239          * Set the uploaded file name.
240          * 
241          * @param name
242          *            the uploaded file name
243          */
244         public void setUploadFileName(final String name) {
245                 this._uploadFileName = name;
246         }
247
248         /**
249          * Set the mime type of the uploaded file.
250          * 
251          * @param mime
252          *            the mime type of the uploaded file
253          */
254         public void setUploadContentType(final String mime) {
255                 this._uploadMimeType = mime;
256         }
257
258         /**
259          * Get the repositoryService.
260          * 
261          * @return the repositoryService
262          */
263         public RepositoryService getRepositoryService() {
264                 return _repositoryService;
265         }
266
267         /**
268          * Set the repositoryService.
269          * 
270          * @param repositoryService
271          *            the repositoryService to set
272          */
273         public void setRepositoryService(final RepositoryService repositoryService) {
274                 _repositoryService = repositoryService;
275         }
276 }