View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.timeblock.service;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.hr.job.Job;
20  import org.kuali.hr.time.department.Department;
21  import org.kuali.hr.time.roles.TkRole;
22  import org.kuali.hr.time.service.base.TkServiceLocator;
23  import org.kuali.hr.time.timeblock.TimeBlockHistory;
24  import org.kuali.hr.time.timeblock.TimeBlockHistoryDetail;
25  import org.kuali.hr.time.util.TKContext;
26  import org.kuali.hr.time.util.TKUtils;
27  import org.kuali.hr.time.util.TkConstants;
28  import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
29  import org.kuali.rice.krad.bo.BusinessObject;
30  
31  import java.util.ArrayList;
32  import java.util.Iterator;
33  import java.util.List;
34  import java.util.Map;
35  
36  public class TimeBlockHistoryLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
37  
38      /**
39       *
40       */
41      private static final long serialVersionUID = 1L;
42  
43      private static final String DOCUMENT_STATUS = "timesheetDocumentHeader.documentStatus";
44      private static final String BEGIN_DATE = "beginDate";
45      private static final String BEGIN_TIMESTAMP = "beginTimestamp";
46  
47      @Override
48      public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
49          List<TimeBlockHistory> results = new ArrayList<TimeBlockHistory>();
50  
51          if (fieldValues.containsKey(BEGIN_DATE)) {
52              //beginDate = fieldValues.get(BEGIN_DATE);
53              fieldValues.put(BEGIN_TIMESTAMP, fieldValues.get(BEGIN_DATE));
54              fieldValues.remove(BEGIN_DATE);
55          }
56  
57          List<? extends BusinessObject> searchResults = super.getSearchResults(fieldValues);
58  
59          for (BusinessObject searchResult : searchResults) {
60              TimeBlockHistory timeBlockHistory = (TimeBlockHistory) searchResult;
61              results.add(timeBlockHistory);
62          }
63  
64          results = filterWithSecurity(results);
65          results = addDetails(results);
66  
67          //Collections.sort(results, new Comparator<TimeBlockHistory>() {
68          //    @Override
69          //    public int compare(TimeBlockHistory timeBlockHistory1, TimeBlockHistory timeBlockHistory2) {
70          //        return timeBlockHistory1.getTkTimeBlockHistoryId().compareTo(timeBlockHistory2.getTkTimeBlockHistoryId());
71          //    }
72          //});
73  
74          return results;
75      }
76  
77      private List<TimeBlockHistory> filterWithSecurity(List<TimeBlockHistory> objectList) {
78          List<TimeBlockHistory> results = new ArrayList<TimeBlockHistory>();
79          results.addAll(objectList);
80          Iterator<? extends BusinessObject> itr = results.iterator();
81          List<TkRole> tkRoles = TkServiceLocator.getTkRoleService().getRoles(TKContext.getPrincipalId(), TKUtils.getCurrentDate());
82          while(itr.hasNext()){
83              TimeBlockHistory tbhd = (TimeBlockHistory)itr.next();
84              Job job = TkServiceLocator.getJobService().getJob(tbhd.getPrincipalId(), tbhd.getJobNumber(), tbhd.getEndTimestamp(), false);
85              boolean valid = false;
86              for (TkRole tkRole : tkRoles) {
87                  if (StringUtils.equals(tkRole.getRoleName(),
88                          TkConstants.ROLE_TK_SYS_ADMIN)
89                          || (StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_GLOBAL_VO))
90                          || (StringUtils.equals(tkRole.getRoleName(),
91                          TkConstants.ROLE_TK_APPROVER) && tbhd.getWorkArea().equals(tkRole.getWorkArea()))
92                          || (StringUtils.equals(tkRole.getRoleName(),
93                          TkConstants.ROLE_TK_DEPT_ADMIN) && (job != null && (job.getDept().equals(tkRole.getDepartment()))))) {
94                      valid = true;
95                      break;
96                  }
97                  if(StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_LOCATION_ADMIN) && job != null && tkRole.getLocationObj()!=null){
98                      List<Department> departments = TkServiceLocator.getDepartmentService().getDepartmentByLocation(tkRole.getLocationObj().getLocation());
99                      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 }