View Javadoc
1   /*
2    * $Source$
3    * $Revision$
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.PoemType;
56  import org.melati.poem.SQLPoemType;
57  
58  /**
59   * A numeric year type. 
60   */
61  
62  class YearPoemType extends IntegerPoemType implements PoemType<Integer> {
63    /** First year for a dropdown. */
64    static final int firstYear = 2000; 
65    /** Limit  (excluded)  year for a dropdown. */
66    static final int limitYear = 2023;
67    
68    /**
69     * Constructor.
70     * @param nullable whether null is an allowed value
71     * @param low lower (inclusive) limit
72     * @param limit upper (exclusive) limit
73     */
74    public YearPoemType(boolean nullable, int low, int limit) {
75      super(nullable);
76      setRawRange(new Integer(low), new Integer(limit));
77    }
78  
79    protected boolean _canRepresent(SQLPoemType<?> other) {
80      return other instanceof YearPoemType;
81    }
82  
83    /**
84     * {@inheritDoc}
85     * @see java.lang.Object#toString()
86     */
87    public String toString() {
88      return super.toString() + " (year)";
89    }
90  }
91  
92  /**
93   * A numeric month type.
94   */
95  class MonthPoemType extends IntegerPoemType {
96  
97    /**
98     * Constructor.
99     * @param nullable whether null is an allowed value
100    */
101   public MonthPoemType(boolean nullable) {
102     super(nullable);
103     setRawRange(new Integer(1), new Integer(13));
104   }
105 
106   protected boolean _canRepresent(SQLPoemType<?> other) {
107     return other instanceof MonthPoemType;
108   }
109 
110   protected String _stringOfCooked(Object raw,
111                                    PoemLocale locale, int style) {
112     int m = ((Integer)raw).intValue();
113     switch (style) {
114       case DateFormat.FULL: case DateFormat.LONG:
115         return locale.monthName(m);
116       case DateFormat.MEDIUM:
117         return locale.shortMonthName(m);
118       default:
119         return "" + m;
120     }
121   }
122 
123   /**
124    * {@inheritDoc}
125    * @see java.lang.Object#toString()
126    */
127   public String toString() {
128     return super.toString() + " (month)";
129   }
130 }
131 
132 /**
133  * A numeric day type.
134  */
135 class DayPoemType extends IntegerPoemType {
136 
137   /**
138    * Constructor.
139    * @param nullable whether null is an allowed value
140    */
141   public DayPoemType(boolean nullable) {
142     super(nullable);
143     setRawRange(new Integer(1), new Integer(32));
144   }
145 
146   protected boolean _canRepresent(SQLPoemType<?> other) {
147     return other instanceof DayPoemType;
148   }
149 
150   /**
151    * {@inheritDoc}
152    * @see java.lang.Object#toString()
153    */
154   public String toString() {
155     return super.toString() + " (day)";
156   }
157 }
158 
159 /**
160  * An adaptor for a string date in YMD format.
161  * See for example org.melati.poem.DatePoemType-dropdown.wm
162  */
163 public class YMDDateAdaptor implements TempletAdaptor {
164 
165   protected static final String
166       yearSuffix = "-year",
167       monthSuffix = "-month",
168       daySuffix = "-day";
169 
170   /** The instance. */
171   public static final YMDDateAdaptor it = new YMDDateAdaptor();
172 
173   protected String getFormOrDie(ServletTemplateContext context,
174                               String fieldName, String suffix) {
175     String fullName = fieldName + suffix;
176     String value = context.getFormField(fullName);
177     if (value == null)
178       throw new MissingFieldException(this, fieldName, fullName);
179     return value;
180   }
181 
182   @Override
183   public Object rawFrom(ServletTemplateContext context, String fieldName) {
184     String year = getFormOrDie(context, fieldName, yearSuffix);
185     String month = getFormOrDie(context, fieldName, monthSuffix);
186     String day = getFormOrDie(context, fieldName, daySuffix);
187 
188     if (year.equals("") && month.equals("") && day.equals(""))
189       return null;
190     else if (!year.equals("") && !month.equals("") ) {
191       if (day.equals("")) // MYDate template need default day
192         day = "1";
193       Calendar cal = Calendar.getInstance();
194       cal.set(Integer.parseInt(year),
195               Integer.parseInt(month) - 1,
196               Integer.parseInt(day));
197       return new Date(cal.getTime().getTime());
198     } else {
199       throw new PartlyNullException(fieldName);
200     }
201   }
202 
203   /**
204    * @param dateField date field to extract year field from 
205    * @return year constituent of date
206    */
207   public Field<Integer> yearField(Field<Date> dateField) {
208 
209     Calendar when = when(dateField);
210 
211     // This isn't meant to be used, so we don't try to localise it
212     String displayName = dateField.getDisplayName() + " (year)";
213 
214     return new Field<Integer>(
215         when == null ? null : new Integer(when.get(Calendar.YEAR)),
216         new BaseFieldAttributes<Integer>(
217             dateField.getName() + yearSuffix,
218             displayName,
219             null,
220             new YearPoemType(dateField.getType().getNullable(),
221                              YearPoemType.firstYear, YearPoemType.limitYear),
222                              5, 1,
223                              null, false, true, true));
224   }
225 
226   /**
227    * @param dateField date field to extract month field from 
228    * @return month constituent of date
229    */
230   public Field<Integer> monthField(Field<Date> dateField) {
231 
232     Calendar when = when(dateField);
233     // This isn't meant to be used, so we don't try to localise it
234 
235     String displayName = dateField.getDisplayName() + " (month)";
236 
237     return new Field<Integer>(
238         when == null ? null : new Integer(when.get(Calendar.MONTH) + 1),
239         new BaseFieldAttributes<Integer>(
240             dateField.getName() + monthSuffix, displayName, null,
241             dateField.getType().getNullable() ? new MonthPoemType(true) :
242                                             new MonthPoemType(false),
243             3, 1,
244             null, false, true, true));
245   }
246 
247   /**
248    * @param dateField date field to extract day field from 
249    * @return day constituent of date
250    */
251   public Field<Integer> dayField(Field<Date> dateField) {
252 
253     Calendar when = when(dateField);
254     // This isn't meant to be used, so we don't try to localise it
255 
256     String displayName = dateField.getDisplayName() + " (day)";
257 
258     return new Field<Integer>(
259         when == null ? null : new Integer(when.get(Calendar.DAY_OF_MONTH)),
260         new BaseFieldAttributes<Integer>(
261             dateField.getName() + daySuffix, displayName, null,
262             dateField.getType().getNullable() ? new DayPoemType(true) :
263                                             new DayPoemType(false),
264             2, 1,
265             null, false, true, true));
266   }
267   
268   protected Calendar when(Field<Date> dateField) {
269     if (dateField.getRaw() == null) return null;
270     Calendar when = Calendar.getInstance();
271     when.setTime((java.util.Date)dateField.getRaw());
272     return when;
273   }
274 }