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.timeblock.service; 017 018 import java.text.DateFormat; 019 import java.text.ParseException; 020 import java.text.SimpleDateFormat; 021 import java.util.ArrayList; 022 import java.util.Collections; 023 import java.util.Comparator; 024 import java.util.Date; 025 import java.util.HashMap; 026 import java.util.Iterator; 027 import java.util.List; 028 import java.util.Map; 029 030 import org.apache.commons.lang.StringUtils; 031 import org.joda.time.Interval; 032 import org.kuali.hr.job.Job; 033 import org.kuali.hr.lm.leaveblock.LeaveBlockHistory; 034 import org.kuali.hr.time.department.Department; 035 import org.kuali.hr.time.roles.TkRole; 036 import org.kuali.hr.time.service.base.TkServiceLocator; 037 import org.kuali.hr.time.timeblock.TimeBlockHistory; 038 import org.kuali.hr.time.timeblock.TimeBlockHistoryDetail; 039 import org.kuali.hr.time.util.TKContext; 040 import org.kuali.hr.time.util.TKUtils; 041 import org.kuali.hr.time.util.TkConstants; 042 import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl; 043 import org.kuali.rice.krad.bo.BusinessObject; 044 045 public class TimeBlockHistoryDetailLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl { 046 047 /** 048 * 049 */ 050 private static final long serialVersionUID = 1L; 051 052 static final String DOC_ID = "documentId"; 053 static final String DOC_STATUS_ID = "timeBlockHistory.timesheetDocumentHeader.documentStatus"; 054 static final String BEGIN_DATE_ID = "beginDate"; 055 056 @SuppressWarnings("unchecked") 057 @Override 058 public List<? extends BusinessObject> getSearchResults(java.util.Map<String, String> fieldValues) { 059 060 String docStatus = "", beginDateString = ""; 061 062 if (fieldValues.containsKey(DOC_STATUS_ID)) { 063 docStatus = fieldValues.get(DOC_STATUS_ID); 064 fieldValues.remove(DOC_STATUS_ID); 065 } 066 if (fieldValues.containsKey(BEGIN_DATE_ID)) { 067 beginDateString = fieldValues.get(BEGIN_DATE_ID); 068 fieldValues.remove(BEGIN_DATE_ID); 069 } 070 List<TimeBlockHistoryDetail> objectList = (List<TimeBlockHistoryDetail>) super.getSearchResults(fieldValues); 071 Map<String,List<TimeBlockHistoryDetail>> timeBlockHistoryToDetailMap = new HashMap<String,List<TimeBlockHistoryDetail>>(); 072 Map<String,List<TimeBlockHistoryDetail>> filteredTimeBlockHistoryToDetailMap = new HashMap<String,List<TimeBlockHistoryDetail>>(); 073 074 if (!objectList.isEmpty()) { 075 this.filterWithSecurity(objectList); 076 077 for(TimeBlockHistoryDetail tbhd : objectList){ 078 if(!timeBlockHistoryToDetailMap.containsKey(tbhd.getTkTimeBlockHistoryId())){ 079 List<TimeBlockHistoryDetail> thdList = new ArrayList<TimeBlockHistoryDetail>(); 080 timeBlockHistoryToDetailMap.put(tbhd.getTkTimeBlockHistoryId(), thdList); 081 } 082 083 List<TimeBlockHistoryDetail> thdList = timeBlockHistoryToDetailMap.get(tbhd.getTkTimeBlockHistoryId()); 084 thdList.add(tbhd); 085 } 086 filteredTimeBlockHistoryToDetailMap.putAll(timeBlockHistoryToDetailMap); 087 088 for (Map.Entry<String, List<TimeBlockHistoryDetail>> entry : timeBlockHistoryToDetailMap.entrySet()) { 089 String timeHourDetailId = entry.getKey(); 090 List<TimeBlockHistoryDetail> tbhdList = entry.getValue(); 091 TimeBlockHistoryDetail tbhd = tbhdList.get(0); 092 TimeBlockHistory tbh = tbhd.getTimeBlockHistory(); 093 094 if(StringUtils.isNotEmpty(docStatus)){ 095 if(tbh.getTimesheetDocumentHeader() == null){ 096 filteredTimeBlockHistoryToDetailMap.remove(timeHourDetailId); 097 continue; 098 } else { 099 if (tbh.getTimesheetDocumentHeader().getDocumentStatus() != null) { 100 if (!tbh.getTimesheetDocumentHeader().getDocumentStatus().equals(docStatus)) { 101 filteredTimeBlockHistoryToDetailMap.remove(timeHourDetailId); 102 continue; 103 } 104 } else { 105 filteredTimeBlockHistoryToDetailMap.remove(timeHourDetailId); 106 continue; 107 } 108 } 109 } 110 111 if(StringUtils.isNotEmpty(beginDateString)) { 112 if(tbh.getBeginDate() != null) { 113 if(!this.inDateRange(tbh.getBeginDate(), beginDateString)) { 114 filteredTimeBlockHistoryToDetailMap.remove(timeHourDetailId); 115 continue; 116 } 117 } else { 118 filteredTimeBlockHistoryToDetailMap.remove(timeHourDetailId); 119 continue; 120 } 121 } 122 } 123 } 124 125 List<TimeBlockHistoryDetail> lstFinalList = new ArrayList<TimeBlockHistoryDetail>(); 126 for(List<TimeBlockHistoryDetail> tbhdList : filteredTimeBlockHistoryToDetailMap.values()){ 127 lstFinalList.addAll(tbhdList); 128 } 129 130 sortByTimeBlockId(lstFinalList); 131 return lstFinalList; 132 } 133 134 private void filterWithSecurity(List<TimeBlockHistoryDetail> objectList) { 135 Iterator<? extends BusinessObject> itr = objectList.iterator(); 136 List<TkRole> tkRoles = TkServiceLocator.getTkRoleService().getRoles(TKContext.getPrincipalId(), TKUtils.getCurrentDate()); 137 while(itr.hasNext()){ 138 TimeBlockHistoryDetail tbhd = (TimeBlockHistoryDetail)itr.next(); 139 Job job = TkServiceLocator.getJobService().getJob(tbhd.getTimeBlockHistory().getPrincipalId(), tbhd.getTimeBlockHistory().getJobNumber(), tbhd.getTimeBlockHistory().getEndTimestamp(), false); 140 boolean valid = false; 141 for (TkRole tkRole : tkRoles) { 142 if (StringUtils.equals(tkRole.getRoleName(), 143 TkConstants.ROLE_TK_SYS_ADMIN) 144 || (StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_GLOBAL_VO)) 145 || (StringUtils.equals(tkRole.getRoleName(), 146 TkConstants.ROLE_TK_APPROVER) && tbhd.getTimeBlockHistory().getWorkArea().equals(tkRole.getWorkArea())) 147 || (StringUtils.equals(tkRole.getRoleName(), 148 TkConstants.ROLE_TK_DEPT_ADMIN) && (job != null && (job.getDept().equals(tkRole.getDepartment()))))) { 149 valid = true; 150 break; 151 } 152 if(StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_LOCATION_ADMIN) && job != null && tkRole.getLocationObj()!=null){ 153 List<Department> departments = TkServiceLocator.getDepartmentService().getDepartmentByLocation(tkRole.getLocationObj().getLocation()); 154 for(Department department : departments){ 155 if(StringUtils.equals(job.getDept(), department.getDept())){ 156 valid = true; 157 break; 158 } 159 } 160 if(valid){ 161 break; 162 } 163 } 164 } 165 if (!valid) { 166 itr.remove(); 167 continue; 168 } 169 } 170 } 171 172 private void sortByTimeBlockId(List<TimeBlockHistoryDetail> objectList) { 173 Collections.sort(objectList, new Comparator<TimeBlockHistoryDetail>() { // Sort the Time Blocks 174 @Override 175 public int compare(TimeBlockHistoryDetail timeBlockHistory, TimeBlockHistoryDetail timeBlockHistory1) { 176 //test for nulls. 177 if (timeBlockHistory.getTimeBlockHistory().getTkTimeBlockId() == null || timeBlockHistory1.getTimeBlockHistory().getTkTimeBlockId() == null) { 178 if (timeBlockHistory.getTimeBlockHistory().getTkTimeBlockId() == null && timeBlockHistory1.getTimeBlockHistory().getTkTimeBlockId() == null) { 179 return 0; 180 } 181 else { 182 return timeBlockHistory.getTimeBlockHistory().getTkTimeBlockId() == null ? -1 : 1; 183 } 184 } 185 186 return timeBlockHistory.getTimeBlockHistory().getTkTimeBlockId().compareTo(timeBlockHistory1.getTimeBlockHistory().getTkTimeBlockId()); 187 } 188 }); 189 } 190 191 public boolean inDateRange(Date asOfDate, String dateString) { 192 try { 193 DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 194 java.util.Date dateFrom = df.parse(TKUtils.getFromDateString(dateString)); 195 java.util.Date dateTo = df.parse(TKUtils.getToDateString(dateString));; 196 197 if(dateString.indexOf("..") == 10) { 198 if(asOfDate != null) { 199 Interval range = new Interval(dateFrom.getTime(),dateTo.getTime()); 200 return range.contains(asOfDate.getTime()); 201 } else { 202 return false; 203 } 204 } else{ 205 if(asOfDate != null) { 206 if( (dateString.startsWith(">=") && asOfDate.before(dateTo)) 207 || (dateString.startsWith("<=") && asOfDate.after(dateTo))) { 208 return false; 209 } 210 } else { 211 return false; 212 } 213 } 214 } catch (ParseException e) { 215 } 216 return true; 217 } 218 }