View Javadoc

1   /*
2    * Created on 18.06.2004
3    *
4    */
5   package net.stff.ical.gui.struts.formbeans;
6   
7   import java.text.SimpleDateFormat;
8   import java.util.Calendar;
9   import java.util.Collection;
10  import java.util.Date;
11  import java.util.GregorianCalendar;
12  import java.util.HashSet;
13  import java.util.Locale;
14  import java.util.ResourceBundle;
15  import java.util.Vector;
16  
17  import javax.servlet.http.HttpServletRequest;
18  
19  import net.stff.ical.beans.IEvent;
20  import net.stff.ical.beans.Recurrence;
21  
22  import org.apache.struts.action.ActionError;
23  import org.apache.struts.action.ActionErrors;
24  import org.apache.struts.action.ActionForm;
25  import org.apache.struts.action.ActionMapping;
26  import org.apache.struts.util.LabelValueBean;
27  
28  /***
29   * @author buntekuh
30   *  
31   */
32  public class RecurrenceFormBean extends ActionForm {
33  
34  	private boolean repeats; // if recurrence occurs at all
35  
36  	//private Recurrence recurrence;
37  
38  	//private HashSet exceptions;
39  	private IEvent event;
40  
41  	private String method;
42  
43  	private String uid;
44  
45  	//private String calendar;
46  
47  	private Vector freq;
48  
49  	private boolean monday = false;
50  
51  	private boolean tuesday = false;
52  
53  	private boolean wednesday = false;
54  
55  	private boolean thursday = false;
56  
57  	private boolean friday = false;
58  
59  	private boolean saturday = false;
60  
61  	private boolean sunday = false;
62  
63  	private String byday;
64  
65  	private String byMonthValue;
66  
67  	/***
68  	 * @return Returns the byMonthValue.
69  	 */
70  	public String getByMonthValue() {
71  		return byMonthValue;
72  	}
73  
74  	private String except;
75  
76  	private String exceptToRemove;
77  
78  	public void reset(ActionMapping mapping, javax.servlet.http.HttpServletRequest request) {
79  		monday = false;
80  		tuesday = false;
81  		wednesday = false;
82  		thursday = false;
83  		friday = false;
84  		saturday = false;
85  		sunday = false;
86  		repeats = false;
87  	}
88  
89  	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
90  
91  		if (!repeats)
92  			return null;
93  
94  		ActionErrors errors = new ActionErrors();
95  		if (event.getRecurrence().getUntil() == null) {
96  			errors.add("RecurrenceFormBean.missingUntil", new ActionError(
97  					"RecurrenceFormBean.missingUntil"));
98  		} else if (event.getRecurrence().getUntil().before(event.getDtstart())) {
99  			errors.add("RecurrenceFormBean.untilBeforeDtstart",
100 					new ActionError("RecurrenceFormBean.untilBeforeDtstart"));
101 		}
102 		if (event.getRecurrence().getFrequency() == Recurrence.WEEKLY) {
103 			if ((!monday) && (!tuesday) && (!wednesday) && (!thursday)
104 					&& (!friday) && (!saturday) && (!sunday)) {
105 				String we = event.getWeekdayAsString();
106 				if (we.equals("MO"))
107 					monday = true;
108 				else if (we.equals("TU"))
109 					tuesday = true;
110 				else if (we.equals("WE"))
111 					wednesday = true;
112 				else if (we.equals("TH"))
113 					thursday = true;
114 				else if (we.equals("FR"))
115 					friday = true;
116 				else if (we.equals("SA"))
117 					saturday = true;
118 				else if (we.equals("SU"))
119 					sunday = true;
120 				errors.add("RecurrenceFormBean.recurreWeekday",
121 						new ActionError("RecurrenceFormBean.recurreWeekday"));
122 			}
123 		}
124 
125 		return errors;
126 	}
127 
128 	public RecurrenceFormBean(IEvent event, Locale l) {
129 		LabelValueBean lvb;
130 
131 		this.event = event;
132 		repeats = event.isRecurs();
133 		uid = event.getUid();
134 		Recurrence recurrence = event.getRecurrence();
135 			if ((recurrence.getByLabel() != null) && (recurrence.getByValue() != null)) {
136 				byday = recurrence.getByValue();
137 				String[] days = event.getRecurrence().getByValue().split(",");
138 				for (int n = 0; n < days.length; n++) {
139 					String d = days[n];
140 					if (d.equals("MO"))
141 						monday = true;
142 					else if (d.equals("TU"))
143 						tuesday = true;
144 					else if (d.equals("WE"))
145 						wednesday = true;
146 					else if (d.equals("TH"))
147 						thursday = true;
148 					else if (d.equals("FR"))
149 						friday = true;
150 					else if (d.equals("SA"))
151 						saturday = true;
152 					else if (d.equals("SU"))
153 						sunday = true;
154 				}
155 			} else {
156 				byday = "none";
157 			}
158 			freq = new Vector(4);
159 			ResourceBundle bundle = ResourceBundle.getBundle(
160 					"resources.application", l);
161 			lvb = new LabelValueBean(bundle.getString("ical.view.DAILY"),
162 					"DAILY");
163 			freq.add(lvb);
164 			lvb = new LabelValueBean(bundle.getString("ical.view.WEEKLY"),
165 					"WEEKLY");
166 			freq.add(lvb);
167 			lvb = new LabelValueBean(bundle.getString("ical.view.MONTHLY"),
168 					"MONTHLY");
169 			freq.add(lvb);
170 			lvb = new LabelValueBean(bundle.getString("ical.view.YEARLY"),
171 					"YEARLY");
172 			freq.add(lvb);
173 
174 			//exceptions= new HashSet(3);
175 			SimpleDateFormat sdf = new SimpleDateFormat("F");
176 			//SimpleDateFormat sdf2= new SimpleDateFormat("F");
177 			Calendar c = event.getDtstartCal();
178 			GregorianCalendar cal = new GregorianCalendar();
179 			sdf.setCalendar(cal);
180 			//sdf2.setCalendar(cal);
181 
182 			StringBuffer buff = new StringBuffer();
183 			buff.append(sdf.format(event.getDtstart()));
184 			//int wd= Integer.parseInt(sdf2.format(event.getDtstart()));
185 			buff.append(Recurrence.WEEKDAYS[c.get(Calendar.DAY_OF_WEEK) - 1]);
186 			byMonthValue = buff.toString();
187 	}
188 
189 	/***
190 	 * @return Returns the friday.
191 	 */
192 	public boolean isFriday() {
193 		return friday;
194 	}
195 
196 	/***
197 	 * @param friday
198 	 *            The friday to set.
199 	 */
200 	public void setFriday(boolean friday) {
201 		this.friday = friday;
202 	}
203 
204 	/***
205 	 * public void setInterval(int interval) { this.interval= interval; } /**
206 	 * 
207 	 * @return Returns the method.
208 	 */
209 	public String getMethod() {
210 		return method;
211 	}
212 
213 	/***
214 	 * @param method
215 	 *            The method to set.
216 	 */
217 	public void setMethod(String method) {
218 		this.method = method;
219 	}
220 
221 	/***
222 	 * @return Returns the monday.
223 	 */
224 	public boolean isMonday() {
225 		return monday;
226 	}
227 
228 	/***
229 	 * @param monday
230 	 *            The monday to set.
231 	 */
232 	public void setMonday(boolean monday) {
233 		this.monday = monday;
234 	}
235 
236 	/***
237 	 * @return Returns the repeats.
238 	 */
239 	public boolean isRepeats() {
240 		return repeats;
241 	}
242 
243 	/***
244 	 * @param repeats
245 	 *            The repeats to set.
246 	 */
247 	public void setRepeats(boolean repeats) {
248 		this.repeats = repeats;
249 	}
250 
251 	/***
252 	 * @return Returns the saturday.
253 	 */
254 	public boolean isSaturday() {
255 		return saturday;
256 	}
257 
258 	/***
259 	 * @param saturday
260 	 *            The saturday to set.
261 	 */
262 	public void setSaturday(boolean saturday) {
263 		this.saturday = saturday;
264 	}
265 
266 	/***
267 	 * @return Returns the sunday.
268 	 */
269 	public boolean isSunday() {
270 		return sunday;
271 	}
272 
273 	/***
274 	 * @param sunday
275 	 *            The sunday to set.
276 	 */
277 	public void setSunday(boolean sunday) {
278 		this.sunday = sunday;
279 	}
280 
281 	/***
282 	 * @return Returns the thursday.
283 	 */
284 	public boolean isThursday() {
285 		return thursday;
286 	}
287 
288 	/***
289 	 * @param thursday
290 	 *            The thursday to set.
291 	 */
292 	public void setThursday(boolean thursday) {
293 		this.thursday = thursday;
294 	}
295 
296 	/***
297 	 * @return Returns the tuesday.
298 	 */
299 	public boolean isTuesday() {
300 		return tuesday;
301 	}
302 
303 	/***
304 	 * @param tuesday
305 	 *            The tuesday to set.
306 	 */
307 	public void setTuesday(boolean tuesday) {
308 		this.tuesday = tuesday;
309 	}
310 
311 	/***
312 	 * @return Returns the uid.
313 	 */
314 	public String getUid() {
315 		return uid;
316 	}
317 
318 	/***
319 	 * @param uid
320 	 *            The uid to set.
321 	 */
322 	public void setUid(String uid) {
323 		this.uid = uid;
324 	}
325 
326 	/***
327 	 * @return Returns the wednesday.
328 	 */
329 	public boolean isWednesday() {
330 		return wednesday;
331 	}
332 
333 	/***
334 	 * @param wednesday
335 	 *            The wednesday to set.
336 	 */
337 	public void setWednesday(boolean wednesday) {
338 		this.wednesday = wednesday;
339 	}
340 
341 	/***
342 	 * @return Returns the recurrence.
343 	 */
344 	public Recurrence getRecurrence() {
345 		return event.getRecurrence();
346 	}
347 
348 	/***
349 	 * @param recurrence
350 	 *            The recurrence to set.
351 	 */
352 	public void setRecurrence(Recurrence recurrence) {
353 	    event.setRecurrence(recurrence);
354 	}
355 
356 	/***
357 	 * @return Returns the freq.
358 	 */
359 	public Collection getFreq() {
360 		return freq;
361 	}
362 
363 	/***
364 	 * @return Returns the except.
365 	 */
366 	public String getExcept() {
367 		return except;
368 	}
369 
370 	/***
371 	 * @param except
372 	 *            The except to set.
373 	 */
374 	public void setExcept(String except) {
375 		this.except = except;
376 	}
377 
378 	/***
379 	 * @return Returns the exceptions.
380 	 */
381 	public Collection getExceptions() {
382 		return event.getRecurrence().getExDates();
383 	}
384 
385 	/***
386 	 * @param exceptions
387 	 *            The exceptions to set.
388 	 */
389 	public void setExceptions(HashSet exceptions) {
390 	    event.getRecurrence().setExDates(exceptions);
391 	}
392 
393 	public void addException(Date d) {
394 	    event.getRecurrence().addExdate(d);
395 	}
396 
397 	public void removeException(Date d) {
398 	    event.getRecurrence().removeExdate(d);
399 	}
400 
401 	/***
402 	 * @return Returns the exceptToRemove.
403 	 */
404 	public String getExceptToRemove() {
405 		return exceptToRemove;
406 	}
407 
408 	/***
409 	 * @param exceptToRemove
410 	 *            The exceptToRemove to set.
411 	 */
412 	public void setExceptToRemove(String exceptToRemove) {
413 		this.exceptToRemove = exceptToRemove;
414 	}
415 
416 	/***
417 	 * @return Returns the event.
418 	 */
419 	public IEvent getEvent() {
420 		return event;
421 	}
422 
423 	/***
424 	 * @return Returns the byday.
425 	 */
426 	public String getByday() {
427 		return byday;
428 	}
429 
430 	/***
431 	 * @param byday
432 	 *            The byday to set.
433 	 */
434 	public void setByday(String byday) {
435 		this.byday = byday;
436 	}
437 }