View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.custom.schedule.util;
21  
22  import java.text.ParseException;
23  import java.text.SimpleDateFormat;
24  import java.util.Calendar;
25  import java.util.Date;
26  import java.util.GregorianCalendar;
27  import java.util.Iterator;
28  import java.util.TimeZone;
29  import java.util.TreeSet;
30  
31  import javax.faces.component.UIComponent;
32  import javax.faces.context.FacesContext;
33  import javax.faces.el.ValueBinding;
34  
35  import org.apache.myfaces.custom.schedule.model.Interval;
36  
37  
38  /**
39   * <p>
40   * Some utility methods
41   * </p>
42   *
43   * @author Jurgen Lust (latest modification by $Author: jlust $)
44   * @author Kito Mann (some methods taken from the source code of his book 'JavaServer Faces in Action', which is also released under the Apache License
45   * @author Bruno Aranda (adaptation of Jurgen's code to myfaces)
46   * @version $Revision: 387334 $
47   */
48  public class ScheduleUtil
49  {
50      //~ Class Variables --------------------------------------------------------
51      private final static String DATE_ID_PATTERN = "yyyyMMdd";
52      
53      //~ Constructors -----------------------------------------------------------
54      /**
55       * This class should not be instantiated, it only contains static methods
56       */
57      private ScheduleUtil() {
58          super();
59      }
60      
61      //~ Methods ----------------------------------------------------------------
62  
63      /**
64       * <p>
65       * Check if the value of a UIComponent is a value or method binding
66       * expression.
67       * </p>
68       *
69       * @param value the value to check
70       *
71       * @return whether the value is a binding expression
72       */
73      public static boolean isBindingExpression(Object value)
74      {
75          boolean returnboolean =
76              ((value != null) && value instanceof String &&
77              ((String) value).startsWith("#{") &&
78              ((String) value).endsWith("}"));
79  
80          return returnboolean;
81      }
82  
83      /**
84       * <p>
85       * Get the boolean value of a UIComponent, even if it is a value
86       * binding expression.
87       * </p>
88       *
89       * @param component the component
90       * @param property the property
91       * @param key the key of the value binding
92       * @param defaultValue the default value
93       *
94       * @return the boolean value
95       */
96      public static boolean getBooleanProperty(
97          UIComponent component,
98          Boolean property,
99          String key,
100         boolean defaultValue
101     )
102     {
103         if (property != null) {
104             return property.booleanValue();
105         } else {
106             ValueBinding binding =
107                 (ValueBinding) component.getValueBinding(key);
108 
109             if (binding != null) {
110                 Boolean value =
111                     (Boolean) binding.getValue(
112                         FacesContext.getCurrentInstance()
113                     );
114 
115                 if (value != null) {
116                     return value.booleanValue();
117                 }
118             }
119         }
120 
121         return defaultValue;
122     }
123 
124     /**
125      * <p>
126      * Get the boolean value of an attribute
127      * </p>
128      *
129      * @param attributeValue the attribute value
130      * @param valueIfNull the default value
131      *
132      * @return the boolean value
133      */
134     public static boolean getBooleanValue(
135         Object attributeValue,
136         boolean valueIfNull
137     )
138     {
139         if (attributeValue == null) {
140             return valueIfNull;
141         }
142 
143         if (attributeValue instanceof String) {
144             return ((String) attributeValue).equalsIgnoreCase("true");
145         } else {
146             return ((Boolean) attributeValue).booleanValue();
147         }
148     }
149 
150     /**
151      * <p>
152      * Get the float value of a UIComponent, even if it is a value
153      * binding expression.
154      * </p>
155      *
156      * @param component the component
157      * @param property the property
158      * @param key the key of the value binding
159      * @param defaultValue the default value
160      *
161      * @return the float value
162      */
163     public static float getFloatProperty(
164         UIComponent component,
165         Float property,
166         String key,
167         float defaultValue
168     )
169     {
170         if (property != null) {
171             return property.floatValue();
172         } else {
173             ValueBinding binding =
174                 (ValueBinding) component.getValueBinding(key);
175 
176             if (binding != null) {
177                 Float value =
178                     (Float) binding.getValue(FacesContext.getCurrentInstance());
179 
180                 if (value != null) {
181                     return value.floatValue();
182                 }
183             }
184         }
185 
186         return defaultValue;
187     }
188 
189     /**
190      * <p>
191      * Get the hashcode for the truncated date
192      * </p>
193      *
194      * @param date the date
195      *
196      * @return the hashCode of the truncated date
197      */
198     public static int getHashCodeForDay(Date date,TimeZone tz)
199     {
200       Calendar calendar = getCalendarInstance(date, tz);
201 
202       return new Integer(calendar.get(Calendar.ERA)).hashCode() ^
203         new Integer(calendar.get(Calendar.YEAR)).hashCode() ^
204         new Integer(calendar.get(Calendar.DAY_OF_YEAR)).hashCode();
205     }
206 
207     /**
208      * <p>
209      * Get the int value of a UIComponent, even if it is a value
210      * binding expression.
211      * </p>
212      *
213      * @param component the component
214      * @param property the property
215      * @param key the key of the value binding
216      * @param defaultValue the default value
217      *
218      * @return the int value
219      */
220     public static int getIntegerProperty(
221         UIComponent component,
222         Integer property,
223         String key,
224         int defaultValue
225     )
226     {
227         if (property != null) {
228             return property.intValue();
229         } else {
230             ValueBinding binding =
231                 (ValueBinding) component.getValueBinding(key);
232 
233             if (binding != null) {
234                 Integer value =
235                     (Integer) binding.getValue(
236                         FacesContext.getCurrentInstance()
237                     );
238 
239                 if (value != null) {
240                     return value.intValue();
241                 }
242             }
243         }
244 
245         return defaultValue;
246     }
247     
248     /**
249      * <p>
250      * Get the object value of a UIComponent, even if it is a value
251      * binding expression.
252      * </p>
253      *
254      * @param component the component
255      * @param property the property
256      * @param key the key of the value binding
257      * @param defaultValue the default value
258      *
259      * @return the object value
260      */
261     public static Object getObjectProperty(
262         UIComponent component,
263         Object property,
264         String key,
265         Object defaultValue
266     )
267     {
268         if (property != null) {
269             return property;
270         } else {
271             ValueBinding binding =
272                 (ValueBinding) component.getValueBinding(key);
273 
274             if (binding != null) {
275                 return binding.getValue(FacesContext.getCurrentInstance());
276             }
277         }
278 
279         return defaultValue;
280     }
281 
282     /**
283      * <p>
284      * Check if the 2 dates are in the same day.
285      * </p>
286      *
287      * @param date1 the first date
288      * @param date2 the second date
289      *
290      * @return whether the dates are in the same day
291      */
292     public static boolean isSameDay(
293         Date date1,
294         Date date2,
295         TimeZone tz
296     )
297     {
298         if ((date1 == null) || (date2 == null)) {
299             return false;
300         }
301 
302         Calendar calendar1 = getCalendarInstance(date1, tz);
303         Calendar calendar2 = getCalendarInstance(date2, tz);
304 
305         return (calendar1.get(Calendar.ERA) == calendar2.get(Calendar.ERA) &&
306             calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR) &&
307             calendar1.get(Calendar.DAY_OF_YEAR) == calendar2.get(Calendar.DAY_OF_YEAR));
308     }
309 
310     /**
311      * <p>
312      * Check if the 2 dates are at the same time of day.
313      * </p>
314      *
315      * @param date1 the first date
316      * @param date2 the second date
317      *
318      * @return whether the dates are at the same time of day
319      */
320     public static boolean isSameTime(Date date1, Date date2)
321     {
322         if ((date1 == null) || (date2 == null))
323         {
324             return false;
325         }
326 
327         Calendar calendar1 = getCalendarInstance(date1, null);
328         Calendar calendar2 = getCalendarInstance(date2, null);
329 
330         return (calendar1.get(Calendar.HOUR_OF_DAY) == calendar2.get(Calendar.HOUR_OF_DAY) &&
331                 calendar1.get(Calendar.MINUTE) == calendar2.get(Calendar.MINUTE) &&
332                 calendar1.get(Calendar.SECOND) == calendar2.get(Calendar.SECOND));
333     }
334 
335 
336     /**
337      * <p>
338      * Check if two sets of intervals are equivalent.
339      * Intervals are equivalent if their label is the same and they begin and end
340      * at the same time of day.
341      * </p>
342      *
343      * @param intervals1 the first set of intervals
344      * @param intervals2 the second set of intervals
345      *
346      * @return whether the dates are at the same time of day
347      */
348     public static boolean areEquivalentIntervals(TreeSet intervals1, TreeSet intervals2)
349     {
350         if (intervals1 == null || intervals2 == null)
351         {
352             return !(intervals1 != null || intervals2 != null);
353         }
354         
355         if (intervals1.size() == intervals2.size())
356         {
357             Iterator it1 = intervals1.iterator();
358             Iterator it2 = intervals2.iterator();
359             
360             while (it1.hasNext())
361             {
362                 if (!((Interval) it1.next()).isEquivalent(((Interval) it2.next()))) {
363                     return false;
364                 }
365             }
366             
367             return true;
368         }
369         else
370         {
371             return false;
372         }
373     }
374     
375     /**
376      * <p>
377      * Get the String value of a UIComponent, even if it is a value
378      * binding expression.
379      * </p>
380      *
381      * @param component the component
382      * @param property the property
383      * @param key the key of the value binding
384      * @param defaultValue the default value
385      *
386      * @return the String value
387      */
388     public static String getStringProperty(
389         UIComponent component,
390         String property,
391         String key,
392         String defaultValue
393     )
394     {
395         if (property != null) {
396             return property;
397         } else {
398             ValueBinding binding =
399                 (ValueBinding) component.getValueBinding(key);
400 
401             if (binding != null) {
402                 return (String) binding.getValue(
403                     FacesContext.getCurrentInstance()
404                 );
405             }
406         }
407 
408         return defaultValue;
409     }
410 
411     /**
412      * <p>
413      * Check if the value of the given component can be modified
414      * </p>
415      *
416      * @param component the component
417      *
418      * @return whether the value can be modified
419      */
420     public static boolean canModifyValue(UIComponent component)
421     {
422         boolean returnboolean =
423             (component.isRendered() &&
424             !getBooleanValue(component.getAttributes().get("readonly"), false) &&
425             !getBooleanValue(component.getAttributes().get("disabled"), false));
426 
427         return returnboolean;
428     }
429 
430     /**
431      * <p>
432      * Compare 2 dates after truncating them.
433      * </p>
434      *
435      * @param date1 the first date
436      * @param date2 the second date
437      *
438      * @return the comparison
439      */
440     public static int compareDays(
441         Date date1,
442         Date date2,
443         TimeZone tz
444     )
445     {
446         if (date1 == null) {
447             return -1;
448         }
449 
450         if (date2 == null) {
451             return 1;
452         }
453 
454         return truncate(date1,tz).compareTo(truncate(date2,tz));
455     }
456 
457     /**
458      * truncate the given Date to 00:00:00 that same day
459      *
460      * @param date the date that should be truncated
461      * @return the truncated date
462      */
463     public static Date truncate(Date date, TimeZone tz)
464     {
465         if (date == null) return null;
466         Calendar cal = getCalendarInstance(date, tz);
467         cal.set(Calendar.HOUR_OF_DAY, 0);
468         cal.set(Calendar.MINUTE, 0);
469         cal.set(Calendar.SECOND, 0);
470         cal.set(Calendar.MILLISECOND, 0);
471         return cal.getTime();
472     }
473     
474     /**
475      * get a String identifying this date
476      * 
477      * @param date the date
478      * @return the identifier for this date
479      */
480     public static String getDateId(Date date, TimeZone timeZone)
481     {
482         if (date == null) return "NULL";
483         return getDateIdFormater(timeZone).format(date);
484     }
485     
486     /**
487      * get a date from the given date ID
488      * 
489      * @param id the date ID
490      * @return the date
491      */
492     public static Date getDateFromId(String id, TimeZone timeZone)
493     {
494         if (id == null || id.length() == 0 || "null".equals(id)) return null;
495         try
496         {
497             return getDateIdFormater(timeZone).parse(id);
498         }
499         catch (ParseException e)
500         {
501             return null;
502         }
503     }
504     
505     public static Calendar getCalendarInstance(Date date, TimeZone timeZone)
506     {
507         Calendar cal = GregorianCalendar.getInstance(
508                 timeZone != null ? timeZone : TimeZone.getDefault());
509         cal.setFirstDayOfWeek(Calendar.MONDAY);
510         cal.setTime(date);
511 
512         return cal;
513     }
514     
515     private static SimpleDateFormat getDateIdFormater(TimeZone timeZone)
516     {
517         SimpleDateFormat format = new SimpleDateFormat(DATE_ID_PATTERN);
518         
519         format.setTimeZone(timeZone);
520         
521         return format;
522     }
523 }
524 //The End