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
index ad513195f7bb45f8eb49288f071b6a8c17cd29ad..95b5b3874f74a086e691303a0277d101776271b2 100644 (file)
 package org.splat.simer;
 
 import java.io.File;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
 
 import org.splat.kernel.Do;
 import org.splat.service.technical.RepositoryService;
-import org.splat.dal.dao.som.Database;
-
+import org.splat.wapp.Constants;
 
+/**
+ * Action for uploading a file into the user's download directory.
+ */
 public class UploadAction extends Action {
 
-       private File    upload         = null;
-       private String  uploadMimeType = null;
-    private String  uploadFileName = null;
+       /**
+        * Serialization version id.
+        */
+       private static final long serialVersionUID = 6003880772275115923L;
+
+       /**
+        * Uploaded file.
+        */
+       private transient File _upload = null;
+       /**
+        * Mime type of the uploaded file.
+        */
+       private transient String _uploadMimeType = null;
+       /**
+        * Uploaded file name.
+        */
+       private transient String _uploadFileName = null;
 
-    private ToDo    action;                  // Action to do
-    private String  next           = null;   // Action to which the uploaded file is passed
-       private String  index          = null;   // Depending on the next action, document index to which the action applies
+       /**
+        * Action to do.
+        */
+       private transient ToDo _action;
+       /**
+        * Action to which the uploaded file is passed.
+        */
+       private String _nextAction = null;
+       /**
+        * Depending on the next action, document index to which the action applies.
+        */
+       private String _index = null;
        /**
         * Injected repository service.
         */
        private RepositoryService _repositoryService;
 
-       private static final long serialVersionUID = 6003880772275115923L;
+       /**
+        * Action modes enumeration.
+        */
+       private enum ToDo {
+               /**
+                * Cancel the operation.
+                */
+               cancel,
+               /**
+                * Perform uploading.
+                */
+               upload
+       };
+
+       // ==============================================================================================================================
+       // Action methods
+       // ==============================================================================================================================
 
-    private enum ToDo { cancel, upload };
-
-//  ==============================================================================================================================
-//  Action methods
-//  ==============================================================================================================================
-
-    public String doInitialize () {
-//  -----------------------------
-      return SUCCESS;
-    }
-
-    public String doUpload () {
-//  -------------------------
-      if (action == ToDo.cancel) return "cancel";
-      try {
-       File    udir = getRepositoryService().getDownloadDirectory(getConnectedUser());
-       String  path = udir.getPath() + "/" + uploadFileName;
-       File    file = new File(path);
-
-       if (!udir.exists()) udir.mkdir();
-       if (file.exists())  file.delete();
-       Do.copy(upload, file);
-        logger.info("Uploading \"" + uploadFileName + "\" " + uploadMimeType + " file.");
-        return next;
-      }
-      catch (OutOfMemoryError error) {
-       return "outofmemory";
-      }
-      catch (Exception error) {
-       logger.error("Reason: ", error);
-        return ERROR;
-      }
-    }
-//  ==============================================================================================================================
-//  Getters and setters
-//  ==============================================================================================================================
-
-    public String getIndex () {
-//  -------------------------
-      return index;
-    }
-    public String getFileName () {
-//  ----------------------------
-      return uploadFileName;
-    }
-    public String getNextAction () {
-//  ------------------------------
-      return next;
-    }
-
-    public void setCancel (boolean back) {
-//  ------------------------------------
-      this.action = ToDo.cancel;
-    }
-    public void setDoIt (boolean upload) {
-//  --------------------------------
-      this.action = ToDo.upload;
-    }
-    public void setIndex (String index) {
-//  -----------------------------------
-      this.index = index;
-    }
-    public void setNextAction (String next) {
-//  ---------------------------------------
-      this.next = next;
-    }
-    public void setUpload (File upload) {
-//  -----------------------------------
-      this.upload = upload;
-    }
-    public void setUploadFileName (String name) {
-//  -------------------------------------------
-      this.uploadFileName = name;
-    }
-    public void setUploadContentType (String mime) {
-//  ----------------------------------------------
-      this.uploadMimeType = mime;
-    }
+       /**
+        * Prepare form for the upload documents to study action.
+        * 
+        * @return SUCCESS
+        */
+       public String doInitializeStudy() {
+               initializationFullScreenContext(Constants.STUDY_MENU,
+                               Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
+                               Constants.STUDY_MENU);
+
+               return SUCCESS;
+       }
+       
+       /**
+        * Prepare form for the upload users action.
+        * @return SUCCESS
+        */
+       public String doInitializeSysAdmin() {
+               initializationFullScreenContext(Constants.SYSADMIN_MENU,
+                               Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
+                               Constants.OPEN);
+               setSimanContext("#Database_Management.htm");
+
+               return SUCCESS;
+       }
+
+       /**
+        * Store uploaded file into the user's download directory.
+        * 
+        * @return next action if ok, "cancel" if the operation is cancelled by user, "outofmemory" if there is no enough memory to upload the
+        *         file or ERROR otherwise
+        */
+       public String doUpload() {
+               initializationScreenContext(getMenuProperty(), Constants.STUDY_MENU,
+                               Constants.TRUE);
+
+               String res;
+               if (_action == ToDo.cancel) {
+                       res = "cancel";
+               } else {
+                       try {
+                               File udir = getRepositoryService().getDownloadDirectory(
+                                               getConnectedUser());
+                               String path = udir.getPath() + "/" + _uploadFileName;
+                               File file = new File(path);
+
+                               if (!udir.exists()) {
+                                       udir.mkdir();
+                               }
+                               if (file.exists()) {
+                                       file.delete();
+                               }
+                               Do.copy(_upload, file);
+                               LOG.info("Uploading \"" + _uploadFileName + "\" "
+                                               + _uploadMimeType + " file.");
+                               /*
+                                * if (next == null || next.isEmpty()) { next = "import"; }
+                                */
+
+                               res = _nextAction;
+                       } catch (OutOfMemoryError error) {
+
+                               initializationFullScreenContext(Constants.NONE,
+                                               Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
+                                               Constants.STUDY_MENU);
+
+                               setErrorCode("message.error.outofmemory");
+
+                               res = "outofmemory";
+                       } catch (Exception error) {
+                               LOG.error("Reason: ", error);
+                               res = ERROR;
+                       }
+               }
+               return res;
+       }
+
+       // ==============================================================================================================================
+       // Getters and setters
+       // ==============================================================================================================================
+
+       /**
+        * Get the document index to which the action applies.
+        * 
+        * @return the document index to which the action applies
+        */
+       public String getIndex() {
+               return _index;
+       }
+
+       /**
+        * Get the uploaded file name.
+        * 
+        * @return the uploaded file name
+        */
+       public String getFileName() {
+               return _uploadFileName;
+       }
+
+       /**
+        * Get fileName with url special symbols canceled for usage in struts.xml.
+        * @return the encoded uploaded file name.
+        */
+       public String getCanceledFileName() {
+               String res = _uploadFileName;
+               try {
+                       res = URLEncoder.encode(res, "ISO-8859-1");
+               } catch (UnsupportedEncodingException e) {
+                       LOG.error("Reason: ", e);
+               }
+               return res;
+       }
+       
+       /**
+        * Get the action to which the uploaded file is passed.
+        * 
+        * @return the action to which the uploaded file is passed.
+        */
+       public String getNextAction() {
+               return _nextAction;
+       }
+
+       /**
+        * Cancel the uploading.
+        * 
+        * @param back
+        */
+       public void setCancel(final boolean back) {
+               this._action = ToDo.cancel;
+       }
+
+       /**
+        * Set the flag to perform uploading.
+        * 
+        * @param upload
+        */
+       public void setDoIt(final boolean upload) {
+               this._action = ToDo.upload;
+       }
+
+       /**
+        * Set the document index to which the action applies.
+        * 
+        * @param index
+        *            the document index to which the action applies.
+        */
+       public void setIndex(final String index) {
+               this._index = index;
+       }
+
+       /**
+        * Set the action to which the uploaded file is passed.
+        * 
+        * @param next
+        *            the action to which the uploaded file is passed.
+        */
+       public void setNextAction(final String next) {
+               this._nextAction = next;
+       }
+
+       /**
+        * Set the uploaded file.
+        * 
+        * @param upload
+        *            the file
+        */
+       public void setUpload(final File upload) {
+               this._upload = upload;
+       }
+
+       /**
+        * Set the uploaded file name.
+        * 
+        * @param name
+        *            the uploaded file name
+        */
+       public void setUploadFileName(final String name) {
+               this._uploadFileName = name;
+       }
+
+       /**
+        * Set the mime type of the uploaded file.
+        * 
+        * @param mime
+        *            the mime type of the uploaded file
+        */
+       public void setUploadContentType(final String mime) {
+               this._uploadMimeType = mime;
+       }
 
        /**
         * Get the repositoryService.
+        * 
         * @return the repositoryService
         */
        public RepositoryService getRepositoryService() {
@@ -112,9 +267,11 @@ public class UploadAction extends Action {
 
        /**
         * Set the repositoryService.
-        * @param repositoryService the repositoryService to set
+        * 
+        * @param repositoryService
+        *            the repositoryService to set
         */
-       public void setRepositoryService(RepositoryService repositoryService) {
+       public void setRepositoryService(final RepositoryService repositoryService) {
                _repositoryService = repositoryService;
        }
 }
\ No newline at end of file