Coverage Report

##### Coverage Report - org.melati.template.YMDHMSTimestampAdaptor

Classes in this File | Line Coverage | Branch Coverage | Complexity
HourPoemType |
60% | 3/5
N/A | N/A
| 2.5;2.5
MinutePoemType |
60% | 3/5
N/A | N/A
| 2.5;2.5
SecondPoemType |
60% | 3/5
N/A | N/A
| 2.5;2.5
YMDHMSTimestampAdaptor |
80% | 20/25
33% | 12/36
| 2.5;2.5

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