Coverage Report - org.melati.template.YMDDateAdaptor
 
Classes in this File Line Coverage Branch Coverage Complexity
DayPoemType
0%
0/5
N/A
1.875
MonthPoemType
0%
0/10
0%
0/4
1.875
YMDDateAdaptor
7%
2/30
0%
0/26
1.875
YearPoemType
0%
0/5
N/A
1.875
 
 1  
 /*
 2  
  * $Source: /usr/cvsroot/melati/melati/src/main/java/org/melati/template/YMDDateAdaptor.java,v $
 3  
  * $Revision: 1.16 $
 4  
  *
 5  
  * Copyright (C) 2000 William Chesters
 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  
  *     William Chesters <williamc At paneris.org>
 42  
  *     http://paneris.org/~williamc
 43  
  *     Obrechtstraat 114, 2517VX Den Haag, The Netherlands
 44  
  */
 45  
 
 46  
 package org.melati.template;
 47  
 
 48  
 import java.sql.Date;
 49  
 import java.text.DateFormat;
 50  
 import java.util.Calendar;
 51  
 
 52  
 import org.melati.poem.BaseFieldAttributes;
 53  
 import org.melati.poem.Field;
 54  
 import org.melati.poem.IntegerPoemType;
 55  
 import org.melati.poem.PoemLocale;
 56  
 import org.melati.poem.SQLPoemType;
 57  
 
 58  
 /**
 59  
  * A numeric year type. 
 60  
  */
 61  
 
 62  
 class YearPoemType extends IntegerPoemType {
 63  
   /** First year for a dropdown. */
 64  
   static final int firstYear = 2000; 
 65  
   /** Limt  (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  0
     super(nullable);
 76  0
     setRawRange(new Integer(low), new Integer(limit));
 77  0
   }
 78  
 
 79  
   protected boolean _canRepresent(SQLPoemType other) {
 80  0
     return other instanceof YearPoemType;
 81  
   }
 82  
 
 83  
   /**
 84  
    * {@inheritDoc}
 85  
    * @see java.lang.Object#toString()
 86  
    */
 87  
   public String toString() {
 88  0
     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  0
     super(nullable);
 103  0
     setRawRange(new Integer(1), new Integer(13));
 104  0
   }
 105  
 
 106  
   protected boolean _canRepresent(SQLPoemType other) {
 107  0
     return other instanceof MonthPoemType;
 108  
   }
 109  
 
 110  
   protected String _stringOfCooked(Object raw,
 111  
                                    PoemLocale locale, int style) {
 112  0
     int m = ((Integer)raw).intValue();
 113  0
     switch (style) {
 114  
       case DateFormat.FULL: case DateFormat.LONG:
 115  0
         return locale.monthName(m);
 116  
       case DateFormat.MEDIUM:
 117  0
         return locale.shortMonthName(m);
 118  
       default:
 119  0
         return "" + m;
 120  
     }
 121  
   }
 122  
 
 123  
   /**
 124  
    * {@inheritDoc}
 125  
    * @see java.lang.Object#toString()
 126  
    */
 127  
   public String toString() {
 128  0
     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  0
     super(nullable);
 143  0
     setRawRange(new Integer(1), new Integer(32));
 144  0
   }
 145  
 
 146  
   protected boolean _canRepresent(SQLPoemType other) {
 147  0
     return other instanceof DayPoemType;
 148  
   }
 149  
 
 150  
   /**
 151  
    * {@inheritDoc}
 152  
    * @see java.lang.Object#toString()
 153  
    */
 154  
   public String toString() {
 155  0
     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  2
 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  1
   public static final YMDDateAdaptor it = new YMDDateAdaptor();
 172  
 
 173  
   protected String getFormOrDie(ServletTemplateContext context,
 174  
                               String fieldName, String suffix) {
 175  0
     String fullName = fieldName + suffix;
 176  0
     String value = context.getForm(fullName);
 177  0
     if (value == null)
 178  0
       throw new MissingFieldException(this, fieldName, fullName);
 179  0
     return value;
 180  
   }
 181  
 
 182  
   /**
 183  
    * {@inheritDoc}
 184  
    * @see org.melati.template.TempletAdaptor#
 185  
    *          rawFrom(org.melati.template.ServletTemplateContext, java.lang.String)
 186  
    */
 187  
   public Object rawFrom(ServletTemplateContext context, String fieldName) {
 188  0
     String year = getFormOrDie(context, fieldName, yearSuffix);
 189  0
     String month = getFormOrDie(context, fieldName, monthSuffix);
 190  0
     String day = getFormOrDie(context, fieldName, daySuffix);
 191  
 
 192  0
     if (year.equals("") && month.equals("") && day.equals(""))
 193  0
       return null;
 194  0
     else if (!year.equals("") && !month.equals("") && !day.equals("")) {
 195  0
       Calendar cal = Calendar.getInstance();
 196  0
       cal.set(Integer.parseInt(year),
 197  
               Integer.parseInt(month) - 1,
 198  
               Integer.parseInt(day));
 199  0
       return new Date(cal.getTime().getTime());
 200  
     } else {
 201  0
       throw new PartlyNullException(fieldName);
 202  
     }
 203  
   }
 204  
 
 205  
   /**
 206  
    * @param dateField date field to extract year field from 
 207  
    * @return year constituent of date
 208  
    */
 209  
   public Field yearField(Field dateField) {
 210  
 
 211  0
     Calendar when = when(dateField);
 212  
 
 213  
     // This isn't meant to be used, so we don't try to localize it
 214  0
     String displayName = dateField.getDisplayName() + " (year)";
 215  
 
 216  0
     return new Field(
 217  
         when == null ? null : new Integer(when.get(Calendar.YEAR)),
 218  
         new BaseFieldAttributes(
 219  
             dateField.getName() + yearSuffix,
 220  
             displayName,
 221  
             null,
 222  
             new YearPoemType(dateField.getType().getNullable(),
 223  
                              YearPoemType.firstYear, YearPoemType.limitYear),
 224  
                              5, 1,
 225  
                              null, false, true, true));
 226  
   }
 227  
 
 228  
   /**
 229  
    * @param dateField date field to extract month field from 
 230  
    * @return month constituent of date
 231  
    */
 232  
   public Field monthField(Field dateField) {
 233  
 
 234  0
     Calendar when = when(dateField);
 235  
     // This isn't meant to be used, so we don't try to localize it
 236  
 
 237  0
     String displayName = dateField.getDisplayName() + " (month)";
 238  
 
 239  0
     return new Field(
 240  
         when == null ? null : new Integer(when.get(Calendar.MONTH) + 1),
 241  
         new BaseFieldAttributes(
 242  
             dateField.getName() + monthSuffix, displayName, null,
 243  
             dateField.getType().getNullable() ? new MonthPoemType(true) :
 244  
                                             new MonthPoemType(false),
 245  
             3, 1,
 246  
             null, false, true, true));
 247  
   }
 248  
 
 249  
   /**
 250  
    * @param dateField date field to extract day field from 
 251  
    * @return day constituent of date
 252  
    */
 253  
   public Field dayField(Field dateField) {
 254  
 
 255  0
     Calendar when = when(dateField);
 256  
     // This isn't meant to be used, so we don't try to localize it
 257  
 
 258  0
     String displayName = dateField.getDisplayName() + " (day)";
 259  
 
 260  0
     return new Field(
 261  
         when == null ? null : new Integer(when.get(Calendar.DAY_OF_MONTH)),
 262  
         new BaseFieldAttributes(
 263  
             dateField.getName() + daySuffix, displayName, null,
 264  
             dateField.getType().getNullable() ? new DayPoemType(true) :
 265  
                                             new DayPoemType(false),
 266  
             2, 1,
 267  
             null, false, true, true));
 268  
   }
 269  
   
 270  
   protected Calendar when(Field dateField) {
 271  0
     if (dateField.getRaw() == null) return null;
 272  0
     Calendar when = Calendar.getInstance();
 273  0
     when.setTime((java.util.Date)dateField.getRaw());
 274  0
     return when;
 275  
   }
 276  
 }