Coverage Report - org.melati.template.YMDHMSTimestampAdaptor
 
Classes in this File Line Coverage Branch Coverage Complexity
HourPoemType
0%
0/5
N/A
1.357
MinutePoemType
0%
0/5
N/A
1.357
SecondPoemType
0%
0/5
N/A
1.357
YMDHMSTimestampAdaptor
12%
3/25
0%
0/36
1.357
 
 1  
 /*
 2  
  * $Source: /usr/cvsroot/melati/melati/src/main/java/org/melati/template/YMDHMSTimestampAdaptor.java,v $
 3  
  * $Revision: 1.11 $
 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.util.Calendar;
 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  0
     super(nullable);
 66  0
     setRawRange(new Integer(0), new Integer(24));
 67  0
   }
 68  
 
 69  
   protected boolean _canRepresent(SQLPoemType other) {
 70  0
     return other instanceof HourPoemType;
 71  
   }
 72  
 
 73  
   /**
 74  
    * {@inheritDoc}
 75  
    * @see java.lang.Object#toString()
 76  
    */
 77  
   public String toString() {
 78  0
     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  0
     super(nullable);
 92  0
     setRawRange(new Integer(0), new Integer(60));
 93  0
   }
 94  
 
 95  
   protected boolean _canRepresent(SQLPoemType other) {
 96  0
     return other instanceof MinutePoemType;
 97  
   }
 98  
 
 99  
   /**
 100  
    * {@inheritDoc}
 101  
    * @see java.lang.Object#toString()
 102  
    */
 103  
   public String toString() {
 104  0
     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  0
     super(nullable);
 118  0
     setRawRange(new Integer(0), new Integer(60));
 119  0
   }
 120  
 
 121  
   protected boolean _canRepresent(SQLPoemType other) {
 122  0
     return other instanceof SecondPoemType;
 123  
   }
 124  
 
 125  
   /**
 126  
    * {@inheritDoc}
 127  
    * @see java.lang.Object#toString()
 128  
    */
 129  
   public String toString() {
 130  0
     return super.toString() + " (seconds)";
 131  
   }
 132  
 }
 133  
 
 134  
 
 135  
 /**
 136  
  * An adaptor for a string date in YMDHMST format.
 137  
  */
 138  1
 public class YMDHMSTimestampAdaptor extends YMDDateAdaptor {
 139  
   private static final String
 140  
       hourSuffix = "-hour",
 141  
       minuteSuffix = "-minute",
 142  
       secondSuffix = "-second";
 143  
 
 144  1
   private static final YMDHMSTimestampAdaptor me = new YMDHMSTimestampAdaptor();
 145  
 
 146  
   /**
 147  
    * @return the instance.
 148  
    */
 149  
   public static YMDHMSTimestampAdaptor getIt() {
 150  1
     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  0
     String year = getFormOrDie(context, fieldName, yearSuffix);
 160  0
     String month = getFormOrDie(context, fieldName, monthSuffix);
 161  0
     String day = getFormOrDie(context, fieldName, daySuffix);
 162  0
     String hour = getFormOrDie(context, fieldName, hourSuffix);
 163  0
     String minute = getFormOrDie(context, fieldName, minuteSuffix);
 164  0
     String second = getFormOrDie(context, fieldName, secondSuffix);
 165  
 
 166  0
     if (year.equals("") && month.equals("") && day.equals("") &&
 167  
         hour.equals("") && minute.equals("") && second.equals(""))
 168  0
       return null;
 169  0
     else if (!year.equals("") && !month.equals("") && !day.equals("") &&
 170  
              !hour.equals("") && !minute.equals("") && !second.equals("")) {
 171  0
       Calendar cal = Calendar.getInstance();
 172  0
       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  0
       return new Timestamp(cal.getTime().getTime());
 179  
     } else {
 180  0
       throw new PartlyNullException(fieldName);
 181  
     }
 182  
   }
 183  
 
 184  
   /**
 185  
    * @param field the field to copy
 186  
    * @return an hour field
 187  
    */
 188  
   public Field hourField(Field field) {
 189  
 
 190  0
     Calendar when = when(field);
 191  
 
 192  
     // This isn't meant to be used, so we don't try to localize it
 193  
 
 194  0
     String displayName = field.getDisplayName() + " (hour)";
 195  
 
 196  0
     return new Field(
 197  
         when == null ? null : new Integer(when.get(Calendar.HOUR_OF_DAY)),
 198  
         new BaseFieldAttributes(
 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 minuteField(Field field) {
 211  
 
 212  0
     Calendar when = when(field);
 213  
 
 214  
     // This isn't meant to be used, so we don't try to localize it
 215  
 
 216  0
     String displayName = field.getDisplayName() + " (minutes)";
 217  
 
 218  0
     return new Field(
 219  
         when == null ? null : new Integer(when.get(Calendar.MINUTE)),
 220  
         new BaseFieldAttributes(
 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 secondField(Field field) {
 233  
 
 234  0
     Calendar when = when(field);
 235  
 
 236  
     // This isn't meant to be used, so we don't try to localize it
 237  
 
 238  0
     String displayName = field.getDisplayName() + " (seconds)";
 239  
 
 240  0
     return new Field(
 241  
         when == null ? null : new Integer(when.get(Calendar.SECOND)),
 242  
         new BaseFieldAttributes(
 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  
 }