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