KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > business > mail > struts > ComposeForm


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: ComposeForm.java,v $
31  * Revision 1.3 2005/04/10 18:47:43 colinmacleod
32  * Changed i tag to em and b tag to strong.
33  *
34  * Revision 1.2 2005/04/09 17:20:01 colinmacleod
35  * Changed copyright text to GPL v2 explicitly.
36  *
37  * Revision 1.1.1.1 2005/03/10 17:51:19 colinmacleod
38  * Restructured ivata op around Hibernate/PicoContainer.
39  * Renamed ivata groupware.
40  *
41  * Revision 1.7 2004/12/23 21:01:34 colinmacleod
42  * Updated Struts to v1.2.4.
43  * Changed base classes to use ivata masks.
44  *
45  * Revision 1.6 2004/11/12 18:19:16 colinmacleod
46  * Change action and form classes to extend MaskAction, MaskForm respectively.
47  *
48  * Revision 1.5 2004/11/12 15:57:25 colinmacleod
49  * Removed dependencies on SSLEXT.
50  * Moved Persistence classes to ivata masks.
51  *
52  * Revision 1.4 2004/07/13 19:48:12 colinmacleod
53  * Moved project to POJOs from EJBs.
54  * Applied PicoContainer to services layer (replacing session EJBs).
55  * Applied Hibernate to persistence layer (replacing entity EJBs).
56  *
57  * Revision 1.3 2004/03/21 21:16:39 colinmacleod
58  * Shortened name to ivata op.
59  *
60  * Revision 1.2 2004/02/01 22:07:32 colinmacleod
61  * Added full names to author tags
62  *
63  * Revision 1.1.1.1 2004/01/27 20:59:59 colinmacleod
64  * Moved ivata openportal to SourceForge..
65  *
66  * Revision 1.2 2003/10/15 14:11:33 colin
67  * fixing for XDoclet
68  *
69  * Revision 1.5 2003/05/14 09:44:53 peter
70  * removing unwanted attachments
71  *
72  * Revision 1.4 2003/05/12 16:32:02 peter
73  * attachment compose changes
74  *
75  * Revision 1.3 2003/03/05 18:10:12 colin
76  * now gets mail from MailAction (rather than session directly)
77  *
78  * Revision 1.2 2003/02/25 11:53:33 colin
79  * bugfixes and minor restructuring
80  *
81  * Revision 1.1 2003/02/24 19:09:24 colin
82  * moved to business
83  *
84  * Revision 1.4 2003/02/20 20:26:32 colin
85  * improved validation by adding ValidationField and ValidationException
86  *
87  * Revision 1.3 2003/02/04 17:39:10 colin
88  * updated for new MaskAction interface
89  *
90  * Revision 1.2 2002/11/17 20:04:36 colin
91  * changed folder to folderName
92  *
93  * Revision 1.1 2002/11/12 11:46:32 colin
94  * first version in CVS. applied Struts to mail subsystem.
95  * -----------------------------------------------------------------------------
96  */

97 package com.ivata.groupware.business.mail.struts;
98
99 import java.util.Iterator JavaDoc;
100
101 import javax.mail.MessagingException JavaDoc;
102 import javax.servlet.http.HttpServletRequest JavaDoc;
103 import javax.servlet.http.HttpSession JavaDoc;
104
105 import org.apache.log4j.Logger;
106 import org.apache.struts.action.ActionMapping;
107
108 import com.ivata.groupware.admin.security.server.SecuritySession;
109 import com.ivata.groupware.business.drive.file.FileDO;
110 import com.ivata.groupware.business.mail.Mail;
111 import com.ivata.groupware.business.mail.message.MessageDO;
112 import com.ivata.mask.Mask;
113 import com.ivata.mask.util.CollectionHandling;
114 import com.ivata.mask.util.StringHandling;
115 import com.ivata.mask.util.SystemException;
116 import com.ivata.mask.validation.ValidationErrors;
117 import com.ivata.mask.web.struts.DialogForm;
118
119
120 /**
121  * <p>Main form used to input/display data in
122  * <code>/mail/compose.jsp</code>.</p>
123  *
124  * @since 2002-11-09
125  * @author Colin MacLeod
126  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
127  * @version $Revision: 1.3 $
128  */

129 public class ComposeForm extends DialogForm {
130     /**
131      * <p>Set when attachments were uploaded to the temporary upload
132      * directory, contains the fileNames separated by
133      * <code>File.SeparatorChar</code>.</p>
134      */

135     private String JavaDoc attach;
136     /**
137      * <p>
138      * Defines the base class of all objects in the value object list.
139      * </p>
140      */

141     private Class JavaDoc baseClass;
142     /**
143      * Refer to {@link Logger}.
144      */

145     private Logger log = Logger.getLogger(ComposeForm.class);
146     Mail mail;
147     /**
148      * <p>
149      * Mask containing all the field definitions for this list.
150      * </p>
151      */

152     private Mask mask;
153
154     /**
155      * <p>Message data object which contains all of the
156      * information in this form.</p>
157      */

158     private MessageDO message = new MessageDO();
159     /**
160      * Stores any exception encountered when sending the mail.
161      */

162     private MessagingException JavaDoc messagingException = null;
163     /**
164      * @param maskParam Refer to {@link DialogForm#DialogForm}.
165      * @param baseClassParam Refer to {@link DialogForm#DialogForm}.
166      */

167     public ComposeForm(final Mail mail) {
168         this.mail = mail;
169     }
170
171     /**
172      * <p>
173      * Return all form state to initial values.
174      * </p>
175      *
176      * @see com.ivata.mask.web.struts.MaskForm#clear()
177      */

178     protected void clear() {
179         attach = null;
180     }
181
182     /**
183      * <p>Set when attachments were uploaded to the temporary upload
184      * directory, contains the fileNames separated by
185      * <code>File.SeparatorChar</code>.</p>
186      *
187      * @return the current value of attach.
188      */

189     public final String JavaDoc getAttach() {
190         return attach;
191     }
192
193     /**
194      * <p>This map contains names of files to remove from the attachment
195      * list</p>
196      * @param attachmentId the fileName or attachmentId of the attachment
197      * to remove
198      * @return the remove status for the specified attachment.
199      */

200     public final boolean getAttachmentRemove(final String JavaDoc attachmentId) {
201         // always return false, a file in the list wants to stay there...
202
return false;
203     }
204
205     /**
206      * <p>
207      * Defines the base class of all objects in the value object list.
208      * </p>
209      *
210      * @return base class of all objects in the value object list.
211      */

212     public final Class JavaDoc getBaseClass() {
213         return baseClass;
214     }
215
216     /**
217      * <p>
218      * Mask containing all the field definitions for this list.
219      * </p>
220      *
221      * @return mask containing all the field definitions for this list.
222      */

223     public final Mask getMask() {
224         return mask;
225     }
226
227     /**
228      * <p>Get the message data object which contains all of the
229      * information in this form.</p>
230      *
231      * @return message data object which contains all of the
232      * information in this form.
233      */

234     public MessageDO getMessage() {
235         return message;
236     }
237
238     /**
239      * @return Returns the messagingException.
240      */

241     public MessagingException JavaDoc getMessagingException() {
242         return messagingException;
243     }
244
245     /**
246      * <p>Get recipients of the message.</p>
247      *
248      * @return recipients of the message, as a <code>String</code>
249      * containing all of the instances separated by semicolons (;), each
250      * one formatted according to <a
251      * HREF='http://www.faqs.org/rfcs/rfc822.HTML'>RFC822</a>.
252      */

253     public final String JavaDoc getRecipients() {
254         return CollectionHandling.convertToLines(message.getRecipients(), ';');
255     }
256
257     /**
258      * <p>Get "Blind carbon copy" recipients as List of strings. These are
259      * additional recipients whose identity is <em>not</em> made known to
260      * any
261      * other recipients.</p>
262      *
263      * @return recipients of the message, as a <code>String</code>
264      * containing all of the instances separated by semicolons (;), each
265      * one formatted according to <a
266      * HREF='http://www.faqs.org/rfcs/rfc822.HTML'>RFC822</a>.
267      *
268      */

269     public final String JavaDoc getRecipientsBCC() {
270         return CollectionHandling.convertToLines(message.getRecipientsBCC(), ';');
271     }
272
273     /**
274      * <p>Get "Carbon copy" recipients of the message. These are
275      * additional
276      * recipients whose identity is made known to all other
277      * recipients.</p>
278      *
279      * @return recipients of the message, as a <code>String</code>
280      * containing all of the instances separated by semicolons (;), each
281      * one formatted according to <a
282      * HREF='http://www.faqs.org/rfcs/rfc822.HTML'>RFC822</a>.
283      *
284      */

285     public final String JavaDoc getRecipientsCC() {
286         return CollectionHandling.convertToLines(message.getRecipientsCC(), ';');
287     }
288
289     /**
290      * <p>Get senders of the message.</p>
291      *
292      * @return senders of the message, as a <code>String</code>
293      * containing all of the instances separated by semicolons (;), each
294      * one formatted according to <a
295      * HREF='http://www.faqs.org/rfcs/rfc822.HTML'>RFC822</a>.
296      */

297     public final String JavaDoc getSenders() {
298         return CollectionHandling.convertToLines(message.getSenders(), ';');
299     }
300
301     /**
302      * <p>Reset all bean properties to their default state. This method
303      * is called before the properties are repopulated by the controller
304      * servlet.<p>
305      *
306      * @param mapping The mapping used to select this instance
307      * @param request The servlet request we are processing
308      */

309     public void reset(final ActionMapping mapping,
310             final HttpServletRequest JavaDoc request) {
311         // ok, it's not very memory-efficient, but it's easier to maintain :-)
312
super.reset(mapping, request);
313         attach = null;
314     }
315
316     /**
317      * <p>Set when attachments were uploaded to the temporary upload
318      * directory, contains the fileNames separated by
319      * <code>File.SeparatorChar</code>.</p>
320      *
321      * @param attach the new value of attach.
322      */

323     public final void setAttach(final String JavaDoc attach) {
324         this.attach = attach;
325     }
326
327     /**
328      * <p>This map contains names of attachments to remove from the message</p>
329      *
330      * @param attachmentId the attachment to remove from the message
331      * @param remove when <code>true</code>, the attachment should be removed.
332      */

333     public final void setAttachmentRemove(final String JavaDoc attachmentId,
334             final boolean remove) {
335         // if there's an attachment marked to remove, remove the corresponding
336
// attachment from the list
337
if (remove && (message.getAttachments() != null)) {
338             int attachmentIdHashCode = Integer.parseInt(attachmentId);
339
340             for (Iterator JavaDoc i = message.getAttachments().iterator(); i.hasNext();) {
341                 FileDO currentDO = (FileDO) i.next();
342                 String JavaDoc currentId = currentDO.getName();
343
344                 if (currentId.hashCode() == attachmentIdHashCode) {
345                     message.getAttachments().remove(currentDO);
346
347                     break;
348                 }
349             }
350         }
351     }
352
353     /**
354      * <p>Set the message data object which contains all of the
355      * information in this form. The contents of the form are entirely
356      * replaced by the contents of the messge provided.</p>
357      *
358      * @param message message data object which contains all of
359      * the new information in this form.
360      */

361     public final void setMessage(final MessageDO message) {
362         this.message = message;
363     }
364     /**
365      * @param messagingExceptionParam The messagingException to set.
366      */

367     public void setMessagingException(MessagingException JavaDoc messagingExceptionParam) {
368         if (log.isDebugEnabled()) {
369             log.debug("setMessagingException before: '" + messagingException
370                     + "', after: '" + messagingExceptionParam + "'");
371         }
372
373         messagingException = messagingExceptionParam;
374     }
375
376     /**
377      * <p>Set recipients of the message.</p>
378      *
379      * @param recipients recipients of the message, as a
380      * <code>String</code> containing all of the instances separated by
381      * semicolons (;), each one formatted according to <a
382      * HREF='http://www.faqs.org/rfcs/rfc822.HTML'>RFC822</a>.
383      */

384     public final void setRecipients(final String JavaDoc recipients) {
385         message.setRecipients(CollectionHandling.convertFromLines(recipients,
386                 ";"));
387     }
388
389     /**
390      * <p>Set "Blind carbon copy" recipients as List of strings. These are
391      * additional recipients whose identity is <em>not</em> made known to
392      * any
393      * other recipients.</p>
394      *
395      * @param recipientsBCC recipients of the message, as a
396      * <code>String</code> containing all of the instances separated by
397      * semicolons (;), each one formatted according to <a
398      * HREF='http://www.faqs.org/rfcs/rfc822.HTML'>RFC822</a>.
399      */

400     public final void setRecipientsBCC(final String JavaDoc recipientsBCC) {
401         message.setRecipientsBCC(CollectionHandling.convertFromLines(
402                 recipientsBCC, ";"));
403     }
404
405     /**
406      * <p>Set "Carbon copy" recipients of the message. These are
407      * additional
408      * recipients whose identity is made known to all other
409      * recipients.</p>
410      *
411      * @param recipientsCC recipients of the message, as a
412      * <code>String</code> containing all of the instances separated by
413      * semicolons (;), each one formatted according to <a
414      * HREF='http://www.faqs.org/rfcs/rfc822.HTML'>RFC822</a>.
415      */

416     public final void setRecipientsCC(final String JavaDoc recipientsCC) {
417         message.setRecipientsCC(CollectionHandling.convertFromLines(
418                 recipientsCC, ";"));
419     }
420
421     /**
422      * <p>Set senders of the message.</p>
423      *
424      * @param senders senders of the message, as a <code>String</code>
425      * containing all of the instances separated by semicolons (;), each
426      * one formatted according to <a
427      * HREF='http://www.faqs.org/rfcs/rfc822.HTML'>RFC822</a>.
428      */

429     public final void setSenders(final String JavaDoc senders) {
430         message.setSenders(CollectionHandling.convertFromLines(senders, ";"));
431     }
432
433     /**
434      * <p>Call the corresponding server-side validation, handle possible
435      * exceptions and return any errors generated.</p>
436      *
437      * @param mapping The mapping used to select this instance
438      * @param request The servlet request we are processing
439      * @return <code>ActionMessages</code> collection containing all
440      * validation errors, or <code>null</code> if there were no errors.
441      */

442     public ValidationErrors validate(final HttpServletRequest JavaDoc request,
443             final HttpSession JavaDoc session) {
444         // only validate if ok was pressed
445
if (StringHandling.isNullOrEmpty(getOk()) &&
446                 StringHandling.isNullOrEmpty(getApply())) {
447             return null;
448         }
449
450         if (mail == null) {
451             throw new NullPointerException JavaDoc(
452                 "ERROR in ComposeForm: mail remote instance is null.");
453         }
454
455         SecuritySession securitySession = (SecuritySession)
456             session.getAttribute("securitySession");
457         ValidationErrors validationErrors;
458         try {
459             validationErrors = mail.validate(securitySession,
460                 message);
461         } catch (SystemException e) {
462             throw new RuntimeException JavaDoc(e);
463         }
464         return validationErrors;
465     }
466 }
467
Popular Tags