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 private static final String BEGIN_TIMESTAMP = "beginTimestamp"; 051 052 @Override 053 public List<? extends BusinessObject> getSearchResults(java.util.Map<String, String> fieldValues) { 054 055 if (fieldValues.containsKey(BEGIN_DATE_ID)) { 056 //beginDate = fieldValues.get(BEGIN_DATE); 057 fieldValues.put(BEGIN_TIMESTAMP, fieldValues.get(BEGIN_DATE_ID)); 058 fieldValues.remove(BEGIN_DATE_ID); 059 } 060 061 List<TimeBlock> objectList = (List<TimeBlock>) super.getSearchResults(fieldValues); 062 063 if(!objectList.isEmpty()) { 064 Iterator<? extends BusinessObject> itr = objectList.iterator(); 065 while(itr.hasNext()){ 066 TimeBlock tb = (TimeBlock)itr.next(); 067 List<TkRole> tkRoles = TkServiceLocator.getTkRoleService().getRoles(TKContext.getPrincipalId(), TKUtils.getCurrentDate()); 068 Job job = TkServiceLocator.getJobService().getJob(tb.getUserPrincipalId(), tb.getJobNumber(), TKUtils.getCurrentDate(), false); 069 boolean valid = false; 070 for (TkRole tkRole : tkRoles) { 071 if (StringUtils.equals(tkRole.getRoleName(), 072 TkConstants.ROLE_TK_SYS_ADMIN) 073 || (StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_GLOBAL_VO)) 074 || (StringUtils.equals(tkRole.getRoleName(), 075 TkConstants.ROLE_TK_APPROVER) && tb 076 .getWorkArea().equals(tkRole.getWorkArea())) 077 || (StringUtils.equals(tkRole.getRoleName(), 078 TkConstants.ROLE_TK_DEPT_ADMIN) && (job != null && (job 079 .getDept().equals(tkRole.getDepartment()))))) { 080 valid = true; 081 break; 082 } 083 if(StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_LOCATION_ADMIN) && job != null && tkRole.getLocationObj()!=null){ 084 List<Department> departments = TkServiceLocator.getDepartmentService().getDepartmentByLocation(tkRole.getLocationObj().getLocation()); 085 for(Department department : departments){ 086 if(StringUtils.equals(job.getDept(), department.getDept())){ 087 valid = true; 088 break; 089 } 090 } 091 if(valid){ 092 break; 093 } 094 } 095 } 096 if (!valid) { 097 itr.remove(); 098 continue; 099 } 100 } 101 102 // Fetch list from time hour detail and convert it into TimeBlock 103 if(!objectList.isEmpty()) { 104 List<TimeBlock> timeBlocks = new ArrayList<TimeBlock>(objectList); 105 for(TimeBlock tb: timeBlocks) { 106 List<TimeHourDetail> timeHourDetails = tb.getTimeHourDetails(); 107 for(TimeHourDetail thd : timeHourDetails) { 108 if(!thd.getEarnCode().equalsIgnoreCase(tb.getEarnCode())) { 109 TimeBlock timeBlock = tb.copy(); 110 timeBlock.setEarnCode(thd.getEarnCode()); 111 timeBlock.setHours(thd.getHours()); 112 timeBlock.setAmount(thd.getAmount()); 113 objectList.add(timeBlock); 114 } 115 } // inner for ends 116 } // outer for ends 117 } // if ends 118 119 } 120 121 122 return objectList; 123 } 124 125 public boolean checkDate(TimeBlock tb, Date asOfDate, String dateString) { 126 if(tb.getTimesheetDocumentHeader() == null) { 127 return false; 128 } 129 try { 130 DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 131 Date dateFrom; 132 Date dateTo; 133 String subDateString; 134 if(dateString.indexOf("..") == 10) { 135 subDateString= dateString.substring(0, 10); 136 dateFrom = df.parse(subDateString); 137 subDateString= dateString.substring(12, dateString.length()); 138 dateTo = df.parse(subDateString); 139 if(asOfDate != null) { 140 if(!( (asOfDate.after(dateFrom) || asOfDate.equals(dateFrom)) 141 && (asOfDate.before(dateTo) || asOfDate.equals(dateTo)))) { 142 return false; 143 } 144 } else { 145 return false; 146 } 147 } else{ 148 subDateString= dateString.substring(2, dateString.length()); 149 dateTo = df.parse(subDateString); 150 if(asOfDate != null) { 151 if( (dateString.startsWith(">=") && asOfDate.before(dateTo)) 152 || (dateString.startsWith("<=") && asOfDate.after(dateTo))) { 153 return false; 154 } 155 } else { 156 return false; 157 } 158 } 159 } catch (ParseException e) { 160 } 161 return true; 162 } 163 164 @SuppressWarnings("unchecked") 165 @Override 166 public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) { 167 List<HtmlData> customActionUrls = super.getCustomActionUrls(businessObject, pkNames); 168 List<HtmlData> overrideUrls = new ArrayList<HtmlData>(); 169 for(HtmlData actionUrl : customActionUrls){ 170 if(!StringUtils.equals(actionUrl.getMethodToCall(), "copy")){ 171 overrideUrls.add(actionUrl); 172 } 173 } 174 return overrideUrls; 175 } 176 }