001: package com.technoetic.xplanner.forms;
002:
003: import com.technoetic.xplanner.domain.virtual.Timesheet;
004: import org.apache.struts.Globals;
005: import org.apache.struts.action.ActionError;
006: import org.apache.struts.action.ActionErrors;
007: import org.apache.struts.action.ActionMapping;
008:
009: import javax.servlet.http.HttpServletRequest;
010: import java.text.ParseException;
011: import java.text.SimpleDateFormat;
012: import java.util.ArrayList;
013: import java.util.Calendar;
014: import java.util.Collection;
015: import java.util.Date;
016:
017: public class AggregateTimesheetForm extends AbstractEditorForm {
018:
019: private java.util.Date startDate;
020: private String startDateString;
021: private java.util.Date endDate;
022: private String endDateString;
023: private SimpleDateFormat dateFormat;
024: private Timesheet timesheet;
025: private Collection allPeople;
026: private String[] selectedPeople;
027:
028: public ActionErrors validate(ActionMapping mapping,
029: HttpServletRequest request) {
030: ActionErrors errors = new ActionErrors();
031:
032: if (this .dateFormat == null) {
033: String format = getResources(request).getMessage(
034: "format.date");
035: dateFormat = new SimpleDateFormat(format);
036: }
037:
038: Date startDate = null;
039: if (isPresent(this .startDateString)) {
040: try {
041: startDate = dateFormat.parse(this .startDateString);
042: this .setStartDate(startDate);
043: } catch (ParseException ex) {
044: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
045: "timesheet.error.unparsable_date"));
046: request.setAttribute(Globals.ERROR_KEY, errors);
047: return errors;
048: }
049: }
050:
051: Date endDate = null;
052: if (isPresent(this .endDateString)) {
053: try {
054: endDate = dateFormat.parse(this .endDateString);
055: this .setEndDate(endDate);
056: } catch (ParseException ex) {
057: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
058: "timesheet.error.unparsable_date"));
059: request.setAttribute(Globals.ERROR_KEY, errors);
060: return errors;
061: }
062: }
063:
064: return errors;
065: }
066:
067: public void reset(ActionMapping mapping, HttpServletRequest request) {
068: super .reset(mapping, request);
069: this .endDate = this .getWeekEndDate();
070: this .startDate = this .getWeekStartDate();
071: this .timesheet = new Timesheet(this .startDate, this .endDate);
072: this .allPeople = new ArrayList();
073: this .selectedPeople = new String[] {};
074: }
075:
076: public java.util.Date getStartDate() {
077: return startDate;
078: }
079:
080: public void setStartDate(java.util.Date startDate) {
081: this .startDate = startDate;
082: }
083:
084: public String getStartDateString() {
085: if (dateFormat == null) {
086: return this .getStartDate().toString();
087: }
088: return dateFormat.format(this .getStartDate());
089: }
090:
091: public void setStartDateString(String start) {
092: this .startDateString = start;
093: }
094:
095: public Date getEndDate() {
096: return endDate;
097: }
098:
099: public void setEndDate(Date endDate) {
100: this .endDate = endDate;
101: }
102:
103: public String getEndDateString() {
104: if (dateFormat == null) {
105: return this .getEndDate().toString();
106: }
107: return dateFormat.format(this .getEndDate());
108: }
109:
110: public void setEndDateString(String end) {
111: this .endDateString = end;
112: }
113:
114: public Timesheet getTimesheet() {
115: return timesheet;
116: }
117:
118: public void setTimesheet(Timesheet timesheet) {
119: this .timesheet = timesheet;
120: }
121:
122: private Date getWeekEndDate() {
123: Calendar cal = Calendar.getInstance();
124: cal.set(Calendar.HOUR, 0);
125: cal.set(Calendar.MINUTE, 0);
126: cal.set(Calendar.SECOND, 0);
127: cal.set(Calendar.MILLISECOND, 0);
128: int weekday = cal.get(Calendar.DAY_OF_WEEK);
129: cal.add(Calendar.DATE, 7 - weekday);
130: return cal.getTime();
131: }
132:
133: private Date getWeekStartDate() {
134: Calendar cal = Calendar.getInstance();
135: cal.set(Calendar.HOUR, 0);
136: cal.set(Calendar.MINUTE, 0);
137: cal.set(Calendar.SECOND, 0);
138: cal.set(Calendar.MILLISECOND, 0);
139: int weekday = cal.get(Calendar.DAY_OF_WEEK);
140: cal.add(Calendar.DATE, -weekday + 1);
141: return cal.getTime();
142: }
143:
144: public void setDateFormat(SimpleDateFormat format) {
145: this .dateFormat = format;
146: }
147:
148: public SimpleDateFormat getDateFormat() {
149: return this .dateFormat;
150: }
151:
152: public Collection getAllPeople() {
153: return allPeople;
154: }
155:
156: public void setAllPeople(Collection allPeople) {
157: this .allPeople = allPeople;
158: }
159:
160: public String[] getSelectedPeople() {
161: return selectedPeople;
162: }
163:
164: public void setSelectedPeople(String[] selectedPeople) {
165: this.selectedPeople = selectedPeople;
166: }
167: }
|