[
http://issues.apache.org/jira/browse/TOMAHAWK-614?page=comments#action_12430162
]
Mikhail Grushinskiy commented on TOMAHAWK-614:
----------------------------------------------
Here is improved version of the code
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import org.apache.myfaces.custom.schedule.model.SimpleScheduleModel;
import org.apache.myfaces.custom.schedule.model.ScheduleDay;
import org.apache.myfaces.custom.schedule.model.ScheduleEntry;
public class ScheduleModel extends SimpleScheduleModel implements
Serializable {
/**
* serialVersionUID
*/
private static final long serialVersionUID =
-2291773335106774130L;
public static final String VER_ID = "$Id$";
private String timeZone;
private Date startDate;
private Date endDate;
public String getTimeZone() {
return timeZone;
}
public void setTimeZone(String timeZone) {
this.timeZone = timeZone;
}
/**
* @return Calendar with settings
*/
public Calendar getCalendar() {
Calendar calendar = GregorianCalendar.getInstance();
String userTimeZone = getTimeZone();
if (userTimeZone != null) {
TimeZone tz =
TimeZone.getTimeZone(userTimeZone);
calendar.setTimeZone(tz);
}
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setMinimalDaysInFirstWeek(1);
return calendar;
}
/*
* (non-Javadoc)
*
* @see
org.apache.myfaces.custom.schedule.model.AbstractScheduleModel#setWeek(j
ava.util.Date)
*/
@Override
protected void setWeek(Date date) {
if (date == null) {
return;
}
clear();
Calendar cal = getCalendar();
cal.setTime(date);
// go back to the monday of this week
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
ScheduleDay firstDay = null;
ScheduleDay lastDay = null;
for (int i = 0; i < 7; i++) {
ScheduleDay addedDay = add(cal.getTime());
if (firstDay == null) {
firstDay = addedDay;
}
lastDay = addedDay;
cal.add(Calendar.DATE, 1);
}
startDate = firstDay.getDayStart();
endDate = lastDay.getDayEnd();
load(firstDay.getDayStart(), lastDay.getDayEnd());
}
/*
* (non-Javadoc)
*
* @see
org.apache.myfaces.custom.schedule.model.AbstractScheduleModel#setWorkWe
ek(java.util.Date)
*/
@Override
protected void setWorkWeek(Date date) {
if (date == null) {
return;
}
clear();
Calendar cal = getCalendar();
cal.setTime(date);
// go back to the monday of this week
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
ScheduleDay firstDay = null;
ScheduleDay lastDay = null;
for (int i = 0; i < 5; i++) {
ScheduleDay addedDay = add(cal.getTime());
if (firstDay == null) {
firstDay = addedDay;
}
lastDay = addedDay;
cal.add(Calendar.DATE, 1);
}
startDate = firstDay.getDayStart();
endDate = lastDay.getDayEnd();
load(firstDay.getDayStart(), lastDay.getDayEnd());
}
/*
* (non-Javadoc)
*
* @see
org.apache.myfaces.custom.schedule.model.AbstractScheduleModel#setMonth(
java.util.Date)
*/
@Override
protected void setMonth(Date date) {
if (date == null) {
return;
}
clear();
Calendar cal = getCalendar();
cal.setTime(date);
// go back to the first day of the month;
cal.set(Calendar.DAY_OF_MONTH,
cal.getMinimum(Calendar.DAY_OF_MONTH));
Date temp = cal.getTime();
cal.add(Calendar.MONTH, 1);
int nextMonth = cal.get(Calendar.MONTH);
cal.setTime(temp);
ScheduleDay firstDay = null;
ScheduleDay lastDay = null;
// we want to show the whole first week, including the
days from the
// previous month
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
while (true) {
ScheduleDay addedDay = add(cal.getTime());
if (firstDay == null) {
firstDay = addedDay;
}
lastDay = addedDay;
cal.add(Calendar.DATE, 1);
// stop when we are at the last day of the last
week
if (cal.get(Calendar.MONTH) == nextMonth
&& cal.get(Calendar.DAY_OF_WEEK)
== Calendar.MONDAY) {
break;
}
}
startDate = firstDay.getDayStart();
endDate = lastDay.getDayEnd();
load(firstDay.getDayStart(), lastDay.getDayEnd());
}
/* (non-Javadoc)
* @see
org.apache.myfaces.custom.schedule.model.AbstractScheduleModel#setDay(ja
va.util.Date)
*/
@Override
protected void setDay(Date date) {
if (date == null) {
return;
}
clear();
ScheduleDay day = add(date);
startDate = day.getDayStart();
endDate = day.getDayEnd();
load(day.getDayStart(), day.getDayEnd());
}
/**
* @param startDate
* @param endDate
*/
private void load(Date startDate, Date endDate) {
Collection entries = loadEntries(startDate, endDate);
for (Iterator entryIterator = entries.iterator();
entryIterator
.hasNext();) {
ScheduleEntry entry = (ScheduleEntry)
entryIterator.next();
for (Iterator dayIterator = days.iterator();
dayIterator.hasNext();) {
ScheduleDay day = (ScheduleDay)
dayIterator.next();
day.addEntry(entry);
}
}
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
}
> Schedule for January is missing January 1, 2006
> -----------------------------------------------
>
> Key: TOMAHAWK-614
> URL: http://issues.apache.org/jira/browse/TOMAHAWK-614
> Project: MyFaces Tomahawk
> Issue Type: Bug
> Components: Schedule
> Environment: all
> Reporter: Mikhail Grushinskiy
> Priority: Minor
> Fix For: 1.1.4-SNAPSHOT, 1.1.5-SNAPSHOT
>
> Attachments: AbstractScheduleModel.java.patch,
> AbstractScheduleModel.java.patch
>
>
> Schedule for January is missing January 1, 2006. Somehow January 1, 2006 is
> not appearing on January 2006 schedule (basically whole week ending with it
> is missing)
>
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira