001    /**
002     * Copyright 2004-2012 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.Iterator;
020    import java.util.List;
021    import java.util.Map;
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.TKUtils;
032    import org.kuali.hr.time.util.TkConstants;
033    import org.kuali.rice.kns.lookup.HtmlData;
034    import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
035    import org.kuali.rice.krad.bo.BusinessObject;
036    
037    public class ClockLogLookupableHelper extends KualiLookupableHelperServiceImpl {
038            /**
039             * 
040             */
041            private static final long serialVersionUID = 1L;
042    
043            @Override
044            public List<HtmlData> getCustomActionUrls(BusinessObject businessObject,
045                            List pkNames) {
046                    List<HtmlData> customActionUrls = super.getCustomActionUrls(
047                                    businessObject, pkNames);
048                    List<HtmlData> overrideUrls = new ArrayList<HtmlData>();
049                    
050                    //Add missed punch to the clock log if it has one
051                    ClockLog clockLog = (ClockLog)businessObject;
052                    MissedPunchDocument mpDoc = TkServiceLocator.getMissedPunchService().getMissedPunchByClockLogId(clockLog.getTkClockLogId());
053                    if(mpDoc != null){
054                            clockLog.setMissedPunchDocumentId(mpDoc.getDocumentNumber());
055                    }
056                    
057                    for(HtmlData actionUrl : customActionUrls){
058                            if(!StringUtils.equals(actionUrl.getMethodToCall(), "copy")){
059                                    overrideUrls.add(actionUrl);
060                            }
061                    }
062                    if (TKContext.getUser().isSystemAdmin() || TKContext.getUser().isGlobalViewOnly()) {
063                            clockLog = (ClockLog) businessObject;
064                            final String className = this.getBusinessObjectClass().getName();
065                            final String tkClockLogId = clockLog.getTkClockLogId();
066                            HtmlData htmlData = new HtmlData() {
067    
068                                    @Override
069                                    public String constructCompleteHtmlTag() {
070                                            return "<a target=\"_blank\" href=\"inquiry.do?businessObjectClassName="
071                                                            + className
072                                                            + "&methodToCall=start&tkClockLogId="
073                                                            + tkClockLogId + "\">view</a>";
074                                    }
075                            };
076                            overrideUrls.add(htmlData);
077                    } else if (overrideUrls.size() != 0) {
078                            overrideUrls.remove(0);
079                    }
080                    return overrideUrls;
081            }
082    
083            @Override
084            public List<? extends BusinessObject> getSearchResults(
085                            Map<String, String> fieldValues) {
086                    List<? extends BusinessObject> objectList = super
087                                    .getSearchResults(fieldValues);
088    
089                    if (!objectList.isEmpty()) {
090                            Iterator itr = objectList.iterator();
091                            while (itr.hasNext()) {
092                                    ClockLog cl = (ClockLog) itr.next();
093                                    List<TkRole> tkRoles = TkServiceLocator.getTkRoleService()
094                                                    .getRoles(TKContext.getPrincipalId(),
095                                                                    TKUtils.getCurrentDate());
096                                    Job job = TkServiceLocator.getJobService().getJob(
097                                                    cl.getUserPrincipalId(), cl.getJobNumber(),
098                                                    TKUtils.getCurrentDate(), false);
099                                    boolean valid = false;
100                                    for (TkRole tkRole : tkRoles) {
101                                            if (StringUtils.equals(tkRole.getRoleName(),
102                                                            TkConstants.ROLE_TK_SYS_ADMIN)
103                                                            || (StringUtils.equals(tkRole.getRoleName(),
104                                                                            TkConstants.ROLE_TK_APPROVER) && cl
105                                                                            .getWorkArea().equals(tkRole.getWorkArea()))
106                                                            || (StringUtils.equals(tkRole.getRoleName(),
107                                                                            TkConstants.ROLE_TK_DEPT_ADMIN) && (job != null && (job
108                                                                            .getDept().equals(tkRole.getDepartment()))))) {
109                                                    valid = true;
110                                                    break;
111                                            }
112                                            if(StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_LOCATION_ADMIN) && job != null && tkRole.getLocationObj()!=null){
113                                                    List<Department> departments = TkServiceLocator.getDepartmentService().getDepartmentByLocation(tkRole.getLocationObj().getLocation());
114                                                    for(Department department : departments){
115                                                            if(StringUtils.equals(job.getDept(), department.getDept())){
116                                                                    valid = true;
117                                                                    break;
118                                                            }
119                                                    }
120                                                    if(valid){
121                                                            break;
122                                                    }
123                                            }
124                                    }
125                                    if (!valid) {
126                                            itr.remove();
127                                            continue;
128                                    }
129                            }
130                    }
131                    return objectList;
132            }
133            
134            
135    }