Salome HOME
Fix for uploading of files with names containing specific (accented, ampersand) chara...
[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                 setSimanContext("#Database_Management.htm");
91
92                 return SUCCESS;
93         }
94
95         /**
96          * Store uploaded file into the user's download directory.
97          * 
98          * @return next action if ok, "cancel" if the operation is cancelled by user, "outofmemory" if there is no enough memory to upload the
99          *         file or ERROR otherwise
100          */
101         public String doUpload() {
102                 initializationScreenContext(getMenuProperty(), Constants.STUDY_MENU,
103                                 Constants.TRUE);
104
105                 String res;
106                 if (_action == ToDo.cancel) {
107                         res = "cancel";
108                 } else {
109                         try {
110                                 File udir = getRepositoryService().getDownloadDirectory(
111                                                 getConnectedUser());
112                                 String path = udir.getPath() + "/" + _uploadFileName;
113                                 File file = new File(path);
114
115                                 if (!udir.exists()) {
116                                         udir.mkdir();
117                                 }
118                                 if (file.exists()) {
119                                         file.delete();
120                                 }
121                                 Do.copy(_upload, file);
122                                 LOG.info("Uploading \"" + _uploadFileName + "\" "
123                                                 + _uploadMimeType + " file.");
124                                 /*
125                                  * if (next == null || next.isEmpty()) { next = "import"; }
126                                  */
127
128                                 res = _nextAction;
129                         } catch (OutOfMemoryError error) {
130
131                                 initializationFullScreenContext(Constants.NONE,
132                                                 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
133                                                 Constants.STUDY_MENU);
134
135                                 setErrorCode("message.error.outofmemory");
136
137                                 res = "outofmemory";
138                         } catch (Exception error) {
139                                 LOG.error("Reason: ", error);
140                                 res = ERROR;
141                         }
142                 }
143                 return res;
144         }
145
146         // ==============================================================================================================================
147         // Getters and setters
148         // ==============================================================================================================================
149
150         /**
151          * Get the document index to which the action applies.
152          * 
153          * @return the document index to which the action applies
154          */
155         public String getIndex() {
156                 return _index;
157         }
158
159         /**
160          * Get the uploaded file name.
161          * 
162          * @return the uploaded file name
163          */
164         public String getFileName() {
165                 return _uploadFileName;
166         }
167
168         /**
169          * Get fileName with url special symbols canceled for usage in struts.xml.
170          * @return the encoded uploaded file name.
171          */
172         public String getCanceledFileName() {
173                 String res = _uploadFileName;
174                 try {
175                         res = URLEncoder.encode(res, "ISO-8859-1");
176                 } catch (UnsupportedEncodingException e) {
177                         LOG.error("Reason: ", e);
178                 }
179                 return res;
180         }
181         
182         /**
183          * Get the action to which the uploaded file is passed.
184          * 
185          * @return the action to which the uploaded file is passed.
186          */
187         public String getNextAction() {
188                 return _nextAction;
189         }
190
191         /**
192          * Cancel the uploading.
193          * 
194          * @param back
195          */
196         public void setCancel(final boolean back) {
197                 this._action = ToDo.cancel;
198         }
199
200         /**
201          * Set the flag to perform uploading.
202          * 
203          * @param upload
204          */
205         public void setDoIt(final boolean upload) {
206                 this._action = ToDo.upload;
207         }
208
209         /**
210          * Set the document index to which the action applies.
211          * 
212          * @param index
213          *            the document index to which the action applies.
214          */
215         public void setIndex(final String index) {
216                 this._index = index;
217         }
218
219         /**
220          * Set the action to which the uploaded file is passed.
221          * 
222          * @param next
223          *            the action to which the uploaded file is passed.
224          */
225         public void setNextAction(final String next) {
226                 this._nextAction = next;
227         }
228
229         /**
230          * Set the uploaded file.
231          * 
232          * @param upload
233          *            the file
234          */
235         public void setUpload(final File upload) {
236                 this._upload = upload;
237         }
238
239         /**
240          * Set the uploaded file name.
241          * 
242          * @param name
243          *            the uploaded file name
244          */
245         public void setUploadFileName(final String name) {
246                 this._uploadFileName = name;
247         }
248
249         /**
250          * Set the mime type of the uploaded file.
251          * 
252          * @param mime
253          *            the mime type of the uploaded file
254          */
255         public void setUploadContentType(final String mime) {
256                 this._uploadMimeType = mime;
257         }
258
259         /**
260          * Get the repositoryService.
261          * 
262          * @return the repositoryService
263          */
264         public RepositoryService getRepositoryService() {
265                 return _repositoryService;
266         }
267
268         /**
269          * Set the repositoryService.
270          * 
271          * @param repositoryService
272          *            the repositoryService to set
273          */
274         public void setRepositoryService(final RepositoryService repositoryService) {
275                 _repositoryService = repositoryService;
276         }
277 }