📄 timesheetcrudcontroller.java
字号:
package net.java.workeffort.webapp.action;import java.math.BigDecimal;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.java.workeffort.infrastructure.view.ViewHelper;import net.java.workeffort.service.ITimesheetService;import net.java.workeffort.service.domain.TimeEntry;import net.java.workeffort.service.domain.Timesheet;import net.java.workeffort.service.domain.TimesheetQuery;import net.java.workeffort.service.support.DuplicateTimesheetException;import net.java.workeffort.service.support.InvalidPropertyException;import net.java.workeffort.service.support.OptimisticLockingException;import net.java.workeffort.service.support.TimesheetUtils;import net.java.workeffort.service.support.UniqueConstraintException;import net.java.workeffort.webapp.action.validator.TimesheetValidator;import net.java.workeffort.webapp.support.InvalidSynchronizerTokenException;import net.java.workeffort.webapp.support.TimesheetHelper;import net.java.workeffort.webapp.support.WebappUtils;import org.apache.commons.lang.StringUtils;import org.apache.commons.lang.Validate;import org.springframework.validation.BindException;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.support.RequestContextUtils;/** * Controller for the timesheet. * @author Antony Joseph */public class TimesheetCrudController extends OneToManyCrudController { // Max days a time sheet can have in system. 16 because it support semi // monthly. public static int MAX_DAYS = 16; public static String TIMESHEET_USER_TYPE = "timesheetUserType"; public static String TIMESHEET_HELPER_ATTR = "timesheetFormHelper"; public static String USER = "user"; public static String SUPERVISOR = "supervisor"; public static String MANAGEMENT = "management"; private static String SAVE_TIMESHEET = "saveTimesheet"; private static String SUBMIT_TIMESHEET = "submitTimesheet"; TimesheetUtils timesheetUtils; public void afterPropertiesSet() { super.afterPropertiesSet(); init(); } protected void init() { Validate.notNull(service, "Service cannot be null"); Validate.notNull(getFormView(), "'formView' cannot be null"); Validate.notNull(timesheetUtils, "'timesheetUtils' cannot be null"); primaryKeyParameter = "timesheetId"; primaryKeyClass = Long.class; collectionPropertyName = "timeEntries"; collectionElementClass = TimeEntry.class; setValidator(new TimesheetValidator()); setSessionForm(true); setValidateOnBinding(false); } public void setTimesheetUtils(TimesheetUtils timesheetUtils) { this.timesheetUtils = timesheetUtils; } protected Object formBackingObject(HttpServletRequest request) throws Exception { if (logger.isInfoEnabled()) logger.info("formBackingObject() invoked."); // store the timesheet helper in session scope. String timesheetUserType = request.getParameter(TIMESHEET_USER_TYPE); if (StringUtils.isNotEmpty(timesheetUserType)) { TimesheetHelper timesheetHelper = new TimesheetHelper(); timesheetHelper.setTimesheetUserType(timesheetUserType); request.getSession().setAttribute(TIMESHEET_HELPER_ATTR, timesheetHelper); } String dispatch = request.getParameter(DISPATCH_PARAMETER_NAME); if (QUERY.equals(dispatch) || RESET.equals(dispatch)) { Object pk = getParameterValue(request, primaryKeyParameter, primaryKeyClass); return getTimesheet(request, (Long) pk); } else if (NEW.equals(dispatch)) { saveToken(request); // let the jsp know to display form in insert mode. saveViewHelper(request, ViewHelper.INSERT_MODE); return createCommand(); } else throw new RuntimeException( "formBackingObject(). dispatch is not 'query' or 'insert'. dispatch=" + dispatch); } protected Map referenceData(HttpServletRequest request) throws Exception { if (logger.isInfoEnabled()) logger.info("referenceData invoked."); Map map = WebappUtils.referenceLovData(getLovMethodCache(), getLovService()); Locale locale = RequestContextUtils.getLocale(request); String dateFormat = getWebApplicationContext().getMessage("dateFormat", null, locale); // 'fromDt' must be available as a request parameter or as a request // attribute. 'fromDt' request *ATTRIBUTE* is set in // getTimesheet()/cancelFlowSetValue()/assignSelectedValue() Date fromDt = (Date) request.getAttribute("fromDt"); if (fromDt == null) fromDt = timesheetUtils.convertStringToDate(request .getParameter("fromDt"), dateFormat); Validate .notNull(fromDt, "No parameter or attribute value for 'fromDt' found in http request"); List datesForTimesheet = timesheetUtils.getDatesForTimesheet(fromDt, dateFormat, locale); map.put("datesForTimesheet", datesForTimesheet); map.put("noOfDays", new Integer(datesForTimesheet.size())); return map; } protected boolean isFormSubmission(HttpServletRequest request) { String dispatch = request.getParameter(DISPATCH_PARAMETER_NAME); // reset is a POST. Handle it like a GET. if (RESET.equals(dispatch)) { // reset is handled like an initial query return false; } else if (ASSIGN_SELECTED_VALUE.equals(dispatch) || CANCEL_FLOW_SET_VALUE.equals(dispatch)) //assignSelectedValue and cancelFlowSetValue are GETs. //Handle them like a form submission. return true; else return super.isFormSubmission(request); } protected ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { String dispatch = request.getParameter(DISPATCH_PARAMETER_NAME); if (!isTokenValid(request)) { clearToken(request); throw new InvalidSynchronizerTokenException( "Invalid token. dispatch=" + dispatch); } if (ADD_ROW.equals(dispatch) || IGNORE_ROW.equals(dispatch) || EDIT_ROW.equals(dispatch) || START_FLOW_SET_VALUE.equals(dispatch) || CANCEL_FLOW_SET_VALUE.equals(dispatch) || ASSIGN_SELECTED_VALUE.equals(dispatch)) { //call onSubmit() even if there are bind errors if (logger.isInfoEnabled()) logger.info(dispatch + " -> processing submit"); return onSubmit(request, response, command, errors); } else { return super.processFormSubmission(request, response, command, errors); } } protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { if (logger.isInfoEnabled()) logger.info("onSubmit() invoked."); String dispatch = request.getParameter(DISPATCH_PARAMETER_NAME); if (SAVE_TIMESHEET.equals(dispatch)) { return saveToDatabase(request, response, command, errors); } else if (SUBMIT_TIMESHEET.equals(dispatch)) { return saveToDatabase(request, response, command, errors); } else if (EDIT_ROW.equals(dispatch)) { return editRow(request, response, command, errors); } else if (ADD_ROW.equals(dispatch)) { return addRow(request, response, command, errors); } else if (IGNORE_ROW.equals(dispatch)) { return ignoreRow(request, response, command, errors); } else if (START_FLOW_SET_VALUE.equals(dispatch)) { return startFlowSetValue(request, response, command, errors); } else if (ASSIGN_SELECTED_VALUE.equals(dispatch)) { return assignSelectedValue(request, response, command, errors); } else if (CANCEL_FLOW_SET_VALUE.equals(dispatch)) { return cancelFlowSetValue(request, response, command, errors); } else throw new RuntimeException("onSubmit(). invalid dispatch:" + dispatch); } private ModelAndView saveToDatabase(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -