KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > business > library > struts > UploadImagesAction


1 /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * -----------------------------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * -----------------------------------------------------------------------------
30  * $Log: UploadImagesAction.java,v $
31  * Revision 1.3 2005/04/10 20:31:58 colinmacleod
32  * Added new themes.
33  * Changed id type to String.
34  * Changed i tag to em and b tag to strong.
35  * Improved PicoContainerFactory with NanoContainer scripts.
36  *
37  * Revision 1.2 2005/04/09 17:19:46 colinmacleod
38  * Changed copyright text to GPL v2 explicitly.
39  *
40  * Revision 1.1.1.1 2005/03/10 17:52:01 colinmacleod
41  * Restructured ivata op around Hibernate/PicoContainer.
42  * Renamed ivata groupware.
43  *
44  * Revision 1.9 2004/12/31 18:27:44 colinmacleod
45  * Added MaskFactory to constructor of MaskAction.
46  *
47  * Revision 1.8 2004/12/23 21:01:29 colinmacleod
48  * Updated Struts to v1.2.4.
49  * Changed base classes to use ivata masks.
50  *
51  * Revision 1.7 2004/11/12 18:19:15 colinmacleod
52  * Change action and form classes to extend MaskAction, MaskForm respectively.
53  *
54  * Revision 1.6 2004/11/03 15:31:51 colinmacleod
55  * Change method interfaces to remove log.
56  *
57  * Revision 1.5 2004/07/13 19:47:29 colinmacleod
58  * Moved project to POJOs from EJBs.
59  * Applied PicoContainer to services layer (replacing session EJBs).
60  * Applied Hibernate to persistence layer (replacing entity EJBs).
61  *
62  * Revision 1.4 2004/03/21 21:16:29 colinmacleod
63  * Shortened name to ivata op.
64  *
65  * Revision 1.3 2004/02/10 19:57:24 colinmacleod
66  * Changed email address.
67  *
68  * Revision 1.2 2004/02/01 22:07:31 colinmacleod
69  * Added full names to author tags
70  *
71  * Revision 1.1.1.1 2004/01/27 20:58:42 colinmacleod
72  * Moved ivata openportal to SourceForge..
73  *
74  * Revision 1.4 2003/11/13 16:11:08 jano
75  * commitng everything to CVS
76  * can deploy and application is ruuning, can login into
77  *
78  * Revision 1.3 2003/10/28 13:16:14 jano
79  * commiting library,
80  * still fixing compile and building openGroupware project
81  *
82  * Revision 1.2 2003/10/15 14:16:53 colin
83  * fixing for XDoclet
84  *
85  * Revision 1.2 2003/07/16 14:47:04 jano
86  * quotaHandling for uploading images during Uploading HTML
87  * Revision 1.1 2003/07/08 06:31:33 peter
88  * added to cvs
89  * -----------------------------------------------------------------------------
90  */

91 package com.ivata.groupware.business.library.struts;
92
93 import javax.servlet.http.HttpServletRequest JavaDoc;
94 import javax.servlet.http.HttpServletResponse JavaDoc;
95 import javax.servlet.http.HttpSession JavaDoc;
96
97 import org.apache.struts.action.ActionErrors;
98 import org.apache.struts.action.ActionForm;
99 import org.apache.struts.action.ActionMapping;
100
101 import com.ivata.mask.MaskFactory;
102 import com.ivata.mask.util.SystemException;
103 import com.ivata.mask.web.struts.MaskAction;
104 import com.ivata.mask.web.struts.MaskAuthenticator;
105
106
107 /**
108  * <p>Library image upload action. It's used when there are some local
109  * images
110  * in the uploaded HTML for library document.</p>
111  *
112  * @since 2003-07-04
113  * @author Peter Illes
114  * @version $Revision: 1.3 $
115  */

116 public class UploadImagesAction extends MaskAction {
117     /**
118      * <p>
119      * Constructor. Called by <strong>PicoContainer.</strong>.
120      * </p>
121      *
122      * @param maskFactory This factory is needed to access the masks and groups
123      * of masks.
124      * @param authenticator used to confirm whether or not the
125      * user should be allowed to continue, in the <code>execute</code> method.
126      */

127     public UploadImagesAction(MaskFactory maskFactory, MaskAuthenticator authenticator) {
128         super(maskFactory, authenticator);
129     }
130     /**
131      * <p>Called from the other <code>execute</code> method, this can
132      * be overridden by each subclass to provide the <em>ivata</em>-specific
133      * processing required.</p>
134      *
135      * @param mapping The ActionMapping used to select this instance.
136      * @param log valid logging object to write messages to.
137      * @param errors valid errors object to append errors to. If there are
138      * any errors, the action will return to the input.
139      * @param form optional ActionForm bean for this request (if any)
140      * @param request non-HTTP request we are processing
141      * @param response The non-HTTP response we are creating
142      * @param session returned from the <code>request</code> parameter.
143      * @param userName valid, non-null user name from session.
144      * @param settings valid, non-null settings from session.
145      * @exception SystemException if there is any problem which
146      * prevents processing. It will result in the webapp being forwarded
147      * to
148      * the standard error page.
149      * @return this method returns the string used to identify the correct
150      * <em>Struts</em> <code>ActionForward</code> which should follow this
151      * page, or <code>null</code> if it should return to the input.
152      */

153     public String JavaDoc execute(final ActionMapping mapping,
154             final ActionErrors errors,
155             final ActionForm form,
156             final HttpServletRequest JavaDoc request,
157             final HttpServletResponse JavaDoc response,
158             final HttpSession JavaDoc session) throws SystemException {
159         return null;
160     }
161
162     /**
163      * <p>This method is called if the ok or apply buttons are
164      * pressed.</p>
165      *
166      * @param mapping The ActionMapping used to select this instance.
167      * @param log valid logging object to write messages to.
168      * @param errors valid errors object to append errors to. If there are
169      * any errors, the action will return to the input.
170      * @param form optional ActionForm bean for this request (if any)
171      * @param request non-HTTP request we are processing
172      * @param response The non-HTTP response we are creating
173      * @param session returned from the <code>request</code> parameter.
174      * @param userName valid, non-null user name from session.
175      * @param settings valid, non-null settings from session.
176      * @param ok <code>true</code> if the ok button was pressed, otherwise
177      * <code>false</code> if the apply button was pressed.
178      * @exception SystemException if there is any problem which
179      * prevents processing. It will result in the webapp being forwarded
180      * to
181      * the standard error page.
182      * @return this method returns the string used to identify the correct
183      * <em>Struts</em> <code>ActionForward</code> which should follow this
184      * page, or <code>null</code> if it should return to the input.
185      */

186     public String JavaDoc onConfirm(final ActionMapping mapping,
187             final ActionErrors errors,
188             final ActionForm form,
189             final HttpServletRequest JavaDoc request,
190             final HttpServletResponse JavaDoc response,
191             final HttpSession JavaDoc session,
192             final String JavaDoc defaultForward) throws SystemException {
193         UploadImagesForm uploadForm = (UploadImagesForm) form;
194
195 /* TODO DriveRemote drive = getDriveFromSession(session);
196         UsageRemote usage = getUsageFromSession(session);
197
198         Map images = uploadForm.getImages();
199
200         // only worth to do something when there's at least one file uploaded
201         if (!images.isEmpty()) {
202             Vector fileNameList = new Vector();
203
204             try {
205                 int finalSize = 0;
206
207                 // count fileSize together and check if we can upload
208                 for (Iterator i = images.keySet().iterator(); i.hasNext();) {
209                     String currentKey = (String) i.next();
210
211                     FormFile formFile = (FormFile) images.get(currentKey);
212
213                     // ignore empty files and files already uploaded
214                     if ((formFile != null) && (formFile.getFileSize() > 0) &&
215                             !fileNameList.contains(formFile.getFileName())) {
216                         finalSize += formFile.getFileSize();
217                     }
218                 }
219
220                 Integer canUpload = null;
221
222                 try {
223                     canUpload = usage.canUpload(new Integer(finalSize / 1024),
224                             (String) session.getAttribute("userName"));
225                 } catch (java.rmi.RemoteException e) {
226                     throw new RuntimeException(e);
227                 }
228
229                 ResourceBundle adminBundle = ResourceBundle.getBundle("com.ivata.groupware.business.ApplicationResources",
230                         (Locale) request.getSession().getAttribute(Globals.LOCALE_KEY));
231
232                 if (canUpload.equals(
233                             com.ivata.groupware.admin.usage.UsageConstants.CAN_UPLOAD_NEARLY_RUN_OUT_OF_QUOTA) ||
234                         canUpload.equals(
235                             com.ivata.groupware.admin.usage.UsageConstants.OK)) {
236                     for (Iterator i = images.keySet().iterator(); i.hasNext();) {
237                         String currentKey = (String) i.next();
238
239                         FormFile formFile = (FormFile) images.get(currentKey);
240
241                         // ignore empty files and files already uploaded
242                         if ((formFile != null) && (formFile.getFileSize() > 0) &&
243                                 !fileNameList.contains(formFile.getFileName())) {
244                             InputStream inStream = formFile.getInputStream();
245
246                             // reading the content, writing it to an output stream
247                             ByteArrayOutputStream outStream = new ByteArrayOutputStream();
248                             int oneByte = 0;
249
250                             while ((oneByte = inStream.read()) != -1) {
251                                 outStream.write(oneByte);
252                             }
253
254                             // storing the content to the fileContentDO
255                             SerializedByteArray fileContent = new SerializedByteArray(outStream.toByteArray());
256                             FileContentDO fileContentDO = new FileContentDO(fileContent,
257                                     formFile.getContentType());
258
259                             // store the file server-side, get the updated list
260                             drive.uploadFile(fileContentDO,
261                                 formFile.getFileName(),
262                                 uploadForm.getComment(currentKey), userName);
263
264                             fileNameList.add(formFile.getFileName());
265                         }
266
267                         if (canUpload.equals(
268                                     com.ivata.groupware.admin.usage.UsageConstants.CAN_UPLOAD_NEARLY_RUN_OUT_OF_QUOTA)) {
269                             request.setAttribute("javaScript",
270                                 "alert(\"" +
271                                 adminBundle.getString(
272                                     "errors.upload.quota.runOut.nearly") +
273                                 "\");");
274                         }
275                     }
276                 } else if (canUpload.equals(
277                             com.ivata.groupware.admin.usage.UsageConstants.NOT_FREE_SPACE_FOR_UPLOAD)) {
278                     request.setAttribute("javaScript",
279                         "alert(\"" +
280                         adminBundle.getString("errors.upload.quota.runOut") +
281                         "\");");
282                 }
283
284                 // if there were some uploads, move them to drive upload directory
285                 if (!fileNameList.isEmpty()) {
286                     drive.moveUploads(fileNameList, "drive", userName);
287                 }
288             } catch (java.rmi.RemoteException e) {
289                 throw new SystemException(e);
290             } catch (FileNotFoundException e) {
291                 throw new SystemException(e);
292             } catch (IOException e) {
293                 throw new SystemException(e);
294             }
295         }
296
297         request.setAttribute("openerPage", "/library/submit.action");
298 */

299         return "utilClosePopUp";
300     }
301 }
302
Popular Tags