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