1 package org.splat.simer;
4 import java.io.UnsupportedEncodingException;
5 import java.net.URLEncoder;
7 import org.splat.kernel.Do;
8 import org.splat.service.technical.RepositoryService;
9 import org.splat.wapp.Constants;
12 * Action for uploading a file into the user's download directory.
14 public class UploadAction extends Action {
17 * Serialization version id.
19 private static final long serialVersionUID = 6003880772275115923L;
24 private transient File _upload = null;
26 * Mime type of the uploaded file.
28 private transient String _uploadMimeType = null;
32 private transient String _uploadFileName = null;
37 private transient ToDo _action;
39 * Action to which the uploaded file is passed.
41 private String _nextAction = null;
43 * Depending on the next action, document index to which the action applies.
45 private String _index = null;
47 * Injected repository service.
49 private RepositoryService _repositoryService;
52 * Action modes enumeration.
56 * Cancel the operation.
65 // ==============================================================================================================================
67 // ==============================================================================================================================
70 * Prepare form for the upload documents to study action.
74 public String doInitializeStudy() {
75 initializationFullScreenContext(Constants.STUDY_MENU,
76 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
77 Constants.STUDY_MENU);
83 * Prepare form for the upload users action.
87 public String doInitializeSysAdmin() {
88 initializationFullScreenContext(Constants.SYSADMIN_MENU,
89 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
91 setSimanContext("#Database_Management.htm");
97 * Store uploaded file into the user's download directory.
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
102 public String doUpload() {
103 initializationScreenContext(getMenuProperty(), Constants.STUDY_MENU,
107 if (_action == ToDo.cancel) {
111 File udir = getRepositoryService().getDownloadDirectory(
113 String path = udir.getPath() + "/" + _uploadFileName;
114 File file = new File(path);
116 if (!udir.exists()) {
122 Do.copy(_upload, file);
123 LOG.info("Uploading \"" + _uploadFileName + "\" "
124 + _uploadMimeType + " file.");
126 * if (next == null || next.isEmpty()) { next = "import"; }
130 } catch (OutOfMemoryError error) {
132 initializationFullScreenContext(Constants.NONE,
133 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
134 Constants.STUDY_MENU);
136 setErrorCode("message.error.outofmemory");
139 } catch (Exception error) {
140 LOG.error("Reason: ", error);
147 // ==============================================================================================================================
148 // Getters and setters
149 // ==============================================================================================================================
152 * Get the document index to which the action applies.
154 * @return the document index to which the action applies
156 public String getIndex() {
161 * Get the uploaded file name.
163 * @return the uploaded file name
165 public String getFileName() {
166 return _uploadFileName;
170 * Get fileName with url special symbols canceled for usage in struts.xml.
172 * @return the encoded uploaded file name.
174 public String getCanceledFileName() {
175 String res = _uploadFileName;
177 res = URLEncoder.encode(res, "ISO-8859-1");
178 } catch (UnsupportedEncodingException e) {
179 LOG.error("Reason: ", e);
185 * Get the action to which the uploaded file is passed.
187 * @return the action to which the uploaded file is passed.
189 public String getNextAction() {
194 * Set the flag to cancel the uploading.
199 public void setCancel(final boolean back) {
200 this._action = ToDo.cancel;
204 * Set the flag to perform uploading.
209 public void setDoIt(final boolean upload) {
210 this._action = ToDo.upload;
214 * Set the document index to which the action applies.
217 * the document index to which the action applies.
219 public void setIndex(final String index) {
224 * Set the action to which the uploaded file is passed.
227 * the action to which the uploaded file is passed.
229 public void setNextAction(final String next) {
230 this._nextAction = next;
234 * Set the uploaded file.
239 public void setUpload(final File upload) {
240 this._upload = upload;
244 * Set the uploaded file name.
247 * the uploaded file name
249 public void setUploadFileName(final String name) {
250 this._uploadFileName = name;
254 * Set the mime type of the uploaded file.
257 * the mime type of the uploaded file
259 public void setUploadContentType(final String mime) {
260 this._uploadMimeType = mime;
264 * Get the repositoryService.
266 * @return the repositoryService
268 public RepositoryService getRepositoryService() {
269 return _repositoryService;
273 * Set the repositoryService.
275 * @param repositoryService
276 * the repositoryService to set
278 public void setRepositoryService(final RepositoryService repositoryService) {
279 _repositoryService = repositoryService;