View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.custom.schedule.model;
21  
22  import java.io.Serializable;
23  
24  import java.text.DateFormat;
25  import java.text.SimpleDateFormat;
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.Date;
29  import java.util.HashMap;
30  import java.util.Iterator;
31  import java.util.TreeSet;
32  
33  import org.apache.myfaces.custom.schedule.util.ScheduleEntryComparator;
34  
35  /**
36   * <p>
37   * A simple implementation of the ScheduleModel, not backed by any kind of
38   * datasource: entries have to be added manually.
39   * </p>
40   * 
41   * @author Jurgen Lust (latest modification by $Author: werpu $)
42   * @version $Revision: 371736 $
43   */
44  public class SimpleScheduleModel extends AbstractScheduleModel implements
45          Serializable
46  {
47      // ~ Instance fields
48      // --------------------------------------------------------
49  
50      /**
51       * serial id for serialisation versioning
52       */
53      private static final long serialVersionUID = 1L;
54  
55      private final TreeSet entries;
56  
57      private final HashMap holidays;
58  
59      private final DateFormat holidayFormat = new SimpleDateFormat("yyyyMMdd");
60  
61      // ~ Constructors
62      // -----------------------------------------------------------
63  
64      /**
65       * Creates a new SimpleScheduleModel object.
66       */
67      public SimpleScheduleModel()
68      {
69          this.entries = new TreeSet(new ScheduleEntryComparator());
70          this.holidays = new HashMap();
71      }
72  
73      // ~ Methods
74      // ----------------------------------------------------------------
75  
76      /**
77       * Set the name of a holiday.
78       * 
79       * @param date
80       *            the date
81       * @param holidayName
82       *            the name of the holiday
83       */
84      public void setHoliday(Date date, String holidayName)
85      {
86          if (date == null)
87          {
88              return;
89          }
90  
91          String key = holidayFormat.format(date);
92          holidays.put(key, holidayName);
93      }
94  
95      /**
96       * Add an entry to the model.
97       * 
98       * @param entry
99       *            the entry to add
100      */
101     public void addEntry(ScheduleEntry entry)
102     {
103         entries.add(entry);
104     }
105 
106     /**
107      * Remove an entry from the model.
108      * 
109      * @param entry
110      *            the entry to remove
111      */
112     public void removeEntry(ScheduleEntry entry)
113     {
114         entries.remove(entry);
115     }
116 
117     /**
118      * @see org.apache.myfaces.custom.schedule.model.ScheduleModel#removeSelectedEntry()
119      */
120     public void removeSelectedEntry()
121     {
122         if (!isEntrySelected())
123             return;
124         removeEntry(getSelectedEntry());
125         setSelectedEntry(null);
126         refresh();
127     }
128 
129     /**
130      * @see org.apache.myfaces.custom.schedule.model.AbstractScheduleModel#loadEntries(java.util.Date,
131      *      java.util.Date)
132      */
133     protected Collection loadEntries(Date startDate, Date endDate)
134     {
135         ArrayList selection = new ArrayList();
136 
137         for (Iterator entryIterator = entries.iterator(); entryIterator
138                 .hasNext();)
139         {
140             ScheduleEntry entry = (ScheduleEntry) entryIterator.next();
141 
142             if (entry.getEndTime().before(startDate)
143                     || entry.getStartTime().after(endDate))
144             {
145                 continue;
146             }
147 
148             selection.add(entry);
149         }
150 
151         return selection;
152     }
153 
154     /**
155      * @see org.apache.myfaces.custom.schedule.model.AbstractScheduleModel#loadDayAttributes(org.apache.myfaces.custom.schedule.model.Day)
156      */
157     protected void loadDayAttributes(Day day)
158     {
159         if (day == null)
160             return;
161         String key = holidayFormat.format(day.getDate());
162         String holiday = (String) holidays.get(key);
163         if (holiday != null)
164         {
165             day.setSpecialDayName(holiday);
166             day.setWorkingDay(false);
167         } else
168         {
169             day.setSpecialDayName(null);
170             day.setWorkingDay(true);
171         }
172     }
173 }
174 // The End