📄 timesheetcrudcontroller.java
字号:
Timesheet timesheet = (Timesheet) command; String dispatch = request.getParameter(DISPATCH_PARAMETER_NAME); TimesheetHelper timesheetHelper = (TimesheetHelper) request .getSession().getAttribute(TIMESHEET_HELPER_ATTR); Validate.notNull(timesheetHelper, " timesheetHelper was not available in session"); String timesheetUserType = timesheetHelper.getTimesheetUserType(); ITimesheetService timesheetService = (ITimesheetService) service; try { if (timesheet.getTimesheetId() == null) { if (USER.equals(timesheetUserType)) { if (SUBMIT_TIMESHEET.equals(dispatch)) timesheet.setSubmitTs(new Date()); timesheetService.insertTimesheetUser(timesheet); } else throw new IllegalStateException( "Trying to insert timesheet." + " timesheetUserType has to be 'user' but it is=" + timesheetUserType); } else { if (USER.equals(timesheetUserType)) { if (SUBMIT_TIMESHEET.equals(dispatch)) timesheet.setSubmitTs(new Date()); timesheetService.updateTimesheetUser(timesheet); } else if (SUPERVISOR.equals(timesheetUserType)) timesheetService.updateTimesheetSupervisor(timesheet); else throw new IllegalStateException("timesheetUserType=" + timesheetUserType + " is invalid during update"); } request.setAttribute("fromDt", timesheet.getFromDt()); saveMessage(request, "save.success"); // to prevent duplicate submissions create a new token saveToken(request); timesheetHelper.setTimesheetTotals(calculateHourTotals(timesheet)); // rebind to session to support clustering. request.getSession().setAttribute(TIMESHEET_HELPER_ATTR, timesheetHelper); return showForm(request, response, errors); } catch (OptimisticLockingException ole) { errors.reject(ole.getErrorCode()); request.setAttribute("fromDt", timesheet.getFromDt()); return showFormWhenException(request, response, command, errors); } catch (DuplicateTimesheetException dte) { errors.reject(dte.getErrorCode()); request.setAttribute("fromDt", timesheet.getFromDt()); return showFormWhenException(request, response, command, errors); } catch (UniqueConstraintException uce) { populatePropertyErrors(errors, uce); request.setAttribute("fromDt", timesheet.getFromDt()); return showFormWhenException(request, response, command, errors); } catch (InvalidPropertyException pe) { populatePropertyErrors(errors, pe); request.setAttribute("fromDt", timesheet.getFromDt()); return showFormWhenException(request, response, command, errors); } } public Timesheet getTimesheet(HttpServletRequest request, Long pk) { TimesheetQuery query = new TimesheetQuery(); query.setTimesheetId(pk); Timesheet timesheet = null; TimesheetHelper timesheetHelper = (TimesheetHelper) request .getSession().getAttribute(TIMESHEET_HELPER_ATTR); Validate.notNull(timesheetHelper, " timesheetHelper was not available in session"); String timesheetUserType = timesheetHelper.getTimesheetUserType(); ITimesheetService timesheetService = (ITimesheetService) service; if (USER.equals(timesheetUserType)) timesheet = timesheetService.getTimesheetUser(query); else if (SUPERVISOR.equals(timesheetUserType)) timesheet = timesheetService.getTimesheetSupervisor(query); else if (MANAGEMENT.equals(timesheetUserType)) timesheet = timesheetService.getTimesheetManagement(query); else throw new IllegalStateException("timesheetUserType=" + timesheetUserType + " is invalid"); if (timesheet != null) { // calculate line and cum totals calculateHourTotals(timesheet); if (USER.equals(timesheetUserType)) { // If approved, user should not be able to modify timesheet if (timesheet.getApprovedFlg() != null) saveViewHelper(request, ViewHelper.VIEWONLY_MODE); else saveViewHelper(request, ViewHelper.UPDATE_DELETE_MODE); } else if (SUPERVISOR.equals(timesheetUserType)) { // If the processed flag is set (generally by an external // system), the supervisor cannot modify the timesheet. if (timesheet.getProcessedFlg() != null) saveViewHelper(request, ViewHelper.VIEWONLY_MODE); else saveViewHelper(request, ViewHelper.UPDATE_DELETE_MODE); } else if (MANAGEMENT.equals(timesheetUserType)) { // Always viewonly for management saveViewHelper(request, ViewHelper.VIEWONLY_MODE); } request.setAttribute("fromDt", timesheet.getFromDt()); } saveToken(request); timesheetHelper.setTimesheetTotals(calculateHourTotals(timesheet)); // rebind to session to support clustering. request.getSession().setAttribute(TIMESHEET_HELPER_ATTR, timesheetHelper); return timesheet; } /** * Returns a map with totals for days, time entry lines and cumultative * total keys in map: day0, day1, ...... timeEntry0, timeEntry1, ..... * cumTotal * @param timesheet * @return map with totals for timesheet */ private Map calculateHourTotals(Timesheet timesheet) { BigDecimal cumTotal = new BigDecimal("0"); List timeEntries = timesheet.getTimeEntries(); // returns a map with key: dayX and value = the total hour for the day. Map map = calculateDayTotalHours(timeEntries); Iterator it = map.keySet().iterator(); while (it.hasNext()) { String key = (String) it.next(); BigDecimal dayTotalHours = (BigDecimal) map.get(key); cumTotal = cumTotal.add(dayTotalHours); } map.put("cumTotal", cumTotal); // calculate the time entry line totals. key='timeEntryX' value= total // hours for time entry line if (timeEntries != null) { for (int i = 0; i < timeEntries.size(); i++) { BigDecimal lineTotal = getTimeEntryTotal((TimeEntry) timeEntries .get(i)); if (lineTotal != null) map.put("timeEntry" + i, lineTotal); } } return map; } private Map calculateDayTotalHours(List timeEntries) { Map map = new HashMap(); if (timeEntries == null) return map; for (int i = 0; i < MAX_DAYS; i++) { BigDecimal totalHoursForDay = new BigDecimal("0.00"); Iterator it = timeEntries.iterator(); while (it.hasNext()) { TimeEntry te = (TimeEntry) it.next(); BigDecimal hours = (BigDecimal) timesheetUtils .getSimpleProperty(te, "day" + i); if (hours != null) totalHoursForDay = totalHoursForDay.add(hours); } map.put("day" + i, totalHoursForDay); } return map; } private BigDecimal getTimeEntryTotal(TimeEntry timeEntry) { BigDecimal total = new BigDecimal("0.00"); if (timeEntry == null) return total; for (int i = 0; i < MAX_DAYS; i++) { BigDecimal hours = (BigDecimal) timesheetUtils.getSimpleProperty( timeEntry, "day" + i); if (hours != null) total = total.add(hours); } return total; } protected ModelAndView cancelFlowSetValue(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { //needed by referenceData() request.setAttribute("fromDt", ((Timesheet) command).getFromDt()); return super.cancelFlowSetValue(request, response, command, errors); } protected ModelAndView assignSelectedValue(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { // needed by referenceData() request.setAttribute("fromDt", ((Timesheet) command).getFromDt()); return super.assignSelectedValue(request, response, command, errors); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -