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.Date; 023 import java.util.Iterator; 024 import java.util.List; 025 026 import org.apache.commons.lang.StringUtils; 027 import org.kuali.hr.job.Job; 028 import org.kuali.hr.time.department.Department; 029 import org.kuali.hr.time.roles.TkRole; 030 import org.kuali.hr.time.service.base.TkServiceLocator; 031 import org.kuali.hr.time.timeblock.TimeBlock; 032 import org.kuali.hr.time.timeblock.TimeHourDetail; 033 import org.kuali.hr.time.util.TKContext; 034 import org.kuali.hr.time.util.TKUtils; 035 import org.kuali.hr.time.util.TkConstants; 036 import org.kuali.rice.kns.lookup.HtmlData; 037 import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl; 038 import org.kuali.rice.krad.bo.BusinessObject; 039 040 public class TimeBlockLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl { 041 042 /** 043 * 044 */ 045 private static final long serialVersionUID = 1L; 046 047 static final String DOC_ID = "documentId"; 048 static final String DOC_STATUS_ID = "timesheetDocumentHeader.documentStatus"; 049 static final String BEGIN_DATE_ID = "beginDate"; 050 051 @Override 052 public List<? extends BusinessObject> getSearchResults(java.util.Map<String, String> fieldValues) { 053 054 String docStatus = "", beginDateString=""; 055 056 if(fieldValues.containsKey(DOC_STATUS_ID)){ 057 docStatus = fieldValues.get(DOC_STATUS_ID); 058 fieldValues.remove(DOC_STATUS_ID); 059 } 060 if(fieldValues.containsKey(BEGIN_DATE_ID)){ 061 beginDateString = fieldValues.get(BEGIN_DATE_ID); 062 fieldValues.remove(BEGIN_DATE_ID); 063 } 064 065 List<TimeBlock> objectList = (List<TimeBlock>) super.getSearchResults(fieldValues); 066 067 if(!objectList.isEmpty()) { 068 Iterator<? extends BusinessObject> itr = objectList.iterator(); 069 while(itr.hasNext()){ 070 TimeBlock tb = (TimeBlock)itr.next(); 071 List<TkRole> tkRoles = TkServiceLocator.getTkRoleService().getRoles(TKContext.getPrincipalId(), TKUtils.getCurrentDate()); 072 Job job = TkServiceLocator.getJobService().getJob(tb.getUserPrincipalId(), tb.getJobNumber(), TKUtils.getCurrentDate(), false); 073 boolean valid = false; 074 for (TkRole tkRole : tkRoles) { 075 if (StringUtils.equals(tkRole.getRoleName(), 076 TkConstants.ROLE_TK_SYS_ADMIN) 077 || (StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_GLOBAL_VO)) 078 || (StringUtils.equals(tkRole.getRoleName(), 079 TkConstants.ROLE_TK_APPROVER) && tb 080 .getWorkArea().equals(tkRole.getWorkArea())) 081 || (StringUtils.equals(tkRole.getRoleName(), 082 TkConstants.ROLE_TK_DEPT_ADMIN) && (job != null && (job 083 .getDept().equals(tkRole.getDepartment()))))) { 084 valid = true; 085 break; 086 } 087 if(StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_LOCATION_ADMIN) && job != null && tkRole.getLocationObj()!=null){ 088 List<Department> departments = TkServiceLocator.getDepartmentService().getDepartmentByLocation(tkRole.getLocationObj().getLocation()); 089 for(Department department : departments){ 090 if(StringUtils.equals(job.getDept(), department.getDept())){ 091 valid = true; 092 break; 093 } 094 } 095 if(valid){ 096 break; 097 } 098 } 099 } 100 if (!valid) { 101 itr.remove(); 102 continue; 103 } 104 if(StringUtils.isNotEmpty(docStatus)) { 105 if(tb.getTimesheetDocumentHeader() == null) { 106 itr.remove(); 107 continue; 108 } else { 109 if(tb.getTimesheetDocumentHeader().getDocumentStatus() != null) { 110 if(!tb.getTimesheetDocumentHeader().getDocumentStatus().equals(docStatus)){ 111 itr.remove(); 112 continue; 113 } 114 } else { 115 itr.remove(); 116 continue; 117 } 118 } 119 } 120 121 if(StringUtils.isNotEmpty(beginDateString)) { 122 if(tb.getBeginDate() != null) { 123 if(!this.checkDate(tb, tb.getBeginDate(), beginDateString)) { 124 itr.remove(); 125 continue; 126 } 127 } else { 128 itr.remove(); 129 continue; 130 } 131 } 132 } 133 134 // Fetch list from time hour detail and convert it into TimeBlock 135 if(!objectList.isEmpty()) { 136 List<TimeBlock> timeBlocks = new ArrayList<TimeBlock>(objectList); 137 for(TimeBlock tb: timeBlocks) { 138 List<TimeHourDetail> timeHourDetails = tb.getTimeHourDetails(); 139 for(TimeHourDetail thd : timeHourDetails) { 140 if(!thd.getEarnCode().equalsIgnoreCase(tb.getEarnCode())) { 141 TimeBlock timeBlock = new TimeBlock(); 142 timeBlock = tb.copy(); 143 timeBlock.setEarnCode(thd.getEarnCode()); 144 timeBlock.setHours(thd.getHours()); 145 timeBlock.setAmount(thd.getAmount()); 146 objectList.add(timeBlock); 147 } 148 } // inner for ends 149 } // outer for ends 150 } // if ends 151 152 } 153 154 155 return objectList; 156 } 157 158 public boolean checkDate(TimeBlock tb, Date asOfDate, String dateString) { 159 if(tb.getTimesheetDocumentHeader() == null) { 160 return false; 161 } 162 try { 163 DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 164 Date dateFrom; 165 Date dateTo; 166 String subDateString; 167 if(dateString.indexOf("..") == 10) { 168 subDateString= dateString.substring(0, 10); 169 dateFrom = df.parse(subDateString); 170 subDateString= dateString.substring(12, dateString.length()); 171 dateTo = df.parse(subDateString); 172 if(asOfDate != null) { 173 if(!( (asOfDate.after(dateFrom) || asOfDate.equals(dateFrom)) 174 && (asOfDate.before(dateTo) || asOfDate.equals(dateTo)))) { 175 return false; 176 } 177 } else { 178 return false; 179 } 180 } else{ 181 subDateString= dateString.substring(2, dateString.length()); 182 dateTo = df.parse(subDateString); 183 if(asOfDate != null) { 184 if( (dateString.startsWith(">=") && asOfDate.before(dateTo)) 185 || (dateString.startsWith("<=") && asOfDate.after(dateTo))) { 186 return false; 187 } 188 } else { 189 return false; 190 } 191 } 192 } catch (ParseException e) { 193 } 194 return true; 195 } 196 197 @SuppressWarnings("unchecked") 198 @Override 199 public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) { 200 List<HtmlData> customActionUrls = super.getCustomActionUrls(businessObject, pkNames); 201 List<HtmlData> overrideUrls = new ArrayList<HtmlData>(); 202 for(HtmlData actionUrl : customActionUrls){ 203 if(!StringUtils.equals(actionUrl.getMethodToCall(), "copy")){ 204 overrideUrls.add(actionUrl); 205 } 206 } 207 return overrideUrls; 208 } 209 }