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