YMDDateAdaptor.java

  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. package org.melati.template;

  45. import java.sql.Date;
  46. import java.text.DateFormat;
  47. import java.util.Calendar;

  48. import org.melati.poem.BaseFieldAttributes;
  49. import org.melati.poem.Field;
  50. import org.melati.poem.IntegerPoemType;
  51. import org.melati.poem.PoemLocale;
  52. import org.melati.poem.PoemType;
  53. import org.melati.poem.SQLPoemType;

  54. /**
  55.  * A numeric year type.
  56.  */

  57. class YearPoemType extends IntegerPoemType implements PoemType<Integer> {
  58.   /** First year for a dropdown. */
  59.   static final int firstYear = 2000;
  60.   /** Limit  (excluded)  year for a dropdown. */
  61.   static final int limitYear = 2023;
  62.  
  63.   /**
  64.    * Constructor.
  65.    * @param nullable whether null is an allowed value
  66.    * @param low lower (inclusive) limit
  67.    * @param limit upper (exclusive) limit
  68.    */
  69.   public YearPoemType(boolean nullable, int low, int limit) {
  70.     super(nullable);
  71.     setRawRange(new Integer(low), new Integer(limit));
  72.   }

  73.   protected boolean _canRepresent(SQLPoemType<?> other) {
  74.     return other instanceof YearPoemType;
  75.   }

  76.   /**
  77.    * {@inheritDoc}
  78.    * @see java.lang.Object#toString()
  79.    */
  80.   public String toString() {
  81.     return super.toString() + " (year)";
  82.   }
  83. }

  84. /**
  85.  * A numeric month type.
  86.  */
  87. class MonthPoemType extends IntegerPoemType {

  88.   /**
  89.    * Constructor.
  90.    * @param nullable whether null is an allowed value
  91.    */
  92.   public MonthPoemType(boolean nullable) {
  93.     super(nullable);
  94.     setRawRange(new Integer(1), new Integer(13));
  95.   }

  96.   protected boolean _canRepresent(SQLPoemType<?> other) {
  97.     return other instanceof MonthPoemType;
  98.   }

  99.   protected String _stringOfCooked(Object raw,
  100.                                    PoemLocale locale, int style) {
  101.     int m = ((Integer)raw).intValue();
  102.     switch (style) {
  103.       case DateFormat.FULL: case DateFormat.LONG:
  104.         return locale.monthName(m);
  105.       case DateFormat.MEDIUM:
  106.         return locale.shortMonthName(m);
  107.       default:
  108.         return "" + m;
  109.     }
  110.   }

  111.   /**
  112.    * {@inheritDoc}
  113.    * @see java.lang.Object#toString()
  114.    */
  115.   public String toString() {
  116.     return super.toString() + " (month)";
  117.   }
  118. }

  119. /**
  120.  * A numeric day type.
  121.  */
  122. class DayPoemType extends IntegerPoemType {

  123.   /**
  124.    * Constructor.
  125.    * @param nullable whether null is an allowed value
  126.    */
  127.   public DayPoemType(boolean nullable) {
  128.     super(nullable);
  129.     setRawRange(new Integer(1), new Integer(32));
  130.   }

  131.   protected boolean _canRepresent(SQLPoemType<?> other) {
  132.     return other instanceof DayPoemType;
  133.   }

  134.   /**
  135.    * {@inheritDoc}
  136.    * @see java.lang.Object#toString()
  137.    */
  138.   public String toString() {
  139.     return super.toString() + " (day)";
  140.   }
  141. }

  142. /**
  143.  * An adaptor for a string date in YMD format.
  144.  * See for example org.melati.poem.DatePoemType-dropdown.wm
  145.  */
  146. public class YMDDateAdaptor implements TempletAdaptor {

  147.   protected static final String
  148.       yearSuffix = "-year",
  149.       monthSuffix = "-month",
  150.       daySuffix = "-day";

  151.   /** The instance. */
  152.   public static final YMDDateAdaptor it = new YMDDateAdaptor();

  153.   protected String getFormOrDie(ServletTemplateContext context,
  154.                               String fieldName, String suffix) {
  155.     String fullName = fieldName + suffix;
  156.     String value = context.getFormField(fullName);
  157.     if (value == null)
  158.       throw new MissingFieldException(this, fieldName, fullName);
  159.     return value;
  160.   }

  161.   @Override
  162.   public Object rawFrom(ServletTemplateContext context, String fieldName) {
  163.     String year = getFormOrDie(context, fieldName, yearSuffix);
  164.     String month = getFormOrDie(context, fieldName, monthSuffix);
  165.     String day = getFormOrDie(context, fieldName, daySuffix);

  166.     if (year.equals("") && month.equals("") && day.equals(""))
  167.       return null;
  168.     else if (!year.equals("") && !month.equals("") ) {
  169.       if (day.equals("")) // MYDate template need default day
  170.         day = "1";
  171.       Calendar cal = Calendar.getInstance();
  172.       cal.set(Integer.parseInt(year),
  173.               Integer.parseInt(month) - 1,
  174.               Integer.parseInt(day));
  175.       return new Date(cal.getTime().getTime());
  176.     } else {
  177.       throw new PartlyNullException(fieldName);
  178.     }
  179.   }

  180.   /**
  181.    * @param dateField date field to extract year field from
  182.    * @return year constituent of date
  183.    */
  184.   public Field<Integer> yearField(Field<Date> dateField) {

  185.     Calendar when = when(dateField);

  186.     // This isn't meant to be used, so we don't try to localise it
  187.     String displayName = dateField.getDisplayName() + " (year)";

  188.     return new Field<Integer>(
  189.         when == null ? null : new Integer(when.get(Calendar.YEAR)),
  190.         new BaseFieldAttributes<Integer>(
  191.             dateField.getName() + yearSuffix,
  192.             displayName,
  193.             null,
  194.             new YearPoemType(dateField.getType().getNullable(),
  195.                              YearPoemType.firstYear, YearPoemType.limitYear),
  196.                              5, 1,
  197.                              null, false, true, true));
  198.   }

  199.   /**
  200.    * @param dateField date field to extract month field from
  201.    * @return month constituent of date
  202.    */
  203.   public Field<Integer> monthField(Field<Date> dateField) {

  204.     Calendar when = when(dateField);
  205.     // This isn't meant to be used, so we don't try to localise it

  206.     String displayName = dateField.getDisplayName() + " (month)";

  207.     return new Field<Integer>(
  208.         when == null ? null : new Integer(when.get(Calendar.MONTH) + 1),
  209.         new BaseFieldAttributes<Integer>(
  210.             dateField.getName() + monthSuffix, displayName, null,
  211.             dateField.getType().getNullable() ? new MonthPoemType(true) :
  212.                                             new MonthPoemType(false),
  213.             3, 1,
  214.             null, false, true, true));
  215.   }

  216.   /**
  217.    * @param dateField date field to extract day field from
  218.    * @return day constituent of date
  219.    */
  220.   public Field<Integer> dayField(Field<Date> dateField) {

  221.     Calendar when = when(dateField);
  222.     // This isn't meant to be used, so we don't try to localise it

  223.     String displayName = dateField.getDisplayName() + " (day)";

  224.     return new Field<Integer>(
  225.         when == null ? null : new Integer(when.get(Calendar.DAY_OF_MONTH)),
  226.         new BaseFieldAttributes<Integer>(
  227.             dateField.getName() + daySuffix, displayName, null,
  228.             dateField.getType().getNullable() ? new DayPoemType(true) :
  229.                                             new DayPoemType(false),
  230.             2, 1,
  231.             null, false, true, true));
  232.   }
  233.  
  234.   protected Calendar when(Field<Date> dateField) {
  235.     if (dateField.getRaw() == null) return null;
  236.     Calendar when = Calendar.getInstance();
  237.     when.setTime((java.util.Date)dateField.getRaw());
  238.     return when;
  239.   }
  240. }