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 java.text.DateFormat;
019    import java.text.ParseException;
020    import java.text.SimpleDateFormat;
021    import java.util.ArrayList;
022    import java.util.Date;
023    import java.util.Iterator;
024    import java.util.List;
025    
026    import org.apache.commons.lang.StringUtils;
027    import org.kuali.hr.job.Job;
028    import org.kuali.hr.time.department.Department;
029    import org.kuali.hr.time.roles.TkRole;
030    import org.kuali.hr.time.service.base.TkServiceLocator;
031    import org.kuali.hr.time.timeblock.TimeBlock;
032    import org.kuali.hr.time.timeblock.TimeHourDetail;
033    import org.kuali.hr.time.util.TKContext;
034    import org.kuali.hr.time.util.TKUtils;
035    import org.kuali.hr.time.util.TkConstants;
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 TimeBlockLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
041    
042            /**
043             * 
044             */
045            private static final long serialVersionUID = 1L;
046            
047            static final String DOC_ID = "documentId";
048            static final String DOC_STATUS_ID = "timesheetDocumentHeader.documentStatus";
049            static final String BEGIN_DATE_ID = "beginDate";
050                    
051             @Override
052        public List<? extends BusinessObject> getSearchResults(java.util.Map<String, String> fieldValues) {
053             
054                     String docStatus = "", beginDateString="";
055    
056                     if(fieldValues.containsKey(DOC_STATUS_ID)){
057                                    docStatus = fieldValues.get(DOC_STATUS_ID);
058                                    fieldValues.remove(DOC_STATUS_ID);
059                            }
060                     if(fieldValues.containsKey(BEGIN_DATE_ID)){
061                                    beginDateString = fieldValues.get(BEGIN_DATE_ID);
062                                    fieldValues.remove(BEGIN_DATE_ID);
063                            }
064            
065            List<TimeBlock> objectList = (List<TimeBlock>) super.getSearchResults(fieldValues);
066          
067            if(!objectList.isEmpty()) {
068                    Iterator<? extends BusinessObject> itr = objectList.iterator();
069                            while(itr.hasNext()){
070                                    TimeBlock tb = (TimeBlock)itr.next();
071                                    List<TkRole> tkRoles = TkServiceLocator.getTkRoleService().getRoles(TKContext.getPrincipalId(), TKUtils.getCurrentDate());
072                                    Job job = TkServiceLocator.getJobService().getJob(tb.getUserPrincipalId(), tb.getJobNumber(), TKUtils.getCurrentDate(), false);
073                                    boolean valid = false;
074                                    for (TkRole tkRole : tkRoles) {
075                                            if (StringUtils.equals(tkRole.getRoleName(),
076                                                            TkConstants.ROLE_TK_SYS_ADMIN)
077                                                            || (StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_GLOBAL_VO))
078                                                            || (StringUtils.equals(tkRole.getRoleName(),
079                                                                            TkConstants.ROLE_TK_APPROVER) && tb
080                                                                            .getWorkArea().equals(tkRole.getWorkArea()))
081                                                            || (StringUtils.equals(tkRole.getRoleName(),
082                                                                            TkConstants.ROLE_TK_DEPT_ADMIN) && (job != null && (job
083                                                                            .getDept().equals(tkRole.getDepartment()))))) {
084                                                    valid = true;
085                                                    break;
086                                            }
087                                            if(StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_LOCATION_ADMIN) && job != null && tkRole.getLocationObj()!=null){
088                                                    List<Department> departments = TkServiceLocator.getDepartmentService().getDepartmentByLocation(tkRole.getLocationObj().getLocation());
089                                                    for(Department department : departments){
090                                                            if(StringUtils.equals(job.getDept(), department.getDept())){
091                                                                    valid = true;
092                                                                    break;
093                                                            }
094                                                    }
095                                                    if(valid){
096                                                            break;
097                                                    }
098                                            }
099                                    }
100                                    if (!valid) {
101                                            itr.remove();
102                                            continue;
103                                    }
104                                    if(StringUtils.isNotEmpty(docStatus)) {
105                                            if(tb.getTimesheetDocumentHeader() == null) {
106                                                    itr.remove();
107                                                    continue;
108                                            } else {
109                                                    if(tb.getTimesheetDocumentHeader().getDocumentStatus() != null) {
110                                                            if(!tb.getTimesheetDocumentHeader().getDocumentStatus().equals(docStatus)){
111                                                                    itr.remove();
112                                                                    continue;
113                                                            }
114                                                    } else {
115                                                            itr.remove();
116                                                            continue;
117                                                    }
118                                            }
119                                    }
120                                                                    
121                                    if(StringUtils.isNotEmpty(beginDateString)) {
122                                            if(tb.getBeginDate() != null) {
123                                                    if(!this.checkDate(tb, tb.getBeginDate(), beginDateString)) {
124                                                            itr.remove();
125                                                            continue;
126                                                    } 
127                                            } else {
128                                                    itr.remove();
129                                                    continue;
130                                            }
131                                    }                               
132                            }
133                            
134                            // Fetch list from time hour detail and convert it into TimeBlock
135                            if(!objectList.isEmpty()) {
136                                    List<TimeBlock> timeBlocks = new ArrayList<TimeBlock>(objectList);
137                                    for(TimeBlock tb: timeBlocks) {
138                                            List<TimeHourDetail> timeHourDetails = tb.getTimeHourDetails();
139                                            for(TimeHourDetail thd : timeHourDetails) {
140                                              if(!thd.getEarnCode().equalsIgnoreCase(tb.getEarnCode())) {
141                                                      TimeBlock timeBlock = tb.copy();
142                                                      timeBlock.setEarnCode(thd.getEarnCode());
143                                                      timeBlock.setHours(thd.getHours());
144                                                      timeBlock.setAmount(thd.getAmount());
145                                                      objectList.add(timeBlock);
146                                              }
147                                            } // inner for ends
148                                    } // outer for ends
149                            } // if ends
150                            
151            }
152            
153         
154            return objectList;
155             }
156             
157             public boolean checkDate(TimeBlock tb, Date asOfDate, String dateString) {
158                     if(tb.getTimesheetDocumentHeader() == null) {
159                                    return false;
160                     }
161                    try {
162                            DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
163                            Date dateFrom;
164                            Date dateTo;
165                            String subDateString;
166                            if(dateString.indexOf("..") == 10) {
167                                    subDateString= dateString.substring(0, 10);
168                                    dateFrom = df.parse(subDateString);
169                                    subDateString= dateString.substring(12, dateString.length());
170                                    dateTo = df.parse(subDateString);
171                                    if(asOfDate != null) {
172                                            if(!( (asOfDate.after(dateFrom) || asOfDate.equals(dateFrom))
173                                                            && (asOfDate.before(dateTo) || asOfDate.equals(dateTo)))) {
174                                                    return false;
175                                            }
176                                    } else {
177                                            return false;
178                                    }
179                            } else{
180                                    subDateString= dateString.substring(2, dateString.length());
181                                    dateTo = df.parse(subDateString);
182                                    if(asOfDate != null) {
183                                            if( (dateString.startsWith(">=") && asOfDate.before(dateTo))
184                                                            || (dateString.startsWith("<=") && asOfDate.after(dateTo))) {
185                                                    return false;
186                                            }
187                                    } else {
188                                            return false;
189                                    }
190                            }
191                    } catch (ParseException e) {
192                    }
193              return true;
194             }
195             
196            @SuppressWarnings("unchecked")
197            @Override
198            public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
199                    List<HtmlData> customActionUrls = super.getCustomActionUrls(businessObject, pkNames);
200                    List<HtmlData> overrideUrls = new ArrayList<HtmlData>();
201                    for(HtmlData actionUrl : customActionUrls){
202                            if(!StringUtils.equals(actionUrl.getMethodToCall(), "copy")){
203                                    overrideUrls.add(actionUrl);
204                            }
205                    }
206                    return overrideUrls;
207            }
208    }