Coverage Report - org.melati.template.YMDDateAdaptor
 
Classes in this File Line Coverage Branch Coverage Complexity
DayPoemType
60%
3/5
N/A
2.438
MonthPoemType
60%
6/10
33%
1/3
2.438
YMDDateAdaptor
62%
27/43
38%
10/26
2.438
YearPoemType
60%
3/5
N/A
2.438
 
 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  6
     super(nullable);
 76  6
     setRawRange(new Integer(low), new Integer(limit));
 77  6
   }
 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  6
     super(nullable);
 103  6
     setRawRange(new Integer(1), new Integer(13));
 104  6
   }
 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  72
     int m = ((Integer)raw).intValue();
 113  72
     switch (style) {
 114  
       case DateFormat.FULL: case DateFormat.LONG:
 115  0
         return locale.monthName(m);
 116  
       case DateFormat.MEDIUM:
 117  72
         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  6
     super(nullable);
 143  6
     setRawRange(new Integer(1), new Integer(32));
 144  6
   }
 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  4
 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  12
     String fullName = fieldName + suffix;
 176  12
     String value = context.getFormField(fullName);
 177  12
     if (value == null)
 178  0
       throw new MissingFieldException(this, fieldName, fullName);
 179  12
     return value;
 180  
   }
 181  
 
 182  
   @Override
 183  
   public Object rawFrom(ServletTemplateContext context, String fieldName) {
 184  2
     String year = getFormOrDie(context, fieldName, yearSuffix);
 185  2
     String month = getFormOrDie(context, fieldName, monthSuffix);
 186  2
     String day = getFormOrDie(context, fieldName, daySuffix);
 187  
 
 188  2
     if (year.equals("") && month.equals("") && day.equals(""))
 189  2
       return null;
 190  0
     else if (!year.equals("") && !month.equals("") ) {
 191  0
       if (day.equals("")) // MYDate template need default day
 192  0
         day = "1";
 193  0
       Calendar cal = Calendar.getInstance();
 194  0
       cal.set(Integer.parseInt(year),
 195  0
               Integer.parseInt(month) - 1,
 196  0
               Integer.parseInt(day));
 197  0
       return new Date(cal.getTime().getTime());
 198  
     } else {
 199  0
       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  6
     Calendar when = when(dateField);
 210  
 
 211  
     // This isn't meant to be used, so we don't try to localise it
 212  6
     String displayName = dateField.getDisplayName() + " (year)";
 213  
 
 214  6
     return new Field<Integer>(
 215  0
         when == null ? null : new Integer(when.get(Calendar.YEAR)),
 216  
         new BaseFieldAttributes<Integer>(
 217  6
             dateField.getName() + yearSuffix,
 218  
             displayName,
 219  
             null,
 220  6
             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  6
     Calendar when = when(dateField);
 233  
     // This isn't meant to be used, so we don't try to localise it
 234  
 
 235  6
     String displayName = dateField.getDisplayName() + " (month)";
 236  
 
 237  6
     return new Field<Integer>(
 238  0
         when == null ? null : new Integer(when.get(Calendar.MONTH) + 1),
 239  
         new BaseFieldAttributes<Integer>(
 240  6
             dateField.getName() + monthSuffix, displayName, null,
 241  6
             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  6
     Calendar when = when(dateField);
 254  
     // This isn't meant to be used, so we don't try to localise it
 255  
 
 256  6
     String displayName = dateField.getDisplayName() + " (day)";
 257  
 
 258  6
     return new Field<Integer>(
 259  0
         when == null ? null : new Integer(when.get(Calendar.DAY_OF_MONTH)),
 260  
         new BaseFieldAttributes<Integer>(
 261  6
             dateField.getName() + daySuffix, displayName, null,
 262  6
             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  24
     if (dateField.getRaw() == null) return null;
 270  0
     Calendar when = Calendar.getInstance();
 271  0
     when.setTime((java.util.Date)dateField.getRaw());
 272  0
     return when;
 273  
   }
 274  
 }