View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.timeblock.service;
17  
18  import java.text.DateFormat;
19  import java.text.ParseException;
20  import java.text.SimpleDateFormat;
21  import java.util.ArrayList;
22  import java.util.Date;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.kuali.hr.job.Job;
28  import org.kuali.hr.time.department.Department;
29  import org.kuali.hr.time.roles.TkRole;
30  import org.kuali.hr.time.service.base.TkServiceLocator;
31  import org.kuali.hr.time.timeblock.TimeBlock;
32  import org.kuali.hr.time.timeblock.TimeHourDetail;
33  import org.kuali.hr.time.util.TKContext;
34  import org.kuali.hr.time.util.TKUtils;
35  import org.kuali.hr.time.util.TkConstants;
36  import org.kuali.rice.kns.lookup.HtmlData;
37  import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
38  import org.kuali.rice.krad.bo.BusinessObject;
39  
40  public class TimeBlockLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
41  
42  	/**
43  	 * 
44  	 */
45  	private static final long serialVersionUID = 1L;
46  	
47  	static final String DOC_ID = "documentId";
48  	static final String DOC_STATUS_ID = "timesheetDocumentHeader.documentStatus";
49  	static final String BEGIN_DATE_ID = "beginDate";
50  		
51  	 @Override
52      public List<? extends BusinessObject> getSearchResults(java.util.Map<String, String> fieldValues) {
53  	 
54  		 String docStatus = "", beginDateString="";
55  
56  		 if(fieldValues.containsKey(DOC_STATUS_ID)){
57  				docStatus = fieldValues.get(DOC_STATUS_ID);
58  				fieldValues.remove(DOC_STATUS_ID);
59  			}
60  		 if(fieldValues.containsKey(BEGIN_DATE_ID)){
61  			 	beginDateString = fieldValues.get(BEGIN_DATE_ID);
62  				fieldValues.remove(BEGIN_DATE_ID);
63  			}
64          
65          List<TimeBlock> objectList = (List<TimeBlock>) super.getSearchResults(fieldValues);
66        
67          if(!objectList.isEmpty()) {
68          	Iterator<? extends BusinessObject> itr = objectList.iterator();
69  			while(itr.hasNext()){
70  				TimeBlock tb = (TimeBlock)itr.next();
71  				List<TkRole> tkRoles = TkServiceLocator.getTkRoleService().getRoles(TKContext.getPrincipalId(), TKUtils.getCurrentDate());
72  				Job job = TkServiceLocator.getJobService().getJob(tb.getUserPrincipalId(), tb.getJobNumber(), TKUtils.getCurrentDate(), false);
73  				boolean valid = false;
74  				for (TkRole tkRole : tkRoles) {
75  					if (StringUtils.equals(tkRole.getRoleName(),
76  							TkConstants.ROLE_TK_SYS_ADMIN)
77  							|| (StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_GLOBAL_VO))
78  							|| (StringUtils.equals(tkRole.getRoleName(),
79  									TkConstants.ROLE_TK_APPROVER) && tb
80  									.getWorkArea().equals(tkRole.getWorkArea()))
81  							|| (StringUtils.equals(tkRole.getRoleName(),
82  									TkConstants.ROLE_TK_DEPT_ADMIN) && (job != null && (job
83  									.getDept().equals(tkRole.getDepartment()))))) {
84  						valid = true;
85  						break;
86  					}
87  					if(StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_LOCATION_ADMIN) && job != null && tkRole.getLocationObj()!=null){
88  						List<Department> departments = TkServiceLocator.getDepartmentService().getDepartmentByLocation(tkRole.getLocationObj().getLocation());
89  						for(Department department : departments){
90  							if(StringUtils.equals(job.getDept(), department.getDept())){
91  								valid = true;
92  								break;
93  							}
94  						}
95  						if(valid){
96  							break;
97  						}
98  					}
99  				}
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 = tb.copy();
142 						  timeBlock.setEarnCode(thd.getEarnCode());
143 						  timeBlock.setHours(thd.getHours());
144 						  timeBlock.setAmount(thd.getAmount());
145 						  objectList.add(timeBlock);
146 					  }
147 					} // inner for ends
148 				} // outer for ends
149 			} // if ends
150 			
151         }
152         
153      
154         return objectList;
155 	 }
156 	 
157 	 public boolean checkDate(TimeBlock tb, Date asOfDate, String dateString) {
158 		 if(tb.getTimesheetDocumentHeader() == null) {
159 				return false;
160 		 }
161 		try {
162 			DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
163 			Date dateFrom;
164 			Date dateTo;
165 			String subDateString;
166 			if(dateString.indexOf("..") == 10) {
167 				subDateString= dateString.substring(0, 10);
168 				dateFrom = df.parse(subDateString);
169 				subDateString= dateString.substring(12, dateString.length());
170 				dateTo = df.parse(subDateString);
171 				if(asOfDate != null) {
172 					if(!( (asOfDate.after(dateFrom) || asOfDate.equals(dateFrom))
173 							&& (asOfDate.before(dateTo) || asOfDate.equals(dateTo)))) {
174 						return false;
175 					}
176 				} else {
177 					return false;
178 				}
179 			} else{
180 				subDateString= dateString.substring(2, dateString.length());
181 				dateTo = df.parse(subDateString);
182 				if(asOfDate != null) {
183 					if( (dateString.startsWith(">=") && asOfDate.before(dateTo))
184 							|| (dateString.startsWith("<=") && asOfDate.after(dateTo))) {
185 						return false;
186 					}
187 				} else {
188 					return false;
189 				}
190 			}
191 		} catch (ParseException e) {
192 		}
193 	  return true;
194 	 }
195 	 
196 	@SuppressWarnings("unchecked")
197 	@Override
198 	public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
199 		List<HtmlData> customActionUrls = super.getCustomActionUrls(businessObject, pkNames);
200 		List<HtmlData> overrideUrls = new ArrayList<HtmlData>();
201 		for(HtmlData actionUrl : customActionUrls){
202 			if(!StringUtils.equals(actionUrl.getMethodToCall(), "copy")){
203 				overrideUrls.add(actionUrl);
204 			}
205 		}
206 		return overrideUrls;
207 	}
208 }