001 /**
002 * Copyright 2004-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.hr.time.clocklog.service;
017
018 import java.util.ArrayList;
019 import java.util.Date;
020 import java.util.Iterator;
021 import java.util.List;
022 import java.util.Map;
023
024 import org.apache.commons.lang.StringUtils;
025 import org.kuali.hr.job.Job;
026 import org.kuali.hr.time.clocklog.ClockLog;
027 import org.kuali.hr.time.department.Department;
028 import org.kuali.hr.time.missedpunch.MissedPunchDocument;
029 import org.kuali.hr.time.roles.TkRole;
030 import org.kuali.hr.time.service.base.TkServiceLocator;
031 import org.kuali.hr.time.util.TKContext;
032 import org.kuali.hr.time.util.TKUser;
033 import org.kuali.hr.time.util.TKUtils;
034 import org.kuali.hr.time.util.TkConstants;
035 import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
036 import org.kuali.hr.time.workflow.service.TimesheetDocumentHeaderService;
037 import org.kuali.rice.kns.lookup.HtmlData;
038 import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
039 import org.kuali.rice.krad.bo.BusinessObject;
040
041 public class ClockLogLookupableHelper extends KualiLookupableHelperServiceImpl {
042 /**
043 *
044 */
045 private static final long serialVersionUID = 1L;
046
047 @Override
048 public List<HtmlData> getCustomActionUrls(BusinessObject businessObject,
049 List pkNames) {
050 List<HtmlData> customActionUrls = super.getCustomActionUrls(
051 businessObject, pkNames);
052 List<HtmlData> overrideUrls = new ArrayList<HtmlData>();
053
054 //Add missed punch to the clock log if it has one
055 ClockLog clockLog = (ClockLog)businessObject;
056 MissedPunchDocument mpDoc = TkServiceLocator.getMissedPunchService().getMissedPunchByClockLogId(clockLog.getTkClockLogId());
057 if(mpDoc != null){
058 clockLog.setMissedPunchDocumentId(mpDoc.getDocumentNumber());
059 }
060
061 for(HtmlData actionUrl : customActionUrls){
062 if(!StringUtils.equals(actionUrl.getMethodToCall(), "copy")){
063 overrideUrls.add(actionUrl);
064 }
065 }
066 if (TKUser.isSystemAdmin() || TKUser.isGlobalViewOnly()) {
067 clockLog = (ClockLog) businessObject;
068 final String className = this.getBusinessObjectClass().getName();
069 final String tkClockLogId = clockLog.getTkClockLogId();
070 HtmlData htmlData = new HtmlData() {
071
072 @Override
073 public String constructCompleteHtmlTag() {
074 return "<a target=\"_blank\" href=\"inquiry.do?businessObjectClassName="
075 + className
076 + "&methodToCall=start&tkClockLogId="
077 + tkClockLogId + "\">view</a>";
078 }
079 };
080 overrideUrls.add(htmlData);
081 } else if (overrideUrls.size() != 0) {
082 overrideUrls.remove(0);
083 }
084 return overrideUrls;
085 }
086
087 @Override
088 public List<? extends BusinessObject> getSearchResults(
089 Map<String, String> fieldValues) {
090 List<? extends BusinessObject> objectList = new ArrayList<BusinessObject>();
091
092
093 TimesheetDocumentHeaderService timesheetDocumentHeaderService = TkServiceLocator.getTimesheetDocumentHeaderService();
094
095 // search if documentid is given for search critria.
096 String documentId =fieldValues.get("documentId");
097 if(documentId != null && StringUtils.isNotEmpty(documentId)) {
098 fieldValues.remove("documentId");
099
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 }