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 49651266f32ceab6c92c2f00d6d4ab40bbd459e1..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;
 
-    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
        /**
-        * Injected repository service.
+        * Uploaded file.
         */
-       private RepositoryService _repositoryService;
-       
+       private transient File _upload = null;
        /**
-        * Value of the menu property. 
-        * It can be: none, create, open, study, knowledge, sysadmin, help.
+        * Mime type of the uploaded file.
         */
-       private String _menuProperty;
-       
+       private transient String _uploadMimeType = null;
        /**
-        * Value of the title bar property. 
-        * It can be: study, knowledge.
+        * Uploaded file name.
         */
-       private String _titleProperty;
-       
+       private transient String _uploadFileName = null;
+
        /**
-        * Value of the tool bar property. 
-        * It can be: none, standard, study, back.
+        * Action to do.
         */
-       private String _toolProperty;
-       
+       private transient ToDo _action;
        /**
-        * Value of the left menu property. 
-        * It can be: open, study, knowledge, scenario.
+        * Action to which the uploaded file is passed.
         */
-       private String _leftMenuProperty;
+       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;
+
+       /**
+        * Action modes enumeration.
+        */
+       private enum ToDo {
+               /**
+                * Cancel the operation.
+                */
+               cancel,
+               /**
+                * Perform uploading.
+                */
+               upload
+       };
+
+       // ==============================================================================================================================
+       // Action methods
+       // ==============================================================================================================================
+
+       /**
+        * 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;
+       }
        
        /**
-        * Property that indicates whether the current open study is editable or not.
-        * On the screen it looks like pen on the status icon, pop-up menu also can be called.
-        * It is necessary for correct building the title bar.
+        * Prepare form for the upload users action.
+        * @return SUCCESS
         */
-       private String _editDisabledProperty = "false";
+       public String doInitializeSysAdmin() {
+               initializationFullScreenContext(Constants.SYSADMIN_MENU,
+                               Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
+                               Constants.OPEN);
+               setSimanContext("#Database_Management.htm");
 
-       private static final long serialVersionUID = 6003880772275115923L;
+               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"; }
+                                */
 
-    private enum ToDo { cancel, upload };
-
-//  ==============================================================================================================================
-//  Action methods
-//  ==============================================================================================================================
-
-    public String doInitialize () {
-//  -----------------------------
-       setMenuProperty("study");
-       setToolProperty("none");
-       setLeftMenuProperty("open");
-               initializationFullScreenContext(_menuProperty, _toolProperty, _leftMenuProperty);
-               
-      return SUCCESS;
-    }
-
-    public String doUpload () {
-//  -------------------------
-      setMenuProperty("study");
-      setTitleProperty("study");
-      setEditDisabledProperty("true");
-      initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
-      
-      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);
-        LOG.info("Uploading \"" + uploadFileName + "\" " + uploadMimeType + " file.");
-        /*if (next == null || next.isEmpty()) {
-               next = "import";
-        }*/
-        
-        return next;
-      }
-      catch (OutOfMemoryError error) {
-         
-       setMenuProperty("none");
-       setTitleProperty("study");
-       setEditDisabledProperty("true");
-       setToolProperty("none");
-       setLeftMenuProperty("study");
-        initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
-               
-       return "outofmemory";
-      }
-      catch (Exception error) {
-       LOG.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;
-    }
+                               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 repositoryService.
-        * @return the repositoryService
+        * Get the document index to which the action applies.
+        * 
+        * @return the document index to which the action applies
         */
-       public RepositoryService getRepositoryService() {
-               return _repositoryService;
+       public String getIndex() {
+               return _index;
        }
 
        /**
-        * Set the repositoryService.
-        * @param repositoryService the repositoryService to set
+        * Get the uploaded file name.
+        * 
+        * @return the uploaded file name
         */
-       public void setRepositoryService(RepositoryService repositoryService) {
-               _repositoryService = repositoryService;
+       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 menuProperty.
-        * @return the menuProperty
+        * Get the action to which the uploaded file is passed.
+        * 
+        * @return the action to which the uploaded file is passed.
         */
-       public String getMenuProperty() {
-               return _menuProperty;
+       public String getNextAction() {
+               return _nextAction;
        }
 
        /**
-        * Set the menuProperty.
-        * @param menuProperty the menuProperty to set
+        * Cancel the uploading.
+        * 
+        * @param back
         */
-       public void setMenuProperty(String menuProperty) {
-               this._menuProperty = menuProperty;
+       public void setCancel(final boolean back) {
+               this._action = ToDo.cancel;
        }
-       
+
        /**
-        * Get the _titleProperty.
-        * @return the _titleProperty
+        * Set the flag to perform uploading.
+        * 
+        * @param upload
         */
-       public String getTitleProperty() {
-               return _titleProperty;
+       public void setDoIt(final boolean upload) {
+               this._action = ToDo.upload;
        }
 
        /**
-        * Set the _titleProperty.
-        * @param _titleProperty the titleProperty to set
+        * Set the document index to which the action applies.
+        * 
+        * @param index
+        *            the document index to which the action applies.
         */
-       public void setTitleProperty(String titleProperty) {
-               _titleProperty = titleProperty;
+       public void setIndex(final String index) {
+               this._index = index;
        }
 
        /**
-        * Get the _editDisabledProperty.
-        * @return the _editDisabledProperty
+        * Set the action to which the uploaded file is passed.
+        * 
+        * @param next
+        *            the action to which the uploaded file is passed.
         */
-       public String getEditDisabledProperty() {
-               return _editDisabledProperty;
+       public void setNextAction(final String next) {
+               this._nextAction = next;
        }
 
        /**
-        * Set the _editDisabledProperty.
-        * @param _editDisabledProperty the _editDisabledProperty to set
+        * Set the uploaded file.
+        * 
+        * @param upload
+        *            the file
         */
-       public void setEditDisabledProperty(String _editDisabledProperty) {
-               this._editDisabledProperty = _editDisabledProperty;
+       public void setUpload(final File upload) {
+               this._upload = upload;
        }
 
        /**
-        * Get the toolProperty.
-        * @return the toolProperty
+        * Set the uploaded file name.
+        * 
+        * @param name
+        *            the uploaded file name
         */
-       public String getToolProperty() {
-               return _toolProperty;
+       public void setUploadFileName(final String name) {
+               this._uploadFileName = name;
        }
 
        /**
-        * Set the toolProperty.
-        * @param toolProperty the toolProperty to set
+        * Set the mime type of the uploaded file.
+        * 
+        * @param mime
+        *            the mime type of the uploaded file
         */
-       public void setToolProperty(final String toolProperty) {
-               _toolProperty = toolProperty;
+       public void setUploadContentType(final String mime) {
+               this._uploadMimeType = mime;
        }
-       
+
        /**
-        * Get the leftMenuProperty.
-        * @return the leftMenuProperty
+        * Get the repositoryService.
+        * 
+        * @return the repositoryService
         */
-       public String getLeftMenuProperty() {
-               return _leftMenuProperty;
+       public RepositoryService getRepositoryService() {
+               return _repositoryService;
        }
 
        /**
-        * Set the leftMenuProperty.
-        * @param leftMenuProperty the leftMenuProperty to set
+        * Set the repositoryService.
+        * 
+        * @param repositoryService
+        *            the repositoryService to set
         */
-       public void setLeftMenuProperty(final String leftMenuProperty) {
-               _leftMenuProperty = leftMenuProperty;
+       public void setRepositoryService(final RepositoryService repositoryService) {
+               _repositoryService = repositoryService;
        }
 }
\ No newline at end of file