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 = 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 }