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.TKUtils;
033    import org.kuali.hr.time.util.TkConstants;
034    import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
035    import org.kuali.hr.time.workflow.service.TimesheetDocumentHeaderService;
036    import org.kuali.rice.kns.lookup.HtmlData;
037    import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
038    import org.kuali.rice.krad.bo.BusinessObject;
039    
040    public class ClockLogLookupableHelper extends KualiLookupableHelperServiceImpl {
041            /**
042             * 
043             */
044            private static final long serialVersionUID = 1L;
045    
046            @Override
047            public List<HtmlData> getCustomActionUrls(BusinessObject businessObject,
048                            List pkNames) {
049                    List<HtmlData> customActionUrls = super.getCustomActionUrls(
050                                    businessObject, pkNames);
051                    List<HtmlData> overrideUrls = new ArrayList<HtmlData>();
052                    
053                    //Add missed punch to the clock log if it has one
054                    ClockLog clockLog = (ClockLog)businessObject;
055                    MissedPunchDocument mpDoc = TkServiceLocator.getMissedPunchService().getMissedPunchByClockLogId(clockLog.getTkClockLogId());
056                    if(mpDoc != null){
057                            clockLog.setMissedPunchDocumentId(mpDoc.getDocumentNumber());
058                    }
059                    
060                    for(HtmlData actionUrl : customActionUrls){
061                            if(!StringUtils.equals(actionUrl.getMethodToCall(), "copy")){
062                                    overrideUrls.add(actionUrl);
063                            }
064                    }
065                    if (TKContext.getUser().isSystemAdmin() || TKContext.getUser().isGlobalViewOnly()) {
066                            clockLog = (ClockLog) businessObject;
067                            final String className = this.getBusinessObjectClass().getName();
068                            final String tkClockLogId = clockLog.getTkClockLogId();
069                            HtmlData htmlData = new HtmlData() {
070    
071                                    @Override
072                                    public String constructCompleteHtmlTag() {
073                                            return "<a target=\"_blank\" href=\"inquiry.do?businessObjectClassName="
074                                                            + className
075                                                            + "&methodToCall=start&tkClockLogId="
076                                                            + tkClockLogId + "\">view</a>";
077                                    }
078                            };
079                            overrideUrls.add(htmlData);
080                    } else if (overrideUrls.size() != 0) {
081                            overrideUrls.remove(0);
082                    }
083                    return overrideUrls;
084            }
085    
086            @Override
087            public List<? extends BusinessObject> getSearchResults(
088                            Map<String, String> fieldValues) {
089                    List<? extends BusinessObject> objectList = new ArrayList<BusinessObject>();
090                                    
091                    
092                    TimesheetDocumentHeaderService timesheetDocumentHeaderService = TkServiceLocator.getTimesheetDocumentHeaderService();
093                    
094                    // search if documentid is given for search critria.
095                    String documentId =fieldValues.get("documentId");
096                    if(documentId != null && StringUtils.isNotEmpty(documentId)) {
097                            fieldValues.remove("documentId");
098    
099                            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.getBeginDate();
105                                    Date endDate =  timesheetDocumentHeader.getEndDate();
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    }