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 java.text.ParseException;
010: import java.text.SimpleDateFormat;
011: import java.util.Calendar;
012: import java.util.Date;
013: import javax.servlet.http.HttpServletRequest;
014:
015: public class PersonTimesheetForm extends AbstractEditorForm {
016:
017: private Date startDate;
018: private String startDateString;
019: private Date endDate;
020: private String endDateString;
021: private SimpleDateFormat dateFormat;
022: private Timesheet timesheet;
023:
024: //DEBT(FORM) Refactor to use base class utility. See TimeEditorForm for example
025: public ActionErrors validate(ActionMapping mapping,
026: HttpServletRequest request) {
027: ActionErrors errors = new ActionErrors();
028:
029: if (this .dateFormat == null) {
030: String format = getResources(request).getMessage(
031: request.getLocale(), "format.date");
032: dateFormat = new SimpleDateFormat(format);
033: }
034:
035: Date startDate = null;
036: if (isPresent(this .startDateString)) {
037: try {
038: startDate = dateFormat.parse(this .startDateString);
039: this .setStartDate(startDate);
040: } catch (ParseException ex) {
041: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
042: "timesheet.error.unparsable_date"));
043: request.setAttribute(Globals.ERROR_KEY, errors);
044: return errors;
045: }
046: }
047:
048: Date endDate = null;
049: if (isPresent(this .endDateString)) {
050: try {
051: endDate = dateFormat.parse(this .endDateString);
052: this .setEndDate(endDate);
053: } catch (ParseException ex) {
054: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
055: "timesheet.error.unparsable_date"));
056: request.setAttribute(Globals.ERROR_KEY, errors);
057: return errors;
058: }
059: }
060: return errors;
061: }
062:
063: public void reset(ActionMapping mapping, HttpServletRequest request) {
064: super .reset(mapping, request);
065: this .endDate = this .getWeekEndDate();
066: this .startDate = this .getWeekStartDate();
067: this .timesheet = new Timesheet(this .startDate, this .endDate);
068: }
069:
070: public java.util.Date getStartDate() {
071: return startDate;
072: }
073:
074: public void setStartDate(java.util.Date startDate) {
075: this .startDate = startDate;
076: }
077:
078: public String getStartDateString() {
079: if (dateFormat == null) {
080: return this .getStartDate().toString();
081: }
082: return dateFormat.format(this .getStartDate());
083: }
084:
085: public void setStartDateString(String start) {
086: this .startDateString = start;
087: }
088:
089: public Date getEndDate() {
090: return endDate;
091: }
092:
093: public void setEndDate(Date endDate) {
094: this .endDate = endDate;
095: }
096:
097: public String getEndDateString() {
098: if (dateFormat == null) {
099: return this .getEndDate().toString();
100: }
101: return dateFormat.format(this .getEndDate());
102: }
103:
104: public void setEndDateString(String end) {
105: this .endDateString = end;
106: }
107:
108: public Timesheet getTimesheet() {
109: return timesheet;
110: }
111:
112: public void setTimesheet(Timesheet timesheet) {
113: this .timesheet = timesheet;
114: }
115:
116: private Date getWeekEndDate() {
117: Calendar cal = Calendar.getInstance();
118: cal.set(Calendar.HOUR_OF_DAY, 0);
119: cal.set(Calendar.MINUTE, 0);
120: cal.set(Calendar.SECOND, 0);
121: cal.set(Calendar.MILLISECOND, 0);
122: int weekday = cal.get(Calendar.DAY_OF_WEEK);
123: cal.add(Calendar.DATE, 7 - weekday);
124: return cal.getTime();
125: }
126:
127: private Date getWeekStartDate() {
128: Calendar cal = Calendar.getInstance();
129: cal.set(Calendar.HOUR_OF_DAY, 0);
130: cal.set(Calendar.MINUTE, 0);
131: cal.set(Calendar.SECOND, 0);
132: cal.set(Calendar.MILLISECOND, 0);
133: int weekday = cal.get(Calendar.DAY_OF_WEEK);
134: cal.add(Calendar.DATE, -weekday + 1);
135: return cal.getTime();
136: }
137:
138: public void setDateFormat(SimpleDateFormat format) {
139: this .dateFormat = format;
140: }
141:
142: public SimpleDateFormat getDateFormat() {
143: return this .dateFormat;
144: }
145:
146: public int getPersonId() {
147: return Integer.parseInt(super.getOid());
148: }
149: }
|