1 /*
2 * Scope: a generic MVC framework.
3 * Copyright (c) 2000-2002, Steve Meyfroidt
4 * All rights reserved.
5 * Email: smeyfroi@users.sourceforge.net
6 *
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * Neither the name "Scope" nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 *
37 * $Id: DateCriteriaModel.java,v 1.4 2002/06/17 09:56:00 ludovicc Exp $
38 */
39
40
41 package samples.filefind;
42
43
44 import java.util.Calendar;
45 import java.util.Date;
46 import org.apache.commons.logging.LogFactory;import org.apache.commons.logging.Log;
47 import org.scopemvc.core.Selector;
48 import org.scopemvc.model.basic.BasicModel;
49 import samples.filefind.SearchViewModel.ComplexFileFilter;
50
51
52 /***
53 * Part of the SearchViewModel.
54 *
55 * @author <A HREF="mailto:daniel.michalik@autel.cz">Daniel Michalik</A>
56 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A>
57 * @version $Revision: 1.4 $ $Date: 2002/06/17 09:56:00 $
58 */
59 public class DateCriteriaModel extends BasicModel {
60
61
62 private static final Log LOG = LogFactory.getLog(DateCriteriaModel.class);
63
64
65 public final static Selector DATE_FROM =
66 Selector.fromString("dateFrom");
67 public final static Selector DATE_TO =
68 Selector.fromString("dateTo");
69 public final static Selector LAST_DAYS =
70 Selector.fromString("lastDays");
71 public final static Selector LAST_MONTHS =
72 Selector.fromString("lastMonths");
73 public final static Selector LAST_DAYS_ENABLED =
74 Selector.fromString("lastDaysEnabled");
75 public final static Selector LAST_MONTHS_ENABLED =
76 Selector.fromString("lastMonthsEnabled");
77 public final static Selector INTERVAL_ENABLED =
78 Selector.fromString("intervalEnabled");
79
80
81 private Date dateFrom;
82 private Date dateTo;
83 private int lastDays = 1;
84 private int lastMonths = 1;
85 private boolean lastDaysEnabled;
86 private boolean lastMonthsEnabled = true;
87 private boolean intervalEnabled;
88
89
90 DateCriteriaModel() {
91 Calendar c = Calendar.getInstance();
92 dateTo = c.getTime();
93 c.add(Calendar.MONTH, -1);
94 dateFrom = c.getTime();
95 }
96
97
98 public Date getDateFrom() {
99 return dateFrom;
100 }
101
102
103 public void setDateFrom(Date dateFrom) {
104 this.dateFrom = dateFrom;
105 fireModelChange(VALUE_CHANGED, DATE_FROM);
106 }
107
108
109 public Date getDateTo() {
110 return dateTo;
111 }
112
113
114 public void setDateTo(Date dateTo) {
115 this.dateTo = dateTo;
116 fireModelChange(VALUE_CHANGED, DATE_TO);
117 }
118
119
120 public int getLastDays() {
121 return lastDays;
122 }
123
124
125 public void setLastDays(int lastDays) {
126 this.lastDays = lastDays;
127 fireModelChange(VALUE_CHANGED, LAST_DAYS);
128 }
129
130
131 public int getLastMonths() {
132 return lastMonths;
133 }
134
135
136 public void setLastMonths(int lastMonths) {
137 this.lastMonths = lastMonths;
138 fireModelChange(VALUE_CHANGED, LAST_MONTHS);
139 }
140
141
142 public boolean isLastDaysEnabled() {
143 return lastDaysEnabled;
144 }
145
146
147 public void setLastDaysEnabled(boolean lastDaysEnabled) {
148 this.lastDaysEnabled = lastDaysEnabled;
149 if (lastDaysEnabled) {
150 setLastMonthsEnabled(false);
151 setIntervalEnabled(false);
152 }
153 fireModelChange(VALUE_CHANGED, LAST_DAYS_ENABLED);
154 }
155
156
157 public boolean isLastMonthsEnabled() {
158 return lastMonthsEnabled;
159 }
160
161
162 public void setLastMonthsEnabled(boolean lastMonthsEnabled) {
163 this.lastMonthsEnabled = lastMonthsEnabled;
164 if (lastMonthsEnabled) {
165 setLastDaysEnabled(false);
166 setIntervalEnabled(false);
167 }
168 fireModelChange(VALUE_CHANGED, LAST_MONTHS_ENABLED);
169 }
170
171
172 public boolean isIntervalEnabled() {
173 return intervalEnabled;
174 }
175
176
177 public void setIntervalEnabled(boolean intervalEnabled) {
178 this.intervalEnabled = intervalEnabled;
179 if (intervalEnabled) {
180 setLastDaysEnabled(false);
181 setLastMonthsEnabled(false);
182 }
183 fireModelChange(VALUE_CHANGED, INTERVAL_ENABLED);
184 }
185
186
187 void prepareFilter(SearchViewModel.ComplexFileFilter filter) {
188 if (isIntervalEnabled()) {
189 filter.dateFrom = getDateFrom();
190 filter.dateTo = getDateTo();
191 return;
192 }
193 Calendar cal = Calendar.getInstance();
194 filter.dateTo = null;
195 filter.dateFrom = null;
196 if (isLastDaysEnabled()) {
197 cal.add(Calendar.DATE, -getLastDays());
198 } else if (isLastMonthsEnabled()) {
199 cal.add(Calendar.MONTH, -getLastMonths());
200 } else {
201 throw new IllegalStateException("No DateCriteria style is enabled");
202 }
203 filter.dateFrom = cal.getTime();
204 }
205 }
This page was automatically generated by Maven