View Javadoc

1   /**
2    * 2010, Digitalis Informatica. All rights reserved. Distribuicao e Gestao de Informatica, Lda. Estrada de Paco de Arcos
3    * num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999 http://www.digitalis.pt
4    */
5   
6   package pt.digitalis.dif.dem.objects.parameters.constraints.impl.date;
7   
8   import java.text.ParseException;
9   import java.util.Calendar;
10  import java.util.Date;
11  
12  import pt.digitalis.dif.dem.interfaces.IStageInstance;
13  import pt.digitalis.utils.common.DateUtils;
14  
15  /**
16   * Date in Past parameter constraint implementation.<br>
17   * Will validate if the given date is in the past as it is mandatory.
18   * 
19   * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a><br/>
20   * @created 2010/09/14
21   */
22  public class ParameterConstraintDateInPastImpl extends ParameterConstraintDateImpl {
23  
24      /**
25       * @param value
26       *            the date to see if it's in the past
27       * @return T if the given date is not after the current one
28       */
29      private boolean isDateInPast(Date value)
30      {
31          Calendar calendarValue = Calendar.getInstance();
32          calendarValue.setTime(value);
33  
34          return !Calendar.getInstance().before(calendarValue);
35      }
36  
37      /**
38       * @see pt.digitalis.dif.dem.objects.parameters.constraints.impl.date.ParameterConstraintDateImpl#validateConstraint(java.lang.Object,
39       *      pt.digitalis.dif.dem.interfaces.IStageInstance)
40       */
41      @Override
42      public boolean validateConstraint(Object value, IStageInstance stageInstance)
43      {
44          if (value == null)
45              return true;
46          else if (value instanceof Date)
47          {
48              lastTestedValue = DateUtils.simpleDateToString((Date) value);
49  
50              return isDateInPast((Date) value);
51          }
52          else if (value instanceof String)
53              return this.validateConstraint(value.toString(), stageInstance);
54          else
55              return false;
56      }
57  
58      /**
59       * @see pt.digitalis.dif.dem.objects.parameters.constraints.impl.date.ParameterConstraintDateImpl#validateConstraint(java.lang.String,
60       *      pt.digitalis.dif.dem.interfaces.IStageInstance)
61       */
62      @Override
63      public boolean validateConstraint(String value, IStageInstance stageInstance)
64      {
65          if (super.validateConstraint(value, stageInstance))
66          {
67              try
68              {
69                  return isDateInPast(DateUtils.stringToSimpleDate(value));
70              }
71              catch (ParseException e)
72              {
73                  return false;
74              }
75          }
76          else
77              return false;
78      }
79  }