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.List;
020    import java.util.Map;
021    import java.util.Properties;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.kuali.hr.job.Job;
025    import org.kuali.hr.time.clocklog.ClockLog;
026    import org.kuali.hr.time.department.Department;
027    import org.kuali.hr.time.missedpunch.MissedPunchDocument;
028    import org.kuali.hr.time.roles.TkRole;
029    import org.kuali.hr.time.service.base.TkServiceLocator;
030    import org.kuali.hr.time.util.TKContext;
031    import org.kuali.hr.time.util.TKUser;
032    import org.kuali.hr.time.util.TKUtils;
033    import org.kuali.hr.time.util.TkConstants;
034    import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
035    import org.kuali.rice.kns.lookup.HtmlData;
036    import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
037    import org.kuali.rice.krad.bo.BusinessObject;
038    import org.kuali.rice.krad.util.GlobalVariables;
039    import org.kuali.rice.krad.util.KRADConstants;
040    import org.kuali.rice.krad.util.UrlFactory;
041    
042    @SuppressWarnings("deprecation")
043    public class ClockLogLookupableHelper extends KualiLookupableHelperServiceImpl {
044    
045            private static final long serialVersionUID = -469827905426221716L;
046    
047            @Override
048        @SuppressWarnings("rawtypes")
049        public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
050            List<HtmlData> customActionUrls = super.getCustomActionUrls(businessObject, pkNames);
051    
052                    ClockLog clockLog = (ClockLog)businessObject;
053                    String tkClockLogId = clockLog.getTkClockLogId();
054                    
055                    MissedPunchDocument missedPunchDocument = TkServiceLocator.getMissedPunchService().getMissedPunchByClockLogId(clockLog.getTkClockLogId());
056                    if (missedPunchDocument != null) {
057                            clockLog.setMissedPunchDocumentId(missedPunchDocument.getDocumentNumber());
058                    }
059                    
060                    List<HtmlData> overrideUrls = new ArrayList<HtmlData>();
061                    for (HtmlData actionUrl : customActionUrls) {
062                            if(!StringUtils.equals(actionUrl.getMethodToCall(), "copy")){
063                                    overrideUrls.add(actionUrl);
064                            }
065                    }
066                    
067                    if (TKUser.isSystemAdmin() || TKUser.isGlobalViewOnly()) {
068                    Properties params = new Properties();
069                    params.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, getBusinessObjectClass().getName());
070                    params.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.MAINTENANCE_NEW_METHOD_TO_CALL);
071                    params.put("tkClockLogId", tkClockLogId);
072                            HtmlData.AnchorHtmlData viewUrl = new HtmlData.AnchorHtmlData(UrlFactory.parameterizeUrl(KRADConstants.INQUIRY_ACTION, params), "view");
073                    viewUrl.setDisplayText("view");
074                            viewUrl.setTarget(HtmlData.AnchorHtmlData.TARGET_BLANK);
075                            overrideUrls.add(viewUrl);
076                    }
077                    
078                    return overrideUrls;
079            }
080    
081            @Override
082            public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
083                    List<ClockLog> results = new ArrayList<ClockLog>();
084    
085            List<? extends BusinessObject> searchResults = super.getSearchResults(fieldValues);
086    
087            for (BusinessObject searchResult : searchResults) {
088                ClockLog clockLog = (ClockLog) searchResult;
089                results.add(clockLog);
090            }
091                    
092            results = filterByPrincipalId(results, GlobalVariables.getUserSession().getPrincipalId());
093    
094                    return results;
095            }
096            
097        private List<ClockLog> filterByPrincipalId(List<ClockLog> clockLogs, String principalId) {
098            List<ClockLog> results = new ArrayList<ClockLog>();
099    
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    }