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.
86 public String doInitializeSysAdmin() {
87 initializationFullScreenContext(Constants.SYSADMIN_MENU,
88 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
90 setSimanContext("#Database_Management.htm");
96 * Store uploaded file into the user's download directory.
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
101 public String doUpload() {
102 initializationScreenContext(getMenuProperty(), Constants.STUDY_MENU,
106 if (_action == ToDo.cancel) {
110 File udir = getRepositoryService().getDownloadDirectory(
112 String path = udir.getPath() + "/" + _uploadFileName;
113 File file = new File(path);
115 if (!udir.exists()) {
121 Do.copy(_upload, file);
122 LOG.info("Uploading \"" + _uploadFileName + "\" "
123 + _uploadMimeType + " file.");
125 * if (next == null || next.isEmpty()) { next = "import"; }
129 } catch (OutOfMemoryError error) {
131 initializationFullScreenContext(Constants.NONE,
132 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
133 Constants.STUDY_MENU);
135 setErrorCode("message.error.outofmemory");
138 } catch (Exception error) {
139 LOG.error("Reason: ", error);
146 // ==============================================================================================================================
147 // Getters and setters
148 // ==============================================================================================================================
151 * Get the document index to which the action applies.
153 * @return the document index to which the action applies
155 public String getIndex() {
160 * Get the uploaded file name.
162 * @return the uploaded file name
164 public String getFileName() {
165 return _uploadFileName;
169 * Get fileName with url special symbols canceled for usage in struts.xml.
170 * @return the encoded uploaded file name.
172 public String getCanceledFileName() {
173 String res = _uploadFileName;
175 res = URLEncoder.encode(res, "ISO-8859-1");
176 } catch (UnsupportedEncodingException e) {
177 LOG.error("Reason: ", e);
183 * Get the action to which the uploaded file is passed.
185 * @return the action to which the uploaded file is passed.
187 public String getNextAction() {
192 * Cancel the uploading.
196 public void setCancel(final boolean back) {
197 this._action = ToDo.cancel;
201 * Set the flag to perform uploading.
205 public void setDoIt(final boolean upload) {
206 this._action = ToDo.upload;
210 * Set the document index to which the action applies.
213 * the document index to which the action applies.
215 public void setIndex(final String index) {
220 * Set the action to which the uploaded file is passed.
223 * the action to which the uploaded file is passed.
225 public void setNextAction(final String next) {
226 this._nextAction = next;
230 * Set the uploaded file.
235 public void setUpload(final File upload) {
236 this._upload = upload;
240 * Set the uploaded file name.
243 * the uploaded file name
245 public void setUploadFileName(final String name) {
246 this._uploadFileName = name;
250 * Set the mime type of the uploaded file.
253 * the mime type of the uploaded file
255 public void setUploadContentType(final String mime) {
256 this._uploadMimeType = mime;
260 * Get the repositoryService.
262 * @return the repositoryService
264 public RepositoryService getRepositoryService() {
265 return _repositoryService;
269 * Set the repositoryService.
271 * @param repositoryService
272 * the repositoryService to set
274 public void setRepositoryService(final RepositoryService repositoryService) {
275 _repositoryService = repositoryService;