001    /*
002     * Copyright 2005-2007 The Kuali Foundation
003     *
004     *
005     * Licensed under the Educational Community License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     * http://www.opensource.org/licenses/ecl2.php
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package edu.sampleu.kew.krad.form;
018    
019    import java.text.ParseException;
020    import java.text.SimpleDateFormat;
021    import java.util.Date;
022    import java.util.HashMap;
023    import java.util.Map;
024    
025    import org.kuali.rice.kew.stats.Stats;
026    import org.kuali.rice.kew.stats.web.StatsAction;
027    import org.kuali.rice.kew.util.KEWConstants;
028    import org.kuali.rice.krad.util.GlobalVariables;
029    import org.kuali.rice.krad.web.form.UifFormBase;
030    
031    /**
032     * A Struts ActionForm for the {@link StatsAction}.
033     * 
034     * @see StatsAction
035     * 
036     * @author Kuali Rice Team (rice.collab@kuali.org)
037     */
038    public class StatsForm extends UifFormBase {
039    
040        private static final long serialVersionUID = 4587377779133823858L;
041        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(StatsForm.class);
042        private static final String BEGIN_DATE = "begDate";
043        private static final String END_DATE = "endDate";
044    
045        public static final String DAY_TIME_UNIT = "DDD";
046        public static final String WEEK_TIME_UNIT = "WW";
047        public static final String MONTH_TIME_UNIT = "MM";
048        public static final String YEAR_TIME_UNIT = "YYYY";
049    
050        public static final String DEFAULT_BEGIN_DATE = "01/01/1900";
051        public static final String DEFAULT_END_DATE = "01/01/2400";
052        public static final String BEG_DAY_TIME = " 00:00";
053        public static final String END_DAY_TIME = " 23:59";
054        public static final String DATE_FORMAT = "MM/dd/yyyy";
055        public static final String TIME_FORMAT = " HH:mm";
056    
057        private Stats stats;
058        private String methodToCall = "";
059        private String avgActionsPerTimeUnit = DAY_TIME_UNIT;
060    
061        private String begDate;
062        private String endDate;
063    
064        private Date beginningDate;
065        private Date endingDate;
066    
067        // KULRICE-3137: Added a backLocation parameter similar to the one from lookups.
068        private String backLocation;
069    
070        public StatsForm() {
071            stats = new Stats();
072        }
073    
074        /**
075         * Retrieves the "returnLocation" parameter after calling "populate" on the superclass.
076         * 
077         * @see org.kuali.rice.krad.web.struts.form.KualiForm#populate(javax.servlet.http.HttpServletRequest)
078         */
079        //  @Override
080        //  public void populate(HttpServletRequest request) {
081        //          super.populate(request);
082        //          
083        //        if (getParameter(request, KRADConstants.RETURN_LOCATION_PARAMETER) != null) {
084        //            setBackLocation(getParameter(request, KRADConstants.RETURN_LOCATION_PARAMETER));
085        //        }
086        //  }
087    
088        public void determineBeginDate() {
089            SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT + TIME_FORMAT);
090    
091            beginningDate = null;
092            try {
093                if (getBegDate() == null || getBegDate().trim().equals("")) {
094                    beginningDate = dateFormat.parse(DEFAULT_BEGIN_DATE + BEG_DAY_TIME);
095                } else {
096                    beginningDate = dateFormat.parse(getBegDate() + BEG_DAY_TIME);
097                }
098    
099                dateFormat = new SimpleDateFormat(DATE_FORMAT);
100                begDate = dateFormat.format(beginningDate);
101            } catch (ParseException e) {
102                //parse error caught in validate methods
103            } finally {
104                if (beginningDate == null) {
105                    try {
106                        beginningDate = dateFormat.parse(DEFAULT_BEGIN_DATE + BEG_DAY_TIME);
107                    } catch (ParseException e) {
108                        throw new RuntimeException("Default Begin Date format incorrect");
109                    }
110                }
111            }
112        }
113    
114        public void determineEndDate() {
115            SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT + TIME_FORMAT);
116    
117            endingDate = null;
118            try {
119                if (getEndDate() == null || getEndDate().trim().equals("")) {
120                    endingDate = dateFormat.parse(DEFAULT_END_DATE + END_DAY_TIME);
121                } else {
122                    endingDate = dateFormat.parse(getEndDate() + END_DAY_TIME);
123                }
124    
125                dateFormat = new SimpleDateFormat(DATE_FORMAT);
126                endDate = dateFormat.format(endingDate);
127            } catch (ParseException e) {
128                //parse error caught in validate methods
129            } finally {
130                if (endingDate == null) {
131                    try {
132                        endingDate = dateFormat.parse(DEFAULT_END_DATE + END_DAY_TIME);
133                    } catch (ParseException e) {
134                        throw new RuntimeException("Default End Date format incorrect");
135                    }
136                }
137            }
138        }
139    
140        public Map makePerUnitOfTimeDropDownMap() {
141    
142            Map dropDownMap = new HashMap();
143            dropDownMap.put(DAY_TIME_UNIT, KEWConstants.DAILY_UNIT);
144            dropDownMap.put(WEEK_TIME_UNIT, KEWConstants.WEEKLY_UNIT);
145            dropDownMap.put(MONTH_TIME_UNIT, KEWConstants.MONTHLY_UNIT);
146            dropDownMap.put(YEAR_TIME_UNIT, KEWConstants.YEARLY_UNIT);
147            return dropDownMap;
148    
149        }
150    
151        public void validateDates() {
152            LOG.debug("validate()");
153    
154            //this.validateDate(BEGIN_DATE, this.getBegDate(), "general.error.fieldinvalid");
155            //this.validateDate(END_DATE, this.getEndDate(), "general.error.fieldinvalid");
156            if (getBegDate() != null && getBegDate().length() != 0) {
157                try {
158                    new SimpleDateFormat(DATE_FORMAT + TIME_FORMAT).parse(getBegDate().trim() + END_DAY_TIME);
159                } catch (ParseException e) {
160                    GlobalVariables.getMessageMap().putError(BEGIN_DATE, "general.error.fieldinvalid", "Begin Date");
161                }
162            }
163            if (getEndDate() != null && getEndDate().length() != 0) {
164                try {
165                    new SimpleDateFormat(DATE_FORMAT + TIME_FORMAT).parse(getEndDate().trim() + END_DAY_TIME);
166                } catch (ParseException e) {
167                    GlobalVariables.getMessageMap().putError(END_DATE, "general.error.fieldinvalid", "End Date");
168                }
169            }
170        }
171    
172        public Stats getStats() {
173            return stats;
174        }
175    
176        public void setStats(Stats stats) {
177            this.stats = stats;
178        }
179    
180    //    public String getApprovedLabel() {
181    //        return KEWConstants.ROUTE_HEADER_APPROVED_LABEL;
182    //    }
183    
184        public String getCanceledLabel() {
185            return KEWConstants.ROUTE_HEADER_CANCEL_LABEL;
186        }
187    
188        public String getDisapprovedLabel() {
189            return KEWConstants.ROUTE_HEADER_DISAPPROVED_LABEL;
190        }
191    
192        public String getEnrouteLabel() {
193            return KEWConstants.ROUTE_HEADER_ENROUTE_LABEL;
194        }
195    
196        public String getExceptionLabel() {
197            return KEWConstants.ROUTE_HEADER_EXCEPTION_LABEL;
198        }
199    
200        public String getFinalLabel() {
201            return KEWConstants.ROUTE_HEADER_FINAL_LABEL;
202        }
203    
204        public String getInitiatedLabel() {
205            return KEWConstants.ROUTE_HEADER_INITIATED_LABEL;
206        }
207    
208        public String getProcessedLabel() {
209            return KEWConstants.ROUTE_HEADER_PROCESSED_LABEL;
210        }
211    
212        public String getSavedLabel() {
213            return KEWConstants.ROUTE_HEADER_SAVED_LABEL;
214        }
215    
216        public String getAvgActionsPerTimeUnit() {
217            return avgActionsPerTimeUnit;
218        }
219    
220        public void setAvgActionsPerTimeUnit(String string) {
221            avgActionsPerTimeUnit = string;
222        }
223    
224        public String getBegDate() {
225            return begDate;
226        }
227    
228        public void setBegDate(String begDate) {
229            this.begDate = begDate;
230        }
231    
232        public String getEndDate() {
233            return endDate;
234        }
235    
236        public void setEndDate(String endDate) {
237            this.endDate = endDate;
238        }
239    
240        public String getMethodToCall() {
241            return methodToCall;
242        }
243    
244        public void setMethodToCall(String methodToCall) {
245            this.methodToCall = methodToCall;
246        }
247    
248        public Date getBeginningDate() {
249            return beginningDate;
250        }
251    
252        public void setBeginningDate(Date beginningDate) {
253            this.beginningDate = beginningDate;
254        }
255    
256        public Date getEndingDate() {
257            return endingDate;
258        }
259    
260        public void setEndingDate(Date endingDate) {
261            this.endingDate = endingDate;
262        }
263    
264        public String getDayTimeUnit() {
265            return DAY_TIME_UNIT;
266        }
267    
268        public String getMonthTimeUnit() {
269            return MONTH_TIME_UNIT;
270        }
271    
272        public String getWeekTimeUnit() {
273            return WEEK_TIME_UNIT;
274        }
275    
276        public String getYearTimeUnit() {
277            return YEAR_TIME_UNIT;
278        }
279    
280        public String getBackLocation() {
281            return this.backLocation;
282        }
283    
284        public void setBackLocation(String backLocation) {
285            this.backLocation = backLocation;
286        }
287    
288    }