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.util.Calendar;
48  import java.sql.Date;
49  import java.sql.Timestamp;
50  
51  import org.melati.poem.Field;
52  import org.melati.poem.IntegerPoemType;
53  import org.melati.poem.BaseFieldAttributes;
54  import org.melati.poem.SQLPoemType;
55  
56  /**
57   * An hour type.
58   */
59  class HourPoemType extends IntegerPoemType {
60    /**
61     * Constructor.
62     * @param nullable whether null is an allowed value
63     */
64    public HourPoemType(boolean nullable) {
65      super(nullable);
66      setRawRange(new Integer(0), new Integer(24));
67    }
68  
69    protected boolean _canRepresent(SQLPoemType<?> other) {
70      return other instanceof HourPoemType;
71    }
72  
73    /**
74     * {@inheritDoc}
75     * @see java.lang.Object#toString()
76     */
77    public String toString() {
78      return super.toString() + " (hour)";
79    }
80  }
81  
82  /**
83   * A minute type.
84   */
85  class MinutePoemType extends IntegerPoemType {
86    /**
87     * Constructor.
88     * @param nullable whether null is an allowed value
89     */
90    public MinutePoemType(boolean nullable) {
91      super(nullable);
92      setRawRange(new Integer(0), new Integer(60));
93    }
94  
95    protected boolean _canRepresent(SQLPoemType<?> other) {
96      return other instanceof MinutePoemType;
97    }
98  
99    /**
100    * {@inheritDoc}
101    * @see java.lang.Object#toString()
102    */
103   public String toString() {
104     return super.toString() + " (minutes)";
105   }
106 }
107 
108 /**
109  * A second.
110  */
111 class SecondPoemType extends IntegerPoemType {
112   /**
113    * Constructor.
114    * @param nullable whether null is an allowed value
115    */
116   public SecondPoemType(boolean nullable) {
117     super(nullable);
118     setRawRange(new Integer(0), new Integer(60));
119   }
120 
121   protected boolean _canRepresent(SQLPoemType<?> other) {
122     return other instanceof SecondPoemType;
123   }
124 
125   /**
126    * {@inheritDoc}
127    * @see java.lang.Object#toString()
128    */
129   public String toString() {
130     return super.toString() + " (seconds)";
131   }
132 }
133 
134 
135 /**
136  * An adaptor for a string date in YMDHMST format.
137  */
138 public class YMDHMSTimestampAdaptor extends YMDDateAdaptor {
139   private static final String
140       hourSuffix = "-hour",
141       minuteSuffix = "-minute",
142       secondSuffix = "-second";
143 
144   private static final YMDHMSTimestampAdaptor me = new YMDHMSTimestampAdaptor();
145 
146   /**
147    * @return the instance.
148    */
149   public static YMDHMSTimestampAdaptor getIt() {
150     return me;
151   }
152 
153   /**
154    * {@inheritDoc}
155    * @see org.melati.template.TempletAdaptor#rawFrom(
156    *          org.melati.template.ServletTemplateContext, java.lang.String)
157    */
158   public Object rawFrom(ServletTemplateContext context, String fieldName) {
159     String year = getFormOrDie(context, fieldName, yearSuffix);
160     String month = getFormOrDie(context, fieldName, monthSuffix);
161     String day = getFormOrDie(context, fieldName, daySuffix);
162     String hour = getFormOrDie(context, fieldName, hourSuffix);
163     String minute = getFormOrDie(context, fieldName, minuteSuffix);
164     String second = getFormOrDie(context, fieldName, secondSuffix);
165 
166     if (year.equals("") && month.equals("") && day.equals("") &&
167         hour.equals("") && minute.equals("") && second.equals(""))
168       return null;
169     else if (!year.equals("") && !month.equals("") && !day.equals("") &&
170              !hour.equals("") && !minute.equals("") && !second.equals("")) {
171       Calendar cal = Calendar.getInstance();
172       cal.set(Integer.parseInt(year),
173               Integer.parseInt(month) - 1,
174               Integer.parseInt(day),
175               Integer.parseInt(hour),
176               Integer.parseInt(minute),
177               Integer.parseInt(second));
178       return new Timestamp(cal.getTime().getTime());
179     } else {
180       throw new PartlyNullException(fieldName);
181     }
182   }
183 
184   /**
185    * @param field the field to copy
186    * @return an hour field
187    */
188   public Field<Integer> hourField(Field<Date> field) {
189 
190     Calendar when = when(field);
191 
192     // This isn't meant to be used, so we don't try to localize it
193 
194     String displayName = field.getDisplayName() + " (hour)";
195 
196     return new Field<Integer>(
197         when == null ? null : new Integer(when.get(Calendar.HOUR_OF_DAY)),
198         new BaseFieldAttributes<Integer>(
199             field.getName() + hourSuffix, displayName, null,
200             field.getType().getNullable() ? new HourPoemType(true) :
201                                             new HourPoemType(false),
202             2, 1,
203             null, false, true, true));
204   }
205 
206   /**
207    * @param field the field to copy
208    * @return a minute field
209    */
210   public Field<Integer> minuteField(Field<Date> field) {
211 
212     Calendar when = when(field);
213 
214     // This isn't meant to be used, so we don't try to localize it
215 
216     String displayName = field.getDisplayName() + " (minutes)";
217 
218     return new Field<Integer>(
219         when == null ? null : new Integer(when.get(Calendar.MINUTE)),
220         new BaseFieldAttributes<Integer>(
221             field.getName() + minuteSuffix, displayName, null,
222             field.getType().getNullable() ? new MinutePoemType(true) :
223                                             new MinutePoemType(false),
224             2, 1,
225             null, false, true, true));
226   }
227 
228   /**
229    * @param field the field to copy
230    * @return a second field
231    */
232   public Field<Integer> secondField(Field<Date> field) {
233 
234     Calendar when = when(field);
235 
236     // This isn't meant to be used, so we don't try to localize it
237 
238     String displayName = field.getDisplayName() + " (seconds)";
239 
240     return new Field<Integer>(
241         when == null ? null : new Integer(when.get(Calendar.SECOND)),
242         new BaseFieldAttributes<Integer>(
243             field.getName() + secondSuffix, displayName, null,
244             field.getType().getNullable() ? new SecondPoemType(true) :
245                                             new SecondPoemType(false),
246             2, 1,
247             null, false, true, true));
248   }
249 }