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.clocklog.service;
17  
18  import java.util.ArrayList;
19  import java.util.Date;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.kuali.hr.job.Job;
26  import org.kuali.hr.time.clocklog.ClockLog;
27  import org.kuali.hr.time.department.Department;
28  import org.kuali.hr.time.missedpunch.MissedPunchDocument;
29  import org.kuali.hr.time.roles.TkRole;
30  import org.kuali.hr.time.service.base.TkServiceLocator;
31  import org.kuali.hr.time.util.TKContext;
32  import org.kuali.hr.time.util.TKUtils;
33  import org.kuali.hr.time.util.TkConstants;
34  import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
35  import org.kuali.hr.time.workflow.service.TimesheetDocumentHeaderService;
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 ClockLogLookupableHelper extends KualiLookupableHelperServiceImpl {
41  	/**
42  	 * 
43  	 */
44  	private static final long serialVersionUID = 1L;
45  
46  	@Override
47  	public List<HtmlData> getCustomActionUrls(BusinessObject businessObject,
48  			List pkNames) {
49  		List<HtmlData> customActionUrls = super.getCustomActionUrls(
50  				businessObject, pkNames);
51  		List<HtmlData> overrideUrls = new ArrayList<HtmlData>();
52  		
53  		//Add missed punch to the clock log if it has one
54  		ClockLog clockLog = (ClockLog)businessObject;
55  		MissedPunchDocument mpDoc = TkServiceLocator.getMissedPunchService().getMissedPunchByClockLogId(clockLog.getTkClockLogId());
56  		if(mpDoc != null){
57  			clockLog.setMissedPunchDocumentId(mpDoc.getDocumentNumber());
58  		}
59  		
60  		for(HtmlData actionUrl : customActionUrls){
61  			if(!StringUtils.equals(actionUrl.getMethodToCall(), "copy")){
62  				overrideUrls.add(actionUrl);
63  			}
64  		}
65  		if (TKContext.getUser().isSystemAdmin() || TKContext.getUser().isGlobalViewOnly()) {
66  			clockLog = (ClockLog) businessObject;
67  			final String className = this.getBusinessObjectClass().getName();
68  			final String tkClockLogId = clockLog.getTkClockLogId();
69  			HtmlData htmlData = new HtmlData() {
70  
71  				@Override
72  				public String constructCompleteHtmlTag() {
73  					return "<a target=\"_blank\" href=\"inquiry.do?businessObjectClassName="
74  							+ className
75  							+ "&methodToCall=start&tkClockLogId="
76  							+ tkClockLogId + "\">view</a>";
77  				}
78  			};
79  			overrideUrls.add(htmlData);
80  		} else if (overrideUrls.size() != 0) {
81  			overrideUrls.remove(0);
82  		}
83  		return overrideUrls;
84  	}
85  
86  	@Override
87  	public List<? extends BusinessObject> getSearchResults(
88  			Map<String, String> fieldValues) {
89  		List<? extends BusinessObject> objectList = new ArrayList<BusinessObject>();
90  				
91  		
92  		TimesheetDocumentHeaderService timesheetDocumentHeaderService = TkServiceLocator.getTimesheetDocumentHeaderService();
93  		
94  		// search if documentid is given for search critria.
95  		String documentId =fieldValues.get("documentId");
96  		if(documentId != null && StringUtils.isNotEmpty(documentId)) {
97  			fieldValues.remove("documentId");
98  
99  			TimesheetDocumentHeader timesheetDocumentHeader = timesheetDocumentHeaderService.getDocumentHeader(documentId);
100 			if(timesheetDocumentHeader == null) {
101 				objectList = new ArrayList<BusinessObject>();
102 			} else {
103 				String timesheetUserId = timesheetDocumentHeader.getPrincipalId();
104 				Date beginDate =  timesheetDocumentHeader.getPayBeginDate();
105 				Date endDate =  timesheetDocumentHeader.getPayEndDate();
106 				objectList = super.getSearchResultsUnbounded(fieldValues);
107 				Iterator itr = objectList.iterator();
108 				while (itr.hasNext()) {
109 					ClockLog cl = (ClockLog) itr.next();
110 					cl.setDocumentId(timesheetUserId);
111 					if(cl.getPrincipalId().equalsIgnoreCase(timesheetUserId)) {
112 						if(new Date(cl.getClockTimestamp().getTime()).compareTo(beginDate) >= 0 && new Date(cl.getClockTimestamp().getTime()).compareTo(endDate) <= 0) {
113 							continue;
114 						} else {
115 							itr.remove();
116 						}
117 					} else {
118 						itr.remove();
119 					}
120 				}
121 			}
122 		} else {
123 			objectList = super.getSearchResults(fieldValues);
124 		}
125 		
126 		if (!objectList.isEmpty()) {
127 			Iterator itr = objectList.iterator();
128 			while (itr.hasNext()) {
129 				ClockLog cl = (ClockLog) itr.next();
130 				
131 				// Set Document Id 
132 				if(cl.getDocumentId() == null) {
133 					TimesheetDocumentHeader tsdh = timesheetDocumentHeaderService.getDocumentHeaderForDate(cl.getPrincipalId(), cl.getClockTimestamp());
134 					if(tsdh != null) {
135 						cl.setDocumentId(tsdh.getDocumentId());
136 					}
137 				}
138 				
139 				List<TkRole> tkRoles = TkServiceLocator.getTkRoleService()
140 						.getRoles(TKContext.getPrincipalId(),
141 								TKUtils.getCurrentDate());
142 				Job job = TkServiceLocator.getJobService().getJob(
143 						cl.getUserPrincipalId(), cl.getJobNumber(),
144 						TKUtils.getCurrentDate(), false);
145 				boolean valid = false;
146 				for (TkRole tkRole : tkRoles) {
147 					if (StringUtils.equals(tkRole.getRoleName(),
148 							TkConstants.ROLE_TK_SYS_ADMIN)
149 							|| (StringUtils.equals(tkRole.getRoleName(),
150 									TkConstants.ROLE_TK_APPROVER) && cl
151 									.getWorkArea().equals(tkRole.getWorkArea()))
152 							|| (StringUtils.equals(tkRole.getRoleName(),
153 									TkConstants.ROLE_TK_DEPT_ADMIN) && (job != null && (job
154 									.getDept().equals(tkRole.getDepartment()))))) {
155 						valid = true;
156 						break;
157 					}
158 					if(StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_LOCATION_ADMIN) && job != null && tkRole.getLocationObj()!=null){
159 						List<Department> departments = TkServiceLocator.getDepartmentService().getDepartmentByLocation(tkRole.getLocationObj().getLocation());
160 						for(Department department : departments){
161 							if(StringUtils.equals(job.getDept(), department.getDept())){
162 								valid = true;
163 								break;
164 							}
165 						}
166 						if(valid){
167 							break;
168 						}
169 					}
170 				}
171 				if (!valid) {
172 					itr.remove();
173 					continue;
174 				}
175 			}
176 		}
177 		return objectList;
178 	}
179 	
180 	
181 }