View Javadoc
1   /**
2    * Copyright 2004-2014 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.kpme.tklm.leave.block.web;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.joda.time.DateTime;
21  import org.joda.time.LocalDate;
22  import org.kuali.kpme.core.api.department.Department;
23  import org.kuali.kpme.core.api.earncode.EarnCodeContract;
24  import org.kuali.kpme.core.api.job.JobContract;
25  import org.kuali.kpme.core.api.namespace.KPMENamespace;
26  import org.kuali.kpme.core.lookup.KPMELookupableImpl;
27  import org.kuali.kpme.core.role.KPMERole;
28  import org.kuali.kpme.core.service.HrServiceLocator;
29  import org.kuali.kpme.core.util.HrContext;
30  import org.kuali.kpme.core.util.TKUtils;
31  import org.kuali.kpme.tklm.api.leave.block.LeaveBlock;
32  import org.kuali.kpme.tklm.leave.block.LeaveBlockBo;
33  import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
34  import org.kuali.kpme.tklm.leave.workflow.LeaveCalendarDocumentHeader;
35  import org.kuali.rice.core.api.search.Range;
36  import org.kuali.rice.core.api.search.SearchExpressionUtils;
37  import org.kuali.rice.krad.bo.BusinessObject;
38  import org.kuali.rice.krad.lookup.LookupForm;
39  import org.kuali.rice.krad.lookup.LookupView;
40  import org.kuali.rice.krad.util.GlobalVariables;
41  
42  import java.util.*;
43  
44  public class LeaveBlockLookupableHelperServiceImpl extends KPMELookupableImpl  {
45  	/**
46  	 * 
47  	 */
48  	private static final long serialVersionUID = 1L;
49  	
50  	private static final String DOC_ID = "documentId";
51  	private static final String DOC_STATUS_ID = "leaveCalendarDocumentHeader.documentStatus";
52  	private static final String BEGIN_DATE_ID = "beginDate";
53  	private static final String BEGIN_TIMESTAMP = "beginTimestamp";
54  
55  	private static final Logger LOG = Logger.getLogger(LeaveBlockLookupableHelperServiceImpl.class);
56  
57  
58      @Override
59      protected Collection<?> executeSearch(Map<String, String> searchCriteria, List<String> wildcardAsLiteralSearchCriteria, boolean bounded, Integer searchResultsLimit) {
60  		// TODO Auto-generated method stub
61  		 if (searchCriteria.containsKey(BEGIN_DATE_ID)) {
62  			 //beginDate = searchCriteria.get(BEGIN_DATE);
63  			 searchCriteria.put(BEGIN_TIMESTAMP, searchCriteria.get(BEGIN_DATE_ID));
64  			 searchCriteria.remove(BEGIN_DATE_ID);
65  		 }
66  		 
67  		String documentId = searchCriteria.get(DOC_ID);
68  		String principalId = searchCriteria.get("principalId");
69  		String userPrincipalId = searchCriteria.get("userPrincipalId");
70  		String leaveBlockType = searchCriteria.get("leaveBlockType");
71  		String affectPay = searchCriteria.get("affectPay");
72  		String fromDateString = TKUtils.getFromDateString(searchCriteria.get(BEGIN_TIMESTAMP));
73  		String toDateString = TKUtils.getToDateString(searchCriteria.get(BEGIN_TIMESTAMP));
74  		
75  		LocalDate fromDate = null;
76  		LocalDate toDate = null;
77  		if(StringUtils.isNotBlank(searchCriteria.get(BEGIN_TIMESTAMP))) {
78  			Range range = SearchExpressionUtils.parseRange(searchCriteria.get(BEGIN_TIMESTAMP));
79  			boolean invalid = false;
80  			if(range.getLowerBoundValue() != null && range.getUpperBoundValue() != null) {
81  				fromDate = TKUtils.formatDateString(fromDateString);
82  				if(fromDate == null) {
83  					GlobalVariables.getMessageMap().putError("lookupCriteria[rangeLowerBoundKeyPrefix_beginDate]", "error.invalidLookupDate", range.getLowerBoundValue());
84  					invalid = true;
85  				}
86  
87  				toDate = TKUtils.formatDateString(toDateString);
88  				if(toDate == null) {
89  					GlobalVariables.getMessageMap().putError("lookupCriteria[beginDate]", "error.invalidLookupDate", range.getUpperBoundValue());
90  					invalid = true;
91  				}
92  			}
93  			else if(range.getLowerBoundValue() != null) {
94  				fromDate = TKUtils.formatDateString(fromDateString);
95  				if(fromDate == null) {
96  					GlobalVariables.getMessageMap().putError("lookupCriteria[rangeLowerBoundKeyPrefix_beginDate]", "error.invalidLookupDate", range.getLowerBoundValue());
97  					invalid = true;
98  				}
99  			}
100 			else if(range.getUpperBoundValue() != null) {
101 				toDate = TKUtils.formatDateString(toDateString);
102 				if(toDate == null) {
103 					GlobalVariables.getMessageMap().putError("lookupCriteria[beginDate]", "error.invalidLookupDate", range.getUpperBoundValue());
104 					invalid = true;
105 				}
106 			}
107 			if(invalid) {
108 				return new ArrayList<LeaveBlockBo>();
109 			}
110 		}
111 
112 		//Could also simply use super.getSearchResults for an initial object list, then invoke LeaveBlockService with the relevant query params.
113 		List<LeaveBlock> leaveBlockList = LmServiceLocator.getLeaveBlockService().getLeaveBlocksForLookup(documentId, principalId, userPrincipalId, fromDate, toDate, leaveBlockType);
114 		List<LeaveBlockBo> objectList = new ArrayList<LeaveBlockBo>();
115 		
116 		for(LeaveBlock lBlock : leaveBlockList) {
117 			LeaveCalendarDocumentHeader lcHeader = LmServiceLocator.getLeaveCalendarDocumentHeaderService().getDocumentHeader(lBlock.getDocumentId());
118 			
119 			if(lcHeader != null) {
120 				if(StringUtils.isNotBlank(searchCriteria.get(DOC_STATUS_ID))) {
121 					//only add if doc status is one of those specified
122 					if(searchCriteria.get(DOC_STATUS_ID).contains("category")) {
123 						//format for categorical statuses is "category:Q" where 'Q' is one of "P,S,U".
124 						//which category was selected, and is the time block on a timesheet with this status.
125 						if(searchCriteria.get(DOC_STATUS_ID).contains("P")) {
126 							//pending statuses
127 							if("I,S,R,E".contains(lcHeader.getDocumentStatus())) {
128 								objectList.add(LeaveBlockBo.from(lBlock));
129 							}
130 						}
131 						else if(searchCriteria.get(DOC_STATUS_ID).contains("S")) {
132 							//successful statuses
133 							if("P,F".contains(lcHeader.getDocumentStatus())) {
134 								objectList.add(LeaveBlockBo.from(lBlock));
135 							}
136 						}
137 						else if(searchCriteria.get(DOC_STATUS_ID).contains("U")) {
138 							//unsuccessful statuses
139 							if("X,D".contains(lcHeader.getDocumentStatus())) {
140 								objectList.add(LeaveBlockBo.from(lBlock));
141 							}
142 						}
143 					}
144 					else if(searchCriteria.get(DOC_STATUS_ID).contains(lcHeader.getDocumentStatus())) {
145 						//match the specific doc status
146 						objectList.add(LeaveBlockBo.from(lBlock));
147 					}
148 				}
149 				else {
150 					//no status specified, add regardless of status
151 					objectList.add(LeaveBlockBo.from(lBlock));
152 				}
153 					
154 				
155 			} else if(StringUtils.isBlank(searchCriteria.get(DOC_STATUS_ID))) {
156 				//can't match doc status with a non existent header
157 				//only add to list if no status was selected
158 				objectList.add(LeaveBlockBo.from(lBlock));
159 			}
160 		}
161 		
162 		
163 		
164         if(!objectList.isEmpty()) {
165         	Iterator<? extends BusinessObject> itr = objectList.iterator();
166             DateTime date = LocalDate.now().toDateTimeAtStartOfDay();
167         	while (itr.hasNext()) {
168 				LeaveBlockBo lb = (LeaveBlockBo) itr.next();
169 				
170 				Long workArea = lb.getWorkArea();
171 				
172 				JobContract job = HrServiceLocator.getJobService().getJob(lb.getPrincipalId(), lb.getJobNumber(), LocalDate.fromDateFields(lb.getLeaveDate()), false);
173 				String department = job != null ? job.getDept() : null;
174 				String groupKeyCode = job != null ? job.getGroupKeyCode() : null;
175 				Department departmentObj = HrServiceLocator.getDepartmentService().getDepartment(department, groupKeyCode, LocalDate.fromDateFields(lb.getLeaveDate()));
176 				String location = departmentObj != null ? departmentObj.getGroupKey().getLocationId() : null;
177 				
178 				boolean valid = false;
179 				if (HrServiceLocator.getKPMEGroupService().isMemberOfSystemAdministratorGroup(HrContext.getPrincipalId(), date)
180 						|| HrServiceLocator.getKPMEGroupService().isMemberOfSystemViewOnlyGroup(HrContext.getPrincipalId(), date)
181 						|| HrServiceLocator.getKPMERoleService().principalHasRoleInWorkArea(HrContext.getPrincipalId(), KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), workArea, date)
182 						|| HrServiceLocator.getKPMERoleService().principalHasRoleInDepartment(HrContext.getPrincipalId(), KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_DEPARTMENT_ADMINISTRATOR.getRoleName(), department, groupKeyCode, date)
183 						|| HrServiceLocator.getKPMERoleService().principalHasRoleInDepartment(HrContext.getPrincipalId(), KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_DEPARTMENT_ADMINISTRATOR.getRoleName(), department, groupKeyCode, date)
184 						|| HrServiceLocator.getKPMERoleService().principalHasRoleInLocation(HrContext.getPrincipalId(), KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_LOCATION_ADMINISTRATOR.getRoleName(), location, date)
185 						|| HrServiceLocator.getKPMERoleService().principalHasRoleInLocation(HrContext.getPrincipalId(), KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_LOCATION_ADMINISTRATOR.getRoleName(), location, date)
186 						|| HrServiceLocator.getKPMERoleService().principalHasRoleInLocation(HrContext.getPrincipalId(), KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_LOCATION_VIEW_ONLY.getRoleName(), location, date)
187 						|| HrServiceLocator.getKPMERoleService().principalHasRoleInDepartment(HrContext.getPrincipalId(), KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_DEPARTMENT_VIEW_ONLY.getRoleName(), department, groupKeyCode, date)
188 						|| HrServiceLocator.getKPMERoleService().principalHasRoleInDepartment(HrContext.getPrincipalId(), KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_DEPARTMENT_VIEW_ONLY.getRoleName(), department, groupKeyCode, date)
189 						|| HrServiceLocator.getKPMERoleService().principalHasRoleInLocation(HrContext.getPrincipalId(), KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_LOCATION_VIEW_ONLY.getRoleName(), location, date)) {
190 					valid = true;
191 				}
192 				
193 				if (!valid) {
194 					itr.remove();
195 					continue;
196 				} else {
197 					// check for affects pay
198 					if(affectPay != null && !affectPay.isEmpty()) {
199 						EarnCodeContract earnCodeObj = HrServiceLocator.getEarnCodeService().getEarnCode(lb.getEarnCode(), lb.getLeaveLocalDate());
200 						if(!(earnCodeObj != null && affectPay.equalsIgnoreCase(earnCodeObj.getAffectPay()))) {
201 							itr.remove();
202 							continue;
203 						} 
204 					}
205 				}
206         	}
207 						
208         }
209         return objectList;
210 	}
211 		 
212 	 
213 
214 	@Override
215 	protected String getMaintenanceActionUrl(LookupForm lookupForm, Object dataObject,
216 			String methodToCall, List<String> pkNames) {
217 		String actionUrlHref = super.getMaintenanceActionUrl(lookupForm, dataObject, methodToCall, pkNames);
218 		LeaveBlockBo tb = null;
219 		String concreteBlockId = null;
220 		if(dataObject instanceof LeaveBlockBo) {
221 			tb = (LeaveBlockBo) dataObject;
222 			concreteBlockId = tb.getLmLeaveBlockId();
223 		}
224 		if(concreteBlockId == null) {
225 			return null;
226 		}
227 
228 		return actionUrlHref;
229 	}
230 
231 
232     @Override
233     public boolean allowsMaintenanceNewOrCopyAction() {
234         return false;
235     }
236 
237     @Override
238     public boolean allowsMaintenanceEditAction(Object dataObject) {
239         return false;
240     }
241 
242     @Override
243     public boolean allowsMaintenanceDeleteAction(Object dataObject) {
244         return false;
245     }
246 }