View Javadoc

1   /**
2    * Copyright 2004-2012 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.util.TKContext;
33  import org.kuali.hr.time.util.TKUtils;
34  import org.kuali.hr.time.util.TkConstants;
35  import org.kuali.rice.kns.lookup.HtmlData;
36  import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
37  import org.kuali.rice.krad.bo.BusinessObject;
38  
39  public class TimeBlockLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
40  
41  	/**
42  	 * 
43  	 */
44  	private static final long serialVersionUID = 1L;
45  	
46  	static final String DOC_ID = "documentId";
47  	static final String DOC_STATUS_ID = "timesheetDocumentHeader.documentStatus";
48  	static final String BEGIN_DATE_ID = "beginDate";
49  		
50  	 @Override
51      public List<? extends BusinessObject> getSearchResults(java.util.Map<String, String> fieldValues) {
52  	 
53  		 String docStatus = "", beginDateString="";
54  
55  		 if(fieldValues.containsKey(DOC_STATUS_ID)){
56  				docStatus = fieldValues.get(DOC_STATUS_ID);
57  				fieldValues.remove(DOC_STATUS_ID);
58  			}
59  		 if(fieldValues.containsKey(BEGIN_DATE_ID)){
60  			 	beginDateString = fieldValues.get(BEGIN_DATE_ID);
61  				fieldValues.remove(BEGIN_DATE_ID);
62  			}
63          
64          List<? extends BusinessObject> objectList = super.getSearchResults(fieldValues);
65        
66          if(!objectList.isEmpty()) {
67          	Iterator<? extends BusinessObject> itr = objectList.iterator();
68  			while(itr.hasNext()){
69  				TimeBlock tb = (TimeBlock)itr.next();
70  				List<TkRole> tkRoles = TkServiceLocator.getTkRoleService().getRoles(TKContext.getPrincipalId(), TKUtils.getCurrentDate());
71  				Job job = TkServiceLocator.getJobService().getJob(tb.getUserPrincipalId(), tb.getJobNumber(), TKUtils.getCurrentDate(), false);
72  				boolean valid = false;
73  				for (TkRole tkRole : tkRoles) {
74  					if (StringUtils.equals(tkRole.getRoleName(),
75  							TkConstants.ROLE_TK_SYS_ADMIN)
76  							|| (StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_GLOBAL_VO))
77  							|| (StringUtils.equals(tkRole.getRoleName(),
78  									TkConstants.ROLE_TK_APPROVER) && tb
79  									.getWorkArea().equals(tkRole.getWorkArea()))
80  							|| (StringUtils.equals(tkRole.getRoleName(),
81  									TkConstants.ROLE_TK_DEPT_ADMIN) && (job != null && (job
82  									.getDept().equals(tkRole.getDepartment()))))) {
83  						valid = true;
84  						break;
85  					}
86  					if(StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_LOCATION_ADMIN) && job != null && tkRole.getLocationObj()!=null){
87  						List<Department> departments = TkServiceLocator.getDepartmentService().getDepartmentByLocation(tkRole.getLocationObj().getLocation());
88  						for(Department department : departments){
89  							if(StringUtils.equals(job.getDept(), department.getDept())){
90  								valid = true;
91  								break;
92  							}
93  						}
94  						if(valid){
95  							break;
96  						}
97  					}
98  				}
99  				if (!valid) {
100 					itr.remove();
101 					continue;
102 				}
103 				if(StringUtils.isNotEmpty(docStatus)) {
104 					if(tb.getTimesheetDocumentHeader() == null) {
105 						itr.remove();
106 						continue;
107 					} else {
108 						if(tb.getTimesheetDocumentHeader().getDocumentStatus() != null) {
109 							if(!tb.getTimesheetDocumentHeader().getDocumentStatus().equals(docStatus)){
110 								itr.remove();
111 								continue;
112 							}
113 						} else {
114 							itr.remove();
115 							continue;
116 						}
117 					}
118 				}
119 								
120 				if(StringUtils.isNotEmpty(beginDateString)) {
121 					if(tb.getBeginDate() != null) {
122 						if(!this.checkDate(tb, tb.getBeginDate(), beginDateString)) {
123 							itr.remove();
124 							continue;
125 						} 
126 					} else {
127 						itr.remove();
128 						continue;
129 					}
130 				}				
131 			}
132         }
133         return objectList;
134 	 }
135 	 
136 	 public boolean checkDate(TimeBlock tb, Date asOfDate, String dateString) {
137 		 if(tb.getTimesheetDocumentHeader() == null) {
138 				return false;
139 		 }
140 		try {
141 			DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
142 			Date dateFrom;
143 			Date dateTo;
144 			String subDateString;
145 			if(dateString.indexOf("..") == 10) {
146 				subDateString= dateString.substring(0, 10);
147 				dateFrom = df.parse(subDateString);
148 				subDateString= dateString.substring(12, dateString.length());
149 				dateTo = df.parse(subDateString);
150 				if(asOfDate != null) {
151 					if(!( (asOfDate.after(dateFrom) || asOfDate.equals(dateFrom))
152 							&& (asOfDate.before(dateTo) || asOfDate.equals(dateTo)))) {
153 						return false;
154 					}
155 				} else {
156 					return false;
157 				}
158 			} else{
159 				subDateString= dateString.substring(2, dateString.length());
160 				dateTo = df.parse(subDateString);
161 				if(asOfDate != null) {
162 					if( (dateString.startsWith(">=") && asOfDate.before(dateTo))
163 							|| (dateString.startsWith("<=") && asOfDate.after(dateTo))) {
164 						return false;
165 					}
166 				} else {
167 					return false;
168 				}
169 			}
170 		} catch (ParseException e) {
171 		}
172 	  return true;
173 	 }
174 	 
175 	@SuppressWarnings("unchecked")
176 	@Override
177 	public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
178 		List<HtmlData> customActionUrls = super.getCustomActionUrls(businessObject, pkNames);
179 		List<HtmlData> overrideUrls = new ArrayList<HtmlData>();
180 		for(HtmlData actionUrl : customActionUrls){
181 			if(!StringUtils.equals(actionUrl.getMethodToCall(), "copy")){
182 				overrideUrls.add(actionUrl);
183 			}
184 		}
185 		return overrideUrls;
186 	}
187 }