View Javadoc

1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.control.form;
16  
17  import org.figure8.join.control.JoinForm;
18  import org.figure8.join.control.action.MessageActions;
19  
20  import org.apache.struts.action.ActionMapping;
21  
22  import javax.servlet.http.HttpServletRequest;
23  
24  import java.util.Date;
25  import java.text.SimpleDateFormat;
26  /**
27   * Form object used for manipulating Messages into Join application.
28   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
29   * @version $Revision: 1.1 $
30   *
31   * @struts.form name="messageForm"
32   */
33  public class MessageForm extends JoinForm{
34  
35     // Attributes ---------------------------------------------------------------
36  
37     /** This message title */
38     private String title;
39     /** This message content */
40     private String content;
41     /** This message link to outer reference */
42     private String link;
43     /** This message publication date */
44     private Date publicationDate;
45     /** This message publication end date */
46     private Date expiryDate;
47     /** The name of release this message is attached to */
48     private String releaseName;
49  
50     /** String representation of <b>publicationDate</b> */
51     private String publicationDateStr;
52     /** Sting representation of <b>expiryDate</b> */
53     private String expiryDateStr;
54  
55  
56     // Constructors -------------------------------------------------------------
57  
58     /** Creates a new MessageForm */
59     public MessageForm(){
60     }
61  
62  
63     // Public -------------------------------------------------------------------
64  
65     /** @return This message title */
66     public String getTitle(){
67        return title;
68     }
69     /** @param title This message title */
70     public void setTitle(String title){
71        this.title = title;
72     }
73     /** @return Content of message */
74     public String getContent(){
75        return content;
76     }
77     /** @param content Content of this message */
78     public void setContent(String content){
79        this.content = content;
80     }
81     /** @return Link reference from this message */
82     public String getLink(){
83        return link;
84     }
85     /** @param link Link reference from this message */
86     public void setLink(String link){
87        this.link = link;
88     }
89     /** @return The publication date of this message */
90     public Date getPublicationDate(){
91        return publicationDate;
92     }
93     /** @param publicationDate The publication date of this message */
94     public void setPublicationDate(Date publicationDate){
95        this.publicationDate = publicationDate;
96        // Format to string according to locale.
97        SimpleDateFormat format = new SimpleDateFormat(getMessageResources().getMessage("pattern.date.stringtodate"));
98        this.publicationDateStr = format.format(publicationDate);
99     }
100    /** @return The date of publication end for this message */
101    public Date getExpiryDate(){
102       return expiryDate;
103    }
104    /** @param expiryDate The date of publication end for this message */
105    public void setExpiryDate(Date expiryDate){
106       this.expiryDate = expiryDate;
107       // Format to string according to locale.
108       SimpleDateFormat format = new SimpleDateFormat(getMessageResources().getMessage("pattern.date.stringtodate"));
109       this.expiryDateStr = format.format(expiryDate);
110    }
111    /** @return The name of the release this message is attached to */
112    public String getReleaseName(){
113       return releaseName;
114    }
115    /** @param releaseName The name of release this message is attached to */
116    public void setReleaseName(String releaseName){
117       this.releaseName = releaseName;
118    }
119 
120    /** @return Publication date for this message (should be a date) */
121    public String getPublicationDateStr(){
122       return publicationDateStr;
123    }
124    /** @param publicationDateStr Publication date for this message (should be a date) */
125    public void setPublicationDateStr(String publicationDateStr){
126       this.publicationDateStr = publicationDateStr;
127    }
128    /** @return Date of publication end for this message (should be a date) */
129    public String getExpiryDateStr(){
130       return expiryDateStr;
131    }
132    /** @param expiryDateStr Date of publication end for this message (should be a date) */
133    public void setExpiryDateStr(String expiryDateStr){
134       this.expiryDateStr = expiryDateStr;
135    }
136 
137 
138    // Implementation of JoinForm -----------------------------------------------
139 
140    /**
141     * Validation of the form attributes.
142     * @param operation String representing the operation to invoke on Action
143     * @param mapping Mapping between forwards name and path for this action
144     * @param request The servlet container request wrapper
145     */
146    public void doValidate(String operation, ActionMapping mapping, HttpServletRequest request){
147 
148       if (MessageActions.LOAD_OP.equals(operation)){
149          // Check and see if id is missing.
150          validateEntityObjectId();
151       }
152       else if (MessageActions.SAVE_OP.equals(operation)){
153          // Check and see if title is missing.
154          if (title == null || title.length() == 0)
155             addActionError("errors.required", getGuiMessageResources().getMessage("label.message.title"));
156          // Check and see if content is missing.
157          if (content == null || content.length() == 0)
158             addActionError("errors.required", getGuiMessageResources().getMessage("label.message.content"));
159       }
160 
161       if (MessageActions.SAVE_OP.equals(operation) || MessageActions.SEARCH_OP.equals(operation)){
162          // Check and see if publication date is missing.
163          if (publicationDateStr == null || publicationDateStr.length() == 0)
164             addActionError("errors.required", getGuiMessageResources().getMessage("label.message.publicationdate"));
165          else{
166             // Build a format using a locale pattern.
167             SimpleDateFormat format = new SimpleDateFormat(getMessageResources().getMessage("pattern.date.stringtodate"));
168             // Check if it's parsable.
169             try {publicationDate = format.parse(publicationDateStr);}
170             catch (Exception e){
171                addActionError("errors.format", getGuiMessageResources().getMessage("label.message.publicationdate"));
172             }
173          }
174          // Check and see if expiry date is missing.
175          if (expiryDateStr == null || expiryDateStr.length() == 0)
176             addActionError("errors.required", getGuiMessageResources().getMessage("label.message.expirydate"));
177          else{
178             // Build a format using a locale pattern.
179             SimpleDateFormat format = new SimpleDateFormat(getMessageResources().getMessage("pattern.date.stringtodate"));
180             // Check if it's parsable.
181             try {expiryDate = format.parse(expiryDateStr);}
182             catch (Exception e){
183                addActionError("errors.format", getGuiMessageResources().getMessage("label.message.expirydate"));
184             }
185          }
186       }
187    }
188 
189 
190    // Override of ActionForm ---------------------------------------------------
191 
192    /**
193     * Reset form attributes.
194     * @param mapping Mapping between forwards name and path for this action
195     * @param request The servlet container request wrapper
196     */
197    public void reset(ActionMapping mapping, HttpServletRequest request){
198       super.reset(mapping, request);
199       title = null;
200       content = null;
201       link = null;
202       publicationDate = null;
203       publicationDateStr = null;
204       expiryDate = null;
205       expiryDateStr = null;
206       releaseName = null;
207    }
208 }