KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scioworks > imap > presentation > imapWeb > FileUploadSessionData


1 /* -----------------------------------------------------------------------------
2  * Copyright (c) 2001, Low Kin Onn
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *
8  * Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * Neither name of the Scioworks Pte. Ltd. nor the names of its contributors
16  * may beused to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * -----------------------------------------------------------------------------
31  */

32
33 package scioworks.imap.presentation.imapWeb;
34
35 import java.util.Vector JavaDoc;
36
37 import scioworks.imap.spec.beans.*;
38
39
40 public class FileUploadSessionData implements java.io.Serializable JavaDoc {
41
42   public static String JavaDoc SESSION_KEY = "FileUploadSessionData";
43
44   private Vector JavaDoc fFileList;
45   private int fFileMaxCount;
46   private int fTotalSize;
47
48   public FileUploadSessionData(int fileMaxCount, int totalSize) {
49     fFileMaxCount = fileMaxCount;
50     fTotalSize = totalSize;
51     fFileList = new Vector JavaDoc(fFileMaxCount);
52   
53   }
54
55   private int getCurrentSize() {
56     int size = 0;
57     for (int i=0; i<fFileList.size(); i++) {
58       size = size + ((FileRecord)fFileList.elementAt(i)).getSize();
59     }
60     return size;
61   }
62
63   public boolean isExceedFileCount() {
64     if ((fFileList.size()+1) > fFileMaxCount) {
65       return true;
66     } else {
67       return false;
68     }
69   }
70
71   private boolean isExceedLimit(int size) {
72     if ( ((fFileList.size()+1) > fFileMaxCount) ||
73          ((getCurrentSize()+size) > fTotalSize) ) {
74       return true;
75     } else {
76       return false;
77     }
78   }
79
80   public Vector JavaDoc getFileList() {
81     return new Vector JavaDoc(fFileList);
82   }
83
84   public boolean addFile(String JavaDoc tmpFilename, String JavaDoc filename, int size,
85       String JavaDoc contentType) {
86     if (!isExceedLimit(size)) {
87         
88     fFileList.addElement(FileRecordFactory.getFileRecord("scioworks.imap.business.beans.FileRecordImpl",tmpFilename,filename,size,contentType));
89     
90       // ok to add
91
return true;
92     } else {
93       // can't add anymore
94
return false;
95     }
96   }
97
98   public boolean removeFile(int index) {
99     if (index >= fFileList.size()) {
100       return false;
101     } else {
102       fFileList.remove(index);
103       return true;
104     }
105   }
106
107   public int getFileCount() {
108     return fFileList.size();
109   }
110
111   public String JavaDoc getTmpFilename(int index) {
112     return ((FileRecord)fFileList.elementAt(index)).getTmpFilename();
113   }
114
115   public String JavaDoc getFilename(int index) {
116     return ((FileRecord)fFileList.elementAt(index)).getFilename();
117   }
118
119   public int getFilesize(int index) {
120     return ((FileRecord)fFileList.elementAt(index)).getSize();
121   }
122
123   public String JavaDoc toString() {
124     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
125     FileRecord fr = null;
126     for (int i=0; i<fFileList.size(); i++) {
127       fr = (FileRecord)fFileList.elementAt(i);
128       buf.append( fr.toString() );
129     }
130     return buf.toString();
131   }
132 }
Popular Tags