View Javadoc

1   /*
2    * Created on 30.03.2004
3    *
4    * © 2004 Stoffwechsel Development GbR
5    */
6   package net.stff.ical.gui.struts.formbeans;
7   
8   import java.util.Calendar;
9   import java.util.Iterator;
10  import java.util.Locale;
11  import java.util.Vector;
12  
13  import javax.servlet.http.HttpServletRequest;
14  
15  import net.stff.ical.beans.CalendarData;
16  import net.stff.ical.beans.CommBean;
17  import net.stff.ical.validators.DateValidator;
18  import net.stff.util.SessionObject;
19  
20  import org.apache.struts.action.ActionError;
21  import org.apache.struts.action.ActionErrors;
22  import org.apache.struts.action.ActionForm;
23  import org.apache.struts.action.ActionMapping;
24  
25  /***
26   * A versatile formbean used within the struts application
27   * @author buntekuh
28   *
29   */
30  public class CommFormBean extends ActionForm{
31  
32      String method;
33      
34      private CommBean bean;
35  
36      private final static String REGEX = "[:,;.///#]+";
37        
38      public CommFormBean(){
39          super();
40          bean= new CommBean();
41      }
42  	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request){
43  		
44  	    CalendarData data = (CalendarData)SessionObject.get(request).getObject("icalData", request);
45  
46  		Vector errors= new Vector();
47  		ActionErrors errs= new ActionErrors();
48  		if (!bean.getDate().trim().equals("")){
49  			String[] dates= bean.getDate().split(REGEX);
50  			if (dates.length == 3){
51  				Calendar c= Calendar.getInstance();
52  				c.setLenient(true);
53  				c.set(Calendar.YEAR, Integer.parseInt(dates[2]));
54  				c.set(Calendar.MONTH, Integer.parseInt(dates[1]));
55  				c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dates[0]));
56  				
57  				errors.addAll(DateValidator.validateDate(Integer.toString(c.get(Calendar.YEAR)), Integer.toString(c.get(Calendar.MONTH)), Integer.toString(c.get(Calendar.DAY_OF_MONTH)), null, null, Locale.getDefault(), DateValidator.DAYFLAG | DateValidator.MONTHFLAG | DateValidator.YEARFLAG));
58  			}
59  			else errors.add("CommFormBean.invalidDate");
60  		}
61  		if (!bean.getCalendar().trim().equals("")){
62  		    if (data.getManager().getCalendar(bean.getCalendar(), request.getRemoteUser()) == null){
63  		        errors.add("CommFormBean.InvalidCalendar");
64  		    }
65  		}
66  		if ((data.getCurrentICal() == null) && (bean.getEvent().equals(""))) errors.add("CommFormBean.SelectCalendar");
67  		if (errors.size() == 0){
68  			return null;
69  		}
70  		Iterator i= errors.iterator();
71  		while (i.hasNext()){
72  			String err= (String)i.next();
73  			errs.add(err, new ActionError(err));
74  		}
75  		return errs;
76  	}
77      /***
78       * @return Returns the bean.
79       */
80      public CommBean getBean() {
81          return bean;
82      }
83      /***
84       * @param bean The bean to set.
85       */
86      public void setBean(CommBean bean) {
87          this.bean = bean;
88      }
89      /***
90       * @return Returns the method.
91       */
92      public String getMethod() {
93          return method;
94      }
95      /***
96       * @param method The method to set.
97       */
98      public void setMethod(String method) {
99          this.method = method;
100     }
101 }