View Javadoc

1   /*
2    * Created on 28.07.2004
3    *
4    * © 2004 Stoffwechsel Development GbR
5    * 
6    * 
7    */
8   package net.stff.ical.beans;
9   
10  import java.text.DateFormatSymbols;
11  import java.util.Calendar;
12  import java.util.Date;
13  import java.util.List;
14  import java.util.Locale;
15  import java.util.Vector;
16  
17  import javax.servlet.http.HttpServletRequest;
18  
19  import net.stff.ical.CalendarManager;
20  import net.stff.util.Initializable;
21  
22  /***
23   *  Stores calendar data for templating. Collates all currently known application data for the current user.
24   *
25   * @author     buntekuh
26   * @created    9. Juni 2003
27   */
28  public class CalendarData implements Initializable{
29  
30      public final static int DAYMODE = 1;
31      public final static int WEEKMODE = 2;
32      public final static int MONTHMODE = 3;
33      
34      public final static int NOMODE = 4;
35      public final static int EVENTMODE = 5;
36      public final static int RECURRENCEMODE = 6;
37  
38      static int[] javaWeek = { Calendar.SUNDAY, Calendar.MONDAY,
39              Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY,
40              Calendar.FRIDAY, Calendar.SATURDAY};
41  
42      int[] localizedWeek; //depends on the first day as defined in
43  
44      // calendar.FirstDayOfWeek()
45  
46      Calendar cal;
47      Locale locale;
48      DateFormatSymbols symbols;
49      Calendar today; //today neither tomorrow nor yesterday
50      int mode = MONTHMODE;
51      int eventMode= NOMODE;
52      Object days;
53      CalendarManager manager;
54      ICal currentICal;
55      String user;
56      Vector weekDays = new Vector(7); // the weekdays
57  
58      public CalendarData() {}
59  
60      /* 
61       * (non-Javadoc)
62       * @see net.stff.util.Initializable#init(javax.servlet.http.HttpServletRequest)
63       */
64      public void init(HttpServletRequest request) {
65          cal = Calendar.getInstance();
66          this.setLocale(request.getLocale());
67          symbols = new DateFormatSymbols(this.locale);
68          cal.setLenient(true);
69          today = Calendar.getInstance(this.locale);
70          manager= new CalendarManager();
71          setUser(request.getRemoteUser());
72      }
73  
74      /***
75       * @return Returns the user.
76       */
77      public String getUser() {
78          return user;
79      }
80      /***
81       * @param user The user to set.
82       */
83      public void setUser(String user) {
84          if (user == null){
85              this.user= "null";
86          }
87          else{
88              this.user = user;
89          }
90      }
91      
92      /***
93       * gets the user selected calendar.
94       * @return
95       */
96      public Calendar getCalendar() {
97          return cal;
98      }
99  
100     /***
101      * Selects a new calendar
102      * @param cal
103      */
104     public void setCalendar(Calendar cal) {
105         this.cal = cal;
106     }
107 
108     /***
109      * Sets the days property
110      * @param days The days attribute encompasses the data for each day that is currently being viewed. It either is a @see CalendarDay or a @see List thereof.
111      */
112     public void setDays(Object days) {
113         this.days = days;
114     }
115 
116     /***
117      * Sets the weekdays property
118      * @param weekdays Weekdays is a Vector of seven Integers consisting of @see java.util.Calendar.SUNDAY through to @see java.util.Calendar.SATURDAY ordered according to the current Locale.
119      */
120     public void setWeekDays(Vector weekdays) {
121         this.weekDays = weekdays;
122     }
123 
124     /***
125      * @return Returns the days property
126      */
127     public Object getDays() {
128         return days;
129     }
130 
131     /***
132      * @return Returns the weekdays property
133      */
134     public Vector getWeekDays() {
135         return weekDays;
136     }
137 
138     /***
139      * @return Returns the current view mode
140      */
141     public int getMode() {
142         return mode;
143     }
144 
145     /***
146      * Sets the current view mode
147      * @param mode
148      */
149     public void setMode(int mode) {
150         this.mode = mode;
151     }
152 
153     /***
154      * @return Returns the eventMode.
155      */
156     public int getEventMode() {
157         return eventMode;
158     }
159     /***
160      * @param eventMode The eventMode to set. The eventmode defines what additional data is to be viewed.
161      */
162     public void setEventMode(int eventMode) {
163         this.eventMode = eventMode;
164     }
165 
166     /*** 
167      * Sets the today property
168      * @param today Today is the special Date that is to be shown
169      */
170     public void setToday(Calendar today) {
171         this.today = today;
172     }
173 
174     /***
175      * @return Returns the today property
176      */
177     public Calendar getToday() {
178         return today;
179     }
180 
181     public int getDayOffset(int d) {
182         for (int n = 0; n < 7; n++) {
183             if (localizedWeek[n] == d) { return n; }
184         }
185 
186         return -1;
187     }
188 
189     /***
190      * Sets today to the current Date
191      */
192     public void today() {
193         cal.setTime(new Date());
194      }
195     
196     /***
197      * Adds a month to today
198      */
199     public void nextMonth() {
200         cal.add(Calendar.MONTH, 1);
201     }
202 
203     /***
204      * Removes a month from today
205      */
206     public void prevMonth() {
207         cal.add(Calendar.MONTH, -1);
208     }
209 
210     /***
211      * Adds a week to today
212      */
213     public void nextWeek() {
214         cal.add(Calendar.DATE, 7);
215     }
216 
217     /***
218      * Removes a week from today
219      */
220     public void prevWeek() {
221         cal.add(Calendar.DATE, -7);
222     }
223 
224     /***
225      * Adds a day to today
226      */
227     public void nextDay() {
228         cal.add(Calendar.DATE, 1);
229     }
230 
231     /***
232      * Removes a day from today
233      */
234     public void prevDay() {
235         cal.add(Calendar.DATE, -1);
236     }
237 
238     /***
239      * Sets today to a Date relative to the month of today
240      * @param selectedDay Represents the Date today is to be set to. A large value may change month and year to a respective Date in the future, a negative value to a month in the past.
241      */
242     public void setDay(String selectedDay){
243 		if (selectedDay != null){
244 			try{
245 				int thisDay = Integer.parseInt(selectedDay);
246 
247 				cal.set(Calendar.DATE, thisDay);
248 			}
249 			catch (NumberFormatException e){
250 			    System.out.println("Exception in method setDay: "+selectedDay+" must be an integer instead!");
251 			}
252 		}
253     }
254     
255     /***
256      * @return Returns the locale.
257      */
258     public Locale getLocale() {
259         return locale;
260     }
261 
262     /***
263      * @param locale The locale to set.
264      */
265     public void setLocale(Locale locale) {
266         this.locale = locale;
267         Date oldTime= cal.getTime();
268         cal = Calendar.getInstance(locale);
269         cal.setTime(oldTime);
270         weekDays = new Vector(7);
271         symbols = new DateFormatSymbols(locale);
272         localizedWeek = new int[7];
273 
274         for (int n = 0; n < 7; n++) {
275             int pos = (cal.getFirstDayOfWeek() - 1 + n) % 7;
276 
277             localizedWeek[n] = javaWeek[pos];
278             weekDays.add(n, getDayName(javaWeek[pos]));
279         }
280     }
281 
282     public String getMonthName(int month) {
283         return symbols.getMonths()[month];
284     }
285 
286     public String getDayName(int day) {
287         return symbols.getWeekdays()[day];
288     }
289     /***
290      * @return Returns the manager.
291      */
292     public CalendarManager getManager() {
293         return manager;
294     }
295     /***
296      * @return Returns the current calendar.
297      */
298     public ICal getCurrentICal() {
299         return currentICal;
300     }
301     /***
302      * @param currentICal The currentICal to set.
303      */
304     public void setCurrentICal(ICal currentICal) {
305         this.currentICal = currentICal;
306         if (currentICal == null) return;
307         this.currentICal.setShown(true);
308     }
309         
310     public void setCurrentICal(String current, String user) {
311         this.currentICal= manager.getCalendar(current, user);
312         if (currentICal == null) return;
313         this.currentICal.setShown(true);
314    }
315     
316    public List getEventsForPeriod(Date s, Date e, String user, int which){
317        return manager.getEventsForPeriod(s, e, user, which);
318    }
319    
320    public List getCalendarNames(){
321        return manager.getCalendarNames(user);
322    }
323    public List getCalendars(){
324        return manager.getCalendars(user);
325    }
326  }