View Javadoc

1   package net.sourceforge.blogentis.utils;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: DateSpecification.java,v 1.1 2004/10/22 17:34:05 tassos Exp $
23  //
24  
25  import java.util.Calendar;
26  import java.util.Date;
27  import java.util.GregorianCalendar;
28  
29  import org.apache.torque.util.Criteria;
30  
31  /***
32   * @author abas
33   */
34  public class DateSpecification {
35      protected int year = -1, month = -1, day = -1;
36  
37      protected Calendar startCal = null, endCal = null;
38  
39      public DateSpecification(Calendar c) {
40          year = c.get(Calendar.YEAR);
41          month = c.get(Calendar.MONTH) + 1;
42          day = c.get(Calendar.DAY_OF_MONTH);
43      }
44  
45      public DateSpecification(int year) {
46          this.year = year;
47      }
48  
49      public DateSpecification(int year, int month) {
50          this(year);
51          this.month = month;
52      }
53  
54      public DateSpecification(int year, int month, int day) {
55          this(year, month);
56          this.day = day;
57      }
58  
59      public int getDay() {
60          return day;
61      }
62  
63      public int getMonth() {
64          return month;
65      }
66  
67      public int getYear() {
68          return year;
69      }
70  
71      public boolean hasMonth() {
72          return month > -1;
73      }
74  
75      public boolean hasDay() {
76          return hasMonth() && day > -1;
77      }
78  
79      public Calendar getStart() {
80          if (startCal == null) {
81              startCal = new GregorianCalendar(year, hasMonth() ? month - 1
82                      : Calendar.JANUARY, hasDay() ? day : 1);
83              startCal.set(Calendar.HOUR_OF_DAY, 0);
84              startCal.set(Calendar.MINUTE, 0);
85              startCal.set(Calendar.SECOND, 0);
86              startCal.set(Calendar.MILLISECOND, 0);
87          }
88          return startCal;
89      }
90  
91      public Calendar getEnd() {
92          if (endCal == null) {
93              endCal = (Calendar)getStart().clone();
94              if (hasDay()) {
95                  endCal.add(Calendar.DAY_OF_YEAR, 1);
96              } else if (hasMonth()) {
97                  endCal.add(Calendar.MONTH, 1);
98              } else {
99                  endCal.add(Calendar.YEAR, 1);
100             }
101             endCal.add(Calendar.DAY_OF_YEAR, -1);
102             endCal.set(Calendar.HOUR_OF_DAY, 23);
103             endCal.set(Calendar.MINUTE, 59);
104             endCal.set(Calendar.SECOND, 59);
105             endCal.set(Calendar.MILLISECOND, 999);
106         }
107         return endCal;
108     }
109 
110     public DateSpecification getNext() {
111         Calendar c = getStart();
112         if (hasDay()) {
113             c.add(Calendar.DAY_OF_YEAR, 1);
114             return new DateSpecification(c.get(Calendar.YEAR),
115                 c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH));
116         } else if (hasMonth()) {
117             c.add(Calendar.MONTH, 1);
118             return new DateSpecification(c.get(Calendar.YEAR),
119                 c.get(Calendar.MONTH) + 1);
120         } else
121             return new DateSpecification(year + 1);
122     }
123 
124     public DateSpecification getPrev() {
125         Calendar c = getStart();
126         if (hasDay()) {
127             c.add(Calendar.DAY_OF_YEAR, -1);
128             return new DateSpecification(c.get(Calendar.YEAR),
129                 c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH));
130         } else if (hasMonth()) {
131             c.add(Calendar.MONTH, -1);
132             return new DateSpecification(c.get(Calendar.YEAR),
133                 c.get(Calendar.MONTH) + 1);
134         } else
135             return new DateSpecification(year - 1);
136     }
137 
138     public void setMonth(int month) {
139         if (month <= 0 || month > 12)
140             return;
141         this.month = month;
142     }
143 
144     public void setDay(int day) {
145         if (day <= 0 || day > 31)
146             return;
147         this.day = day;
148     }
149 
150     public DateSpecification(Date date) {
151         Calendar c = new GregorianCalendar();
152         c.setTime(date);
153         year = c.get(Calendar.YEAR);
154         month = c.get(Calendar.MONTH) + 1;
155         day = c.get(Calendar.DAY_OF_MONTH);
156     }
157 
158     public void addToCriteria(Criteria c, String tableName) {
159         c.add(tableName, getStart().getTime(), Criteria.GREATER_EQUAL);
160         Criteria.Criterion cc = c.getCriterion(tableName);
161         cc.and(c.getNewCriterion(cc.getTable(), cc.getColumn(),
162                 getEnd().getTime(), Criteria.LESS_THAN));
163     }
164 }