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.timeblock.service;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.hr.job.Job;
020    import org.kuali.hr.time.department.Department;
021    import org.kuali.hr.time.roles.TkRole;
022    import org.kuali.hr.time.service.base.TkServiceLocator;
023    import org.kuali.hr.time.timeblock.TimeBlockHistory;
024    import org.kuali.hr.time.timeblock.TimeBlockHistoryDetail;
025    import org.kuali.hr.time.util.TKContext;
026    import org.kuali.hr.time.util.TKUtils;
027    import org.kuali.hr.time.util.TkConstants;
028    import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
029    import org.kuali.rice.krad.bo.BusinessObject;
030    
031    import java.util.ArrayList;
032    import java.util.Iterator;
033    import java.util.List;
034    import java.util.Map;
035    
036    public class TimeBlockHistoryLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
037    
038        /**
039         *
040         */
041        private static final long serialVersionUID = 1L;
042    
043        private static final String DOCUMENT_STATUS = "timesheetDocumentHeader.documentStatus";
044        private static final String BEGIN_DATE = "beginDate";
045        private static final String BEGIN_TIMESTAMP = "beginTimestamp";
046    
047        @Override
048        public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
049            List<TimeBlockHistory> results = new ArrayList<TimeBlockHistory>();
050    
051            if (fieldValues.containsKey(BEGIN_DATE)) {
052                //beginDate = fieldValues.get(BEGIN_DATE);
053                fieldValues.put(BEGIN_TIMESTAMP, fieldValues.get(BEGIN_DATE));
054                fieldValues.remove(BEGIN_DATE);
055            }
056    
057            List<? extends BusinessObject> searchResults = super.getSearchResults(fieldValues);
058    
059            for (BusinessObject searchResult : searchResults) {
060                TimeBlockHistory timeBlockHistory = (TimeBlockHistory) searchResult;
061                results.add(timeBlockHistory);
062            }
063    
064            results = filterWithSecurity(results);
065            results = addDetails(results);
066    
067            //Collections.sort(results, new Comparator<TimeBlockHistory>() {
068            //    @Override
069            //    public int compare(TimeBlockHistory timeBlockHistory1, TimeBlockHistory timeBlockHistory2) {
070            //        return timeBlockHistory1.getTkTimeBlockHistoryId().compareTo(timeBlockHistory2.getTkTimeBlockHistoryId());
071            //    }
072            //});
073    
074            return results;
075        }
076    
077        private List<TimeBlockHistory> filterWithSecurity(List<TimeBlockHistory> objectList) {
078            List<TimeBlockHistory> results = new ArrayList<TimeBlockHistory>();
079            results.addAll(objectList);
080            Iterator<? extends BusinessObject> itr = results.iterator();
081            List<TkRole> tkRoles = TkServiceLocator.getTkRoleService().getRoles(TKContext.getPrincipalId(), TKUtils.getCurrentDate());
082            while(itr.hasNext()){
083                TimeBlockHistory tbhd = (TimeBlockHistory)itr.next();
084                Job job = TkServiceLocator.getJobService().getJob(tbhd.getPrincipalId(), tbhd.getJobNumber(), tbhd.getEndTimestamp(), false);
085                boolean valid = false;
086                for (TkRole tkRole : tkRoles) {
087                    if (StringUtils.equals(tkRole.getRoleName(),
088                            TkConstants.ROLE_TK_SYS_ADMIN)
089                            || (StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_GLOBAL_VO))
090                            || (StringUtils.equals(tkRole.getRoleName(),
091                            TkConstants.ROLE_TK_APPROVER) && tbhd.getWorkArea().equals(tkRole.getWorkArea()))
092                            || (StringUtils.equals(tkRole.getRoleName(),
093                            TkConstants.ROLE_TK_DEPT_ADMIN) && (job != null && (job.getDept().equals(tkRole.getDepartment()))))) {
094                        valid = true;
095                        break;
096                    }
097                    if(StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_LOCATION_ADMIN) && job != null && tkRole.getLocationObj()!=null){
098                        List<Department> departments = TkServiceLocator.getDepartmentService().getDepartmentByLocation(tkRole.getLocationObj().getLocation());
099                        for(Department department : departments){
100                            if(StringUtils.equals(job.getDept(), department.getDept())){
101                                valid = true;
102                                break;
103                            }
104                        }
105                        if(valid){
106                            break;
107                        }
108                    }
109                }
110                if (!valid) {
111                    itr.remove();
112                    continue;
113                }
114            }
115            return results;
116        }
117    
118        private List<TimeBlockHistory> addDetails(List<TimeBlockHistory> timeBlockHistories) {
119            List<TimeBlockHistory> results = new ArrayList<TimeBlockHistory>(timeBlockHistories);
120    
121            for (TimeBlockHistory timeBlockHistory : timeBlockHistories) {
122                List<TimeBlockHistoryDetail> timeBlockHistoryDetails = timeBlockHistory.getTimeBlockHistoryDetails();
123    
124                for (TimeBlockHistoryDetail timeBlockHistoryDetail : timeBlockHistoryDetails) {
125                    if (!timeBlockHistoryDetail.getEarnCode().equalsIgnoreCase(timeBlockHistory.getEarnCode())) {
126                        TimeBlockHistory newTimeBlockHistory = timeBlockHistory.copy();
127                        newTimeBlockHistory.setEarnCode(timeBlockHistoryDetail.getEarnCode());
128                        newTimeBlockHistory.setHours(timeBlockHistoryDetail.getHours());
129                        newTimeBlockHistory.setAmount(timeBlockHistoryDetail.getAmount());
130                        results.add(newTimeBlockHistory);
131                    }
132                }
133            }
134    
135            return results;
136        }
137    }