Salome HOME
18a58645fee615185da69a5cb23ad319ca49ce99
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / UploadAction.java
1 package org.splat.simer;
2
3 import java.io.File;
4
5 import org.splat.kernel.Do;
6 import org.splat.som.Database;
7
8
9 public class UploadAction extends Action {
10
11         private File    upload         = null;
12         private String  uploadMimeType = null;
13     private String  uploadFileName = null;
14
15     private ToDo    action;                  // Action to do
16     private String  next           = null;   // Action to which the uploaded file is passed
17         private String  index          = null;   // Depending on the next action, document index to which the action applies
18
19         private static final long serialVersionUID = 6003880772275115923L;
20
21     private enum ToDo { cancel, upload };
22
23 //  ==============================================================================================================================
24 //  Action methods
25 //  ==============================================================================================================================
26
27     public String doInitialize () {
28 //  -----------------------------
29       return SUCCESS;
30     }
31
32     public String doUpload () {
33 //  -------------------------
34       if (action == ToDo.cancel) return "cancel";
35       try {
36         File    udir = Database.getDownloadDirectory(getConnectedUser());
37         String  path = udir.getPath() + "/" + uploadFileName;
38         File    file = new File(path);
39
40         if (!udir.exists()) udir.mkdir();
41         if (file.exists())  file.delete();
42         Do.copy(upload, file);
43         logger.info("Uploading \"" + uploadFileName + "\" " + uploadMimeType + " file.");
44         return next;
45       }
46       catch (OutOfMemoryError error) {
47         return "outofmemory";
48       }
49       catch (Exception error) {
50         logger.error("Reason: ", error);
51         return ERROR;
52       }
53     }
54 //  ==============================================================================================================================
55 //  Getters and setters
56 //  ==============================================================================================================================
57
58     public String getIndex () {
59 //  -------------------------
60       return index;
61     }
62     public String getFileName () {
63 //  ----------------------------
64       return uploadFileName;
65     }
66     public String getNextAction () {
67 //  ------------------------------
68       return next;
69     }
70
71     public void setCancel (boolean back) {
72 //  ------------------------------------
73       this.action = ToDo.cancel;
74     }
75     public void setDoIt (boolean upload) {
76 //  --------------------------------
77       this.action = ToDo.upload;
78     }
79     public void setIndex (String index) {
80 //  -----------------------------------
81       this.index = index;
82     }
83     public void setNextAction (String next) {
84 //  ---------------------------------------
85       this.next = next;
86     }
87     public void setUpload (File upload) {
88 //  -----------------------------------
89       this.upload = upload;
90     }
91     public void setUploadFileName (String name) {
92 //  -------------------------------------------
93       this.uploadFileName = name;
94     }
95     public void setUploadContentType (String mime) {
96 //  ----------------------------------------------
97       this.uploadMimeType = mime;
98     }
99 }