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,
95 * Store uploaded file into the user's download directory.
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
100 public String doUpload() {
101 initializationScreenContext(getMenuProperty(), Constants.STUDY_MENU,
105 if (_action == ToDo.cancel) {
109 File udir = getRepositoryService().getDownloadDirectory(
111 String path = udir.getPath() + "/" + _uploadFileName;
112 File file = new File(path);
114 if (!udir.exists()) {
120 Do.copy(_upload, file);
121 LOG.info("Uploading \"" + _uploadFileName + "\" "
122 + _uploadMimeType + " file.");
124 * if (next == null || next.isEmpty()) { next = "import"; }
128 } catch (OutOfMemoryError error) {
130 initializationFullScreenContext(Constants.NONE,
131 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
132 Constants.STUDY_MENU);
134 setErrorCode("message.error.outofmemory");
137 } catch (Exception error) {
138 LOG.error("Reason: ", error);
145 // ==============================================================================================================================
146 // Getters and setters
147 // ==============================================================================================================================
150 * Get the document index to which the action applies.
152 * @return the document index to which the action applies
154 public String getIndex() {
159 * Get the uploaded file name.
161 * @return the uploaded file name
163 public String getFileName() {
164 return _uploadFileName;
168 * Get fileName with url special symbols canceled for usage in struts.xml.
169 * @return the encoded uploaded file name.
171 public String getCanceledFileName() {
172 String res = _uploadFileName;
174 res = URLEncoder.encode(res, "UTF-8");
175 } catch (UnsupportedEncodingException e) {
176 LOG.error("Reason: ", e);
182 * Get the action to which the uploaded file is passed.
184 * @return the action to which the uploaded file is passed.
186 public String getNextAction() {
191 * Cancel the uploading.
195 public void setCancel(final boolean back) {
196 this._action = ToDo.cancel;
200 * Set the flag to perform uploading.
204 public void setDoIt(final boolean upload) {
205 this._action = ToDo.upload;
209 * Set the document index to which the action applies.
212 * the document index to which the action applies.
214 public void setIndex(final String index) {
219 * Set the action to which the uploaded file is passed.
222 * the action to which the uploaded file is passed.
224 public void setNextAction(final String next) {
225 this._nextAction = next;
229 * Set the uploaded file.
234 public void setUpload(final File upload) {
235 this._upload = upload;
239 * Set the uploaded file name.
242 * the uploaded file name
244 public void setUploadFileName(final String name) {
245 this._uploadFileName = name;
249 * Set the mime type of the uploaded file.
252 * the mime type of the uploaded file
254 public void setUploadContentType(final String mime) {
255 this._uploadMimeType = mime;
259 * Get the repositoryService.
261 * @return the repositoryService
263 public RepositoryService getRepositoryService() {
264 return _repositoryService;
268 * Set the repositoryService.
270 * @param repositoryService
271 * the repositoryService to set
273 public void setRepositoryService(final RepositoryService repositoryService) {
274 _repositoryService = repositoryService;