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.tobago.model;
21  
22  import java.text.DecimalFormat;
23  import java.text.SimpleDateFormat;
24  import java.util.Calendar;
25  
26  public class CalendarModel {
27  
28    private DateModel[][] calendarArray;
29    private int firstDayOffset;
30  
31    public CalendarModel(final Calendar calendar) {
32  //    int weekCount = CalendarUtils.weekCount(calendar);
33      final int weekCount = 6; // switching off dynamic weekCount!
34      calendarArray = new DateModel[weekCount][7];
35      final Calendar c = (Calendar) calendar.clone();
36      c.clear(Calendar.DAY_OF_MONTH);
37      c.set(Calendar.DAY_OF_MONTH, 1);
38      // assert c.isLenient() : "'add -x days' may not work in a non-lenient calendar";
39      firstDayOffset = firstDayOffset(c);
40      c.add(Calendar.DAY_OF_WEEK, -firstDayOffset);
41      for (int week = 0; week < weekCount; ++week) {
42        for (int day = 0; day < 7; ++day) {
43          calendarArray[week][day] = new DateModel(c);
44          c.add(Calendar.DAY_OF_MONTH, 1);
45        }
46      }
47    }
48  
49    public int getWeekCount() {
50      return calendarArray.length;
51    }
52  
53    public int getMonth() {
54      return calendarArray[0][firstDayOffset].getMonth();
55    }
56  
57    public int getYear() {
58      return calendarArray[0][firstDayOffset].getYear();
59    }
60  
61    public DateModel getDate(final int week, final int day) {
62      return calendarArray[week][day];
63    }
64  
65    private int firstDayOffset(final Calendar calendar) {
66      final Calendar c = (Calendar) calendar.clone();
67      c.clear(Calendar.DAY_OF_MONTH);
68      c.set(Calendar.DAY_OF_MONTH, 1);
69      final int day = c.get(Calendar.DAY_OF_WEEK);
70      final int firstDayOfWeek = c.getFirstDayOfWeek();
71      // Fails: assertEquals((1+7-3)%7, (1-3)%7);
72      return (day + 7 - firstDayOfWeek) % 7;
73    }
74  
75    public String toString() {
76      final StringBuilder buffer = new StringBuilder();
77      buffer.append("Month: ").append(getMonth()).append("\n");
78      final int weekCount = getWeekCount();
79      final DecimalFormat format = new DecimalFormat("00");
80      final SimpleDateFormat dateFormat = new SimpleDateFormat("E");
81      for (int day = 0; day < 7; ++day) {
82        final DateModel date = getDate(0, day);
83        final String dayName = dateFormat.format(date.getCalendar().getTime());
84        buffer.append(dayName.substring(0, 2)).append(" ");
85      }
86      buffer.append("\n");
87      for (int week = 0; week < weekCount; ++week) {
88        for (int day = 0; day < 7; ++day) {
89          final DateModel date = getDate(week, day);
90          buffer.append(format.format(date.getDay())).append(" ");
91        }
92        buffer.append("\n");
93      }
94      return buffer.toString();
95    }
96  
97  }