View Javadoc

1   /*
2    * $Source: /usr/cvsroot/melati/melati/src/main/java/org/melati/template/YMDDateAdaptor.java,v $
3    * $Revision: 1.18 $
4    *
5    * Copyright (C) 2000 Tim Joyce
6    *
7    * Part of Melati (http://melati.org), a framework for the rapid
8    * development of clean, maintainable web applications.
9    *
10   * Melati is free software; Permission is granted to copy, distribute
11   * and/or modify this software under the terms either:
12   *
13   * a) the GNU General Public License as published by the Free Software
14   *    Foundation; either version 2 of the License, or (at your option)
15   *    any later version,
16   *
17   *    or
18   *
19   * b) any version of the Melati Software License, as published
20   *    at http://melati.org
21   *
22   * You should have received a copy of the GNU General Public License and
23   * the Melati Software License along with this program;
24   * if not, write to the Free Software Foundation, Inc.,
25   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26   * GNU General Public License and visit http://melati.org to obtain the
27   * Melati Software License.
28   *
29   * Feel free to contact the Developers of Melati (http://melati.org),
30   * if you would like to work out a different arrangement than the options
31   * outlined here.  It is our intention to allow Melati to be used by as
32   * wide an audience as possible.
33   *
34   * This program is distributed in the hope that it will be useful,
35   * but WITHOUT ANY WARRANTY; without even the implied warranty of
36   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37   * GNU General Public License for more details.
38   *
39   * Contact details for copyright holder:
40   *
41   *     Tim Joyce  <timj At paneris.org>
42   *     http://paneris.org/~timj
43   */
44  
45  package org.melati.template;
46  
47  import java.sql.Date;
48  import java.text.DateFormat;
49  import java.util.Calendar;
50  
51  import org.melati.poem.BaseFieldAttributes;
52  import org.melati.poem.Field;
53  import org.melati.poem.IntegerPoemType;
54  import org.melati.poem.PoemLocale;
55  import org.melati.poem.SQLPoemType;
56  
57  /**
58   * A numeric year type. 
59   */
60  
61  class YearPoemType extends IntegerPoemType {
62    /** First year for a dropdown. */
63    static final int firstYear = 2000; 
64    /** Limit  (excluded)  year for a dropdown. */
65    static final int limitYear = 2023;
66    
67    /**
68     * Constructor.
69     * @param nullable whether null is an allowed value
70     * @param low lower (inclusive) limit
71     * @param limit upper (exclusive) limit
72     */
73    public YearPoemType(boolean nullable, int low, int limit) {
74      super(nullable);
75      setRawRange(new Integer(low), new Integer(limit));
76    }
77  
78    protected boolean _canRepresent(SQLPoemType other) {
79      return other instanceof YearPoemType;
80    }
81  
82    /**
83     * {@inheritDoc}
84     * @see java.lang.Object#toString()
85     */
86    public String toString() {
87      return super.toString() + " (year)";
88    }
89  }
90  
91  /**
92   * A numeric month type.
93   */
94  class MonthPoemType extends IntegerPoemType {
95  
96    /**
97     * Constructor.
98     * @param nullable whether null is an allowed value
99     */
100   public MonthPoemType(boolean nullable) {
101     super(nullable);
102     setRawRange(new Integer(1), new Integer(13));
103   }
104 
105   protected boolean _canRepresent(SQLPoemType other) {
106     return other instanceof MonthPoemType;
107   }
108 
109   protected String _stringOfCooked(Object raw,
110                                    PoemLocale locale, int style) {
111     int m = ((Integer)raw).intValue();
112     switch (style) {
113       case DateFormat.FULL: case DateFormat.LONG:
114         return locale.monthName(m);
115       case DateFormat.MEDIUM:
116         return locale.shortMonthName(m);
117       default:
118         return "" + m;
119     }
120   }
121 
122   /**
123    * {@inheritDoc}
124    * @see java.lang.Object#toString()
125    */
126   public String toString() {
127     return super.toString() + " (month)";
128   }
129 }
130 
131 /**
132  * A numeric day type.
133  */
134 class DayPoemType extends IntegerPoemType {
135 
136   /**
137    * Constructor.
138    * @param nullable whether null is an allowed value
139    */
140   public DayPoemType(boolean nullable) {
141     super(nullable);
142     setRawRange(new Integer(1), new Integer(32));
143   }
144 
145   protected boolean _canRepresent(SQLPoemType other) {
146     return other instanceof DayPoemType;
147   }
148 
149   /**
150    * {@inheritDoc}
151    * @see java.lang.Object#toString()
152    */
153   public String toString() {
154     return super.toString() + " (day)";
155   }
156 }
157 
158 /**
159  * An adaptor for a string date in YMD format.
160  * See for example org.melati.poem.DatePoemType-dropdown.wm
161  */
162 public class YMDDateAdaptor implements TempletAdaptor {
163 
164   protected static final String
165       yearSuffix = "-year",
166       monthSuffix = "-month",
167       daySuffix = "-day";
168 
169   /** The instance. */
170   public static final YMDDateAdaptor it = new YMDDateAdaptor();
171 
172   protected String getFormOrDie(ServletTemplateContext context,
173                               String fieldName, String suffix) {
174     String fullName = fieldName + suffix;
175     String value = context.getFormField(fullName);
176     if (value == null)
177       throw new MissingFieldException(this, fieldName, fullName);
178     return value;
179   }
180 
181   /**
182    * {@inheritDoc}
183    * @see org.melati.template.TempletAdaptor#
184    *          rawFrom(org.melati.template.ServletTemplateContext, java.lang.String)
185    */
186   public Object rawFrom(ServletTemplateContext context, String fieldName) {
187     String year = getFormOrDie(context, fieldName, yearSuffix);
188     String month = getFormOrDie(context, fieldName, monthSuffix);
189     String day = getFormOrDie(context, fieldName, daySuffix);
190 
191     if (year.equals("") && month.equals("") && day.equals(""))
192       return null;
193     else if (!year.equals("") && !month.equals("") ) {
194       if (day.equals("")) // MYDate template need default day
195         day = "1";
196       Calendar cal = Calendar.getInstance();
197       cal.set(Integer.parseInt(year),
198               Integer.parseInt(month) - 1,
199               Integer.parseInt(day));
200       return new Date(cal.getTime().getTime());
201     } else {
202       throw new PartlyNullException(fieldName);
203     }
204   }
205 
206   /**
207    * @param dateField date field to extract year field from 
208    * @return year constituent of date
209    */
210   public Field yearField(Field dateField) {
211 
212     Calendar when = when(dateField);
213 
214     // This isn't meant to be used, so we don't try to localise it
215     String displayName = dateField.getDisplayName() + " (year)";
216 
217     return new Field(
218         when == null ? null : new Integer(when.get(Calendar.YEAR)),
219         new BaseFieldAttributes(
220             dateField.getName() + yearSuffix,
221             displayName,
222             null,
223             new YearPoemType(dateField.getType().getNullable(),
224                              YearPoemType.firstYear, YearPoemType.limitYear),
225                              5, 1,
226                              null, false, true, true));
227   }
228 
229   /**
230    * @param dateField date field to extract month field from 
231    * @return month constituent of date
232    */
233   public Field monthField(Field dateField) {
234 
235     Calendar when = when(dateField);
236     // This isn't meant to be used, so we don't try to localise it
237 
238     String displayName = dateField.getDisplayName() + " (month)";
239 
240     return new Field(
241         when == null ? null : new Integer(when.get(Calendar.MONTH) + 1),
242         new BaseFieldAttributes(
243             dateField.getName() + monthSuffix, displayName, null,
244             dateField.getType().getNullable() ? new MonthPoemType(true) :
245                                             new MonthPoemType(false),
246             3, 1,
247             null, false, true, true));
248   }
249 
250   /**
251    * @param dateField date field to extract day field from 
252    * @return day constituent of date
253    */
254   public Field dayField(Field dateField) {
255 
256     Calendar when = when(dateField);
257     // This isn't meant to be used, so we don't try to localise it
258 
259     String displayName = dateField.getDisplayName() + " (day)";
260 
261     return new Field(
262         when == null ? null : new Integer(when.get(Calendar.DAY_OF_MONTH)),
263         new BaseFieldAttributes(
264             dateField.getName() + daySuffix, displayName, null,
265             dateField.getType().getNullable() ? new DayPoemType(true) :
266                                             new DayPoemType(false),
267             2, 1,
268             null, false, true, true));
269   }
270   
271   protected Calendar when(Field dateField) {
272     if (dateField.getRaw() == null) return null;
273     Calendar when = Calendar.getInstance();
274     when.setTime((java.util.Date)dateField.getRaw());
275     return when;
276   }
277 }