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.time.department.Department; 034 import org.kuali.hr.time.roles.TkRole; 035 import org.kuali.hr.time.service.base.TkServiceLocator; 036 import org.kuali.hr.time.timeblock.TimeBlockHistory; 037 import org.kuali.hr.time.timeblock.TimeBlockHistoryDetail; 038 import org.kuali.hr.time.util.TKContext; 039 import org.kuali.hr.time.util.TKUtils; 040 import org.kuali.hr.time.util.TkConstants; 041 import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl; 042 import org.kuali.rice.krad.bo.BusinessObject; 043 044 public class TimeBlockHistoryDetailLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl { 045 046 /** 047 * 048 */ 049 private static final long serialVersionUID = 1L; 050 051 static final String DOC_ID = "documentId"; 052 static final String DOC_STATUS_ID = "timeBlockHistory.timesheetDocumentHeader.documentStatus"; 053 static final String BEGIN_DATE_ID = "beginDate"; 054 055 @SuppressWarnings("unchecked") 056 @Override 057 public List<? extends BusinessObject> getSearchResults(java.util.Map<String, String> fieldValues) { 058 059 String docStatus = "", beginDateString = ""; 060 061 if (fieldValues.containsKey(DOC_STATUS_ID)) { 062 docStatus = fieldValues.get(DOC_STATUS_ID); 063 fieldValues.remove(DOC_STATUS_ID); 064 } 065 if (fieldValues.containsKey(BEGIN_DATE_ID)) { 066 beginDateString = fieldValues.get(BEGIN_DATE_ID); 067 fieldValues.remove(BEGIN_DATE_ID); 068 } 069 List<TimeBlockHistoryDetail> objectList = (List<TimeBlockHistoryDetail>) super.getSearchResults(fieldValues); 070 Map<String,List<TimeBlockHistoryDetail>> timeBlockHistoryToDetailMap = new HashMap<String,List<TimeBlockHistoryDetail>>(); 071 Map<String,List<TimeBlockHistoryDetail>> filteredTimeBlockHistoryToDetailMap = new HashMap<String,List<TimeBlockHistoryDetail>>(); 072 073 if (!objectList.isEmpty()) { 074 this.filterWithSecurity(objectList); 075 076 for(TimeBlockHistoryDetail tbhd : objectList){ 077 if(!timeBlockHistoryToDetailMap.containsKey(tbhd.getTkTimeBlockHistoryId())){ 078 List<TimeBlockHistoryDetail> thdList = new ArrayList<TimeBlockHistoryDetail>(); 079 timeBlockHistoryToDetailMap.put(tbhd.getTkTimeBlockHistoryId(), thdList); 080 } 081 082 List<TimeBlockHistoryDetail> thdList = timeBlockHistoryToDetailMap.get(tbhd.getTkTimeBlockHistoryId()); 083 thdList.add(tbhd); 084 } 085 filteredTimeBlockHistoryToDetailMap.putAll(timeBlockHistoryToDetailMap); 086 087 Iterator<String> itr = timeBlockHistoryToDetailMap.keySet().iterator(); 088 while(itr.hasNext()){ 089 String timeHourDetailId = itr.next(); 090 List<TimeBlockHistoryDetail> tbhdList = timeBlockHistoryToDetailMap.get(timeHourDetailId); 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(), TKUtils.getCurrentDate(), 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 return timeBlockHistory.getTimeBlockHistory().getTkTimeBlockId().compareTo(timeBlockHistory1.getTimeBlockHistory().getTkTimeBlockId()); 177 } 178 }); 179 } 180 181 public boolean inDateRange(Date asOfDate, String dateString) { 182 try { 183 DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 184 java.util.Date dateFrom = df.parse(TKUtils.getFromDateString(dateString)); 185 java.util.Date dateTo = df.parse(TKUtils.getToDateString(dateString));; 186 187 if(dateString.indexOf("..") == 10) { 188 if(asOfDate != null) { 189 Interval range = new Interval(dateFrom.getTime(),dateTo.getTime()); 190 return range.contains(asOfDate.getTime()); 191 } else { 192 return false; 193 } 194 } else{ 195 if(asOfDate != null) { 196 if( (dateString.startsWith(">=") && asOfDate.before(dateTo)) 197 || (dateString.startsWith("<=") && asOfDate.after(dateTo))) { 198 return false; 199 } 200 } else { 201 return false; 202 } 203 } 204 } catch (ParseException e) { 205 } 206 return true; 207 } 208 }