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.List;
20  import java.util.Map;
21  import java.util.Properties;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.hr.job.Job;
25  import org.kuali.hr.time.clocklog.ClockLog;
26  import org.kuali.hr.time.department.Department;
27  import org.kuali.hr.time.missedpunch.MissedPunchDocument;
28  import org.kuali.hr.time.roles.TkRole;
29  import org.kuali.hr.time.service.base.TkServiceLocator;
30  import org.kuali.hr.time.util.TKContext;
31  import org.kuali.hr.time.util.TKUser;
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.rice.kns.lookup.HtmlData;
36  import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
37  import org.kuali.rice.krad.bo.BusinessObject;
38  import org.kuali.rice.krad.util.GlobalVariables;
39  import org.kuali.rice.krad.util.KRADConstants;
40  import org.kuali.rice.krad.util.UrlFactory;
41  
42  @SuppressWarnings("deprecation")
43  public class ClockLogLookupableHelper extends KualiLookupableHelperServiceImpl {
44  
45  	private static final long serialVersionUID = -469827905426221716L;
46  
47  	@Override
48      @SuppressWarnings("rawtypes")
49      public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
50          List<HtmlData> customActionUrls = super.getCustomActionUrls(businessObject, pkNames);
51  
52  		ClockLog clockLog = (ClockLog)businessObject;
53  		String tkClockLogId = clockLog.getTkClockLogId();
54  		
55  		MissedPunchDocument missedPunchDocument = TkServiceLocator.getMissedPunchService().getMissedPunchByClockLogId(clockLog.getTkClockLogId());
56  		if (missedPunchDocument != null) {
57  			clockLog.setMissedPunchDocumentId(missedPunchDocument.getDocumentNumber());
58  		}
59  		
60  		List<HtmlData> overrideUrls = new ArrayList<HtmlData>();
61  		for (HtmlData actionUrl : customActionUrls) {
62  			if(!StringUtils.equals(actionUrl.getMethodToCall(), "copy")){
63  				overrideUrls.add(actionUrl);
64  			}
65  		}
66  		
67  		if (TKUser.isSystemAdmin() || TKUser.isGlobalViewOnly()) {
68  	        Properties params = new Properties();
69  	        params.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, getBusinessObjectClass().getName());
70  	        params.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.MAINTENANCE_NEW_METHOD_TO_CALL);
71  	        params.put("tkClockLogId", tkClockLogId);
72  			HtmlData.AnchorHtmlData viewUrl = new HtmlData.AnchorHtmlData(UrlFactory.parameterizeUrl(KRADConstants.INQUIRY_ACTION, params), "view");
73  	        viewUrl.setDisplayText("view");
74  			viewUrl.setTarget(HtmlData.AnchorHtmlData.TARGET_BLANK);
75  			overrideUrls.add(viewUrl);
76  		}
77  		
78  		return overrideUrls;
79  	}
80  
81  	@Override
82  	public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
83  		List<ClockLog> results = new ArrayList<ClockLog>();
84  
85          List<? extends BusinessObject> searchResults = super.getSearchResults(fieldValues);
86  
87          for (BusinessObject searchResult : searchResults) {
88              ClockLog clockLog = (ClockLog) searchResult;
89              results.add(clockLog);
90          }
91  		
92          results = filterByPrincipalId(results, GlobalVariables.getUserSession().getPrincipalId());
93  
94  		return results;
95  	}
96  	
97      private List<ClockLog> filterByPrincipalId(List<ClockLog> clockLogs, String principalId) {
98          List<ClockLog> results = new ArrayList<ClockLog>();
99  
100         for (ClockLog clockLog : clockLogs) {
101             if (clockLog.getDocumentId() == null) {
102                 TimesheetDocumentHeader tsdh = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeaderForDate(clockLog.getPrincipalId(), clockLog.getClockTimestamp());
103                 if (tsdh != null) {
104                     clockLog.setDocumentId(tsdh.getDocumentId());
105                 }
106             }
107 
108             Job jobObj = TkServiceLocator.getJobService().getJob(clockLog.getUserPrincipalId(), clockLog.getJobNumber(), TKUtils.getCurrentDate(), false);
109             String department = jobObj != null ? jobObj.getDept() : null;
110 
111             Department departmentObj = jobObj != null ? TkServiceLocator.getDepartmentService().getDepartment(department, jobObj.getEffectiveDate()) : null;
112             String location = departmentObj != null ? departmentObj.getLocation() : null;
113 
114             List<TkRole> tkRoles = TkServiceLocator.getTkRoleService().getRoles(TKContext.getPrincipalId(), TKUtils.getCurrentDate());
115             for (TkRole tkRole : tkRoles) {
116 	            if (StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_SYS_ADMIN) 
117 						|| (StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_APPROVER) && clockLog.getWorkArea().equals(tkRole.getWorkArea()))
118 						|| (StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_DEPT_ADMIN) && StringUtils.equals(department, tkRole.getDepartment()))) {
119 	            	results.add(clockLog);
120 					break;
121 				} else if (StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_LOCATION_ADMIN)) {
122 					List<Department> locationDepartments = TkServiceLocator.getDepartmentService().getDepartmentByLocation(location);
123 					for (Department locationDepartment : locationDepartments) {
124 						if (StringUtils.equals(department, locationDepartment.getDept())) {
125 							results.add(clockLog);
126 							break;
127 						}
128 					}
129 				}
130             }
131         }
132 
133         return results;
134     }
135     
136 }