001 /** 002 * Copyright 2004-2013 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 */ 016 package org.kuali.hr.time.approval.web; 017 018 import java.text.SimpleDateFormat; 019 import java.util.ArrayList; 020 import java.util.Date; 021 import java.util.HashMap; 022 import java.util.LinkedList; 023 import java.util.List; 024 import java.util.Map; 025 import java.util.Set; 026 027 import javax.servlet.http.HttpServletRequest; 028 import javax.servlet.http.HttpServletResponse; 029 030 import com.google.common.collect.Lists; 031 import org.apache.commons.collections.MapUtils; 032 import org.apache.commons.lang.StringUtils; 033 import org.apache.commons.lang.time.DateUtils; 034 import org.apache.log4j.Logger; 035 import org.apache.struts.action.ActionForm; 036 import org.apache.struts.action.ActionForward; 037 import org.apache.struts.action.ActionMapping; 038 import org.hsqldb.lib.StringUtil; 039 import org.json.simple.JSONValue; 040 import org.kuali.hr.lm.leaveblock.LeaveBlock; 041 import org.kuali.hr.time.assignment.Assignment; 042 import org.kuali.hr.time.base.web.ApprovalForm; 043 import org.kuali.hr.time.base.web.TkAction; 044 import org.kuali.hr.time.detail.web.ActionFormUtils; 045 import org.kuali.hr.time.service.base.TkServiceLocator; 046 import org.kuali.hr.time.timeblock.TimeBlock; 047 import org.kuali.hr.time.timesheet.TimesheetDocument; 048 import org.kuali.hr.time.timesummary.AssignmentColumn; 049 import org.kuali.hr.time.timesummary.AssignmentRow; 050 import org.kuali.hr.time.timesummary.EarnCodeSection; 051 import org.kuali.hr.time.timesummary.EarnGroupSection; 052 import org.kuali.hr.time.timesummary.TimeSummary; 053 import org.kuali.hr.time.util.TKContext; 054 import org.kuali.hr.time.util.TKUtils; 055 import org.kuali.hr.time.workflow.TimesheetDocumentHeader; 056 057 public class TimeApprovalWSAction extends TkAction { 058 059 private static final Logger LOG = Logger.getLogger(TimeApprovalWSAction.class); 060 061 /** 062 * Action called via AJAX. (ajaj really...) 063 * <p/> 064 * This search returns quick-results to the search box for the user to further 065 * refine upon. The end value can then be form submitted. 066 */ 067 public ActionForward searchApprovalRows(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 068 TimeApprovalActionForm taaf = (TimeApprovalActionForm) form; 069 List<Map<String, String>> results = new LinkedList<Map<String, String>>(); 070 if(StringUtils.isNotEmpty(taaf.getPayBeginDateForSearch()) 071 && StringUtils.isNotEmpty(taaf.getPayEndDateForSearch()) ) { 072 Date beginDate = new SimpleDateFormat("MM/dd/yyyy").parse(taaf.getPayBeginDateForSearch()); 073 Date endDate = new SimpleDateFormat("MM/dd/yyyy").parse(taaf.getPayEndDateForSearch()); 074 //the endDate we get here is coming from approval.js and is extracted from html. we need to add a day to cover the last day in the pay period. 075 endDate = DateUtils.addDays(endDate,1); 076 List<String> workAreaList = new ArrayList<String>(); 077 if(StringUtil.isEmpty(taaf.getSelectedWorkArea())) { 078 Set<Long> workAreas = TkServiceLocator.getTkRoleService().getWorkAreasForApprover(TKContext.getTargetPrincipalId(), TKUtils.getCurrentDate()); 079 for(Long workArea : workAreas) { //taaf.getWorkAreaDescr().keySet() 080 workAreaList.add(workArea.toString()); 081 } 082 } else { 083 workAreaList.add(taaf.getSelectedWorkArea()); 084 } 085 List<String> principalIds = TkServiceLocator.getTimeApproveService() 086 .getTimePrincipalIdsWithSearchCriteria(workAreaList, taaf.getSelectedPayCalendarGroup(), 087 new java.sql.Date(endDate.getTime()), new java.sql.Date(beginDate.getTime()), new java.sql.Date(endDate.getTime())); 088 089 090 if (StringUtils.equals(taaf.getSearchField(), ApprovalForm.ORDER_BY_PRINCIPAL)) { 091 for (String id : principalIds) { 092 if(StringUtils.contains(id, taaf.getSearchTerm())) { 093 Map<String, String> labelValue = new HashMap<String, String>(); 094 labelValue.put("id", id); 095 labelValue.put("result", id); 096 results.add(labelValue); 097 } 098 } 099 } else if (StringUtils.equals(taaf.getSearchField(), ApprovalForm.ORDER_BY_DOCID)) { 100 Map<String, TimesheetDocumentHeader> principalDocumentHeaders = 101 TkServiceLocator.getTimeApproveService().getPrincipalDocumentHeader(principalIds, beginDate, endDate); 102 103 for (Map.Entry<String,TimesheetDocumentHeader> entry : principalDocumentHeaders.entrySet()) { 104 if (StringUtils.contains(entry.getValue().getDocumentId(), taaf.getSearchTerm())) { 105 Map<String, String> labelValue = new HashMap<String, String>(); 106 // labelValue.put("id", entry.getValue().getDocumentId() + " (" + entry.getValue().getPrincipalId() + ")"); 107 labelValue.put("id", entry.getValue().getDocumentId()); 108 labelValue.put("result", entry.getValue().getPrincipalId()); 109 results.add(labelValue); 110 } 111 } 112 } 113 } 114 115 taaf.setOutputString(JSONValue.toJSONString(results)); 116 return mapping.findForward("ws"); 117 } 118 119 public ActionForward getTimeSummary(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 120 TimeApprovalActionForm taaf = (TimeApprovalActionForm) form; 121 TimesheetDocument td = TkServiceLocator.getTimesheetService().getTimesheetDocument(taaf.getDocumentId()); 122 TimeSummary ts = TkServiceLocator.getTimeSummaryService().getTimeSummary(td); 123 List<Assignment> assignments = td.getAssignments(); 124 List<String> assignmentKeys = new ArrayList<String>(); 125 for(Assignment assignment : assignments) { 126 assignmentKeys.add(assignment.getAssignmentKey()); 127 } 128 List<TimeBlock> timeBlocks = td.getTimeBlocks(); 129 List<LeaveBlock> leaveBlocks = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForTimeCalendar(td.getPrincipalId(), td.getAsOfDate(), td.getDocEndDate(), assignmentKeys); 130 Map<String, String> aMap = ActionFormUtils.buildAssignmentStyleClassMap(timeBlocks, leaveBlocks); 131 // set css classes for each assignment row 132 for (EarnGroupSection earnGroupSection : ts.getSections()) { 133 for (EarnCodeSection section : earnGroupSection.getEarnCodeSections()) { 134 for (AssignmentRow assignRow : section.getAssignmentsRows()) { 135 String assignmentCssStyle = MapUtils.getString(aMap, assignRow.getAssignmentKey()); 136 assignRow.setCssClass(assignmentCssStyle); 137 for (AssignmentColumn assignmentColumn : assignRow.getAssignmentColumns()) { 138 assignmentColumn.setCssClass(assignmentCssStyle); 139 } 140 } 141 } 142 } 143 144 //reverse sections for javascripts $parent.after (always inserting directly after parent element, which reverses order) 145 146 ts.setSections(Lists.reverse(ts.getSections())); 147 taaf.setOutputString(ts.toJsonString()); 148 return mapping.findForward("ws"); 149 } 150 151 }