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.earncode.service;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.hr.earncodesec.EarnCodeSecurity;
020    import org.kuali.hr.earncodesec.EarnCodeType;
021    import org.kuali.hr.job.Job;
022    import org.kuali.hr.time.assignment.Assignment;
023    import org.kuali.hr.time.earncode.EarnCode;
024    import org.kuali.hr.time.earncode.dao.EarnCodeDao;
025    import org.kuali.hr.time.roles.TkUserRoles;
026    import org.kuali.hr.time.service.base.TkServiceLocator;
027    import org.kuali.hr.time.util.TKContext;
028    import org.kuali.hr.time.util.TKUser;
029    import org.kuali.hr.time.workarea.WorkArea;
030    import org.kuali.rice.krad.util.GlobalVariables;
031    
032    import java.sql.Date;
033    import java.util.*;
034    
035    public class EarnCodeServiceImpl implements EarnCodeService {
036    
037            private EarnCodeDao earnCodeDao;
038    
039            public void setEarnCodeDao(EarnCodeDao earnCodeDao) {
040                    this.earnCodeDao = earnCodeDao;
041            }
042    
043    
044        @Override
045        public List<EarnCode> getEarnCodes(Assignment a, Date asOfDate) {
046            return getEarnCodesForTime(a, asOfDate);
047        }
048    
049        @Override
050        public List<EarnCode> getEarnCodes(Assignment a, Date asOfDate, String earnTypeCode) {
051            List<EarnCode> earnCodes = new LinkedList<EarnCode>();
052    
053            // Note: https://jira.kuali.org/browse/KPME-689
054            // We are grabbing a TkUser from the current thread local context here.
055            // really, this should probably be passed in..
056    
057            TKUser user = TKContext.getUser();
058            if (user == null) {
059                // TODO: Determine how to fail if there is no TkUser
060                throw new RuntimeException("No User on context.");
061            }
062    
063            if (a == null)
064                throw new RuntimeException("Can not get earn codes for null assignment");
065            Job job = a.getJob();
066            if (job == null || job.getPayTypeObj() == null)
067                throw new RuntimeException("Null job/job paytype on assignment!");
068    
069            EarnCode regularEc = getEarnCode(job.getPayTypeObj().getRegEarnCode(), asOfDate);
070            if (regularEc == null)
071                throw new RuntimeException("No regular earn code defined.");
072            earnCodes.add(regularEc);
073            List<EarnCodeSecurity> decs = TkServiceLocator.getEarnCodeSecurityService().getEarnCodeSecurities(job.getDept(), job.getHrSalGroup(), job.getLocation(), asOfDate);
074            for (EarnCodeSecurity dec : decs) {
075                if (StringUtils.isBlank(earnTypeCode)
076                        || earnTypeCode.equals(dec.getEarnCodeType())
077                        ) {
078    
079                    boolean addEarnCode = false;
080                    // Check employee flag
081                    if (dec.isEmployee() &&
082                            (StringUtils.equals(TKUser.getCurrentTargetPerson().getEmployeeId(), GlobalVariables.getUserSession().getPerson().getEmployeeId()))) {
083                        addEarnCode = true;
084                    }
085                    // Check approver flag
086                    if (!addEarnCode && dec.isApprover()) {
087                        Set<Long> workAreas = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).getApproverWorkAreas();
088                        for (Long wa : workAreas) {
089                            WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(wa, asOfDate);
090                            if (workArea!= null && a.getWorkArea().compareTo(workArea.getWorkArea())==0) {
091                                addEarnCode = true;
092                                break;
093                            }
094                        }
095                    }
096                    if (addEarnCode) {
097                        EarnCode ec = getEarnCode(dec.getEarnCode(), asOfDate);
098                        if(ec!=null){
099                            earnCodes.add(ec);
100                        }
101                    }
102                }
103            }
104    
105            return earnCodes;
106        }
107    
108        public EarnCode getEarnCode(String earnCode, Date asOfDate) {
109            EarnCode ec = null;
110    
111            ec = earnCodeDao.getEarnCode(earnCode, asOfDate);
112    
113            return ec;
114        }
115    
116        @Override
117        public String getEarnCodeType(String earnCode, Date asOfDate) {
118            EarnCode earnCodeObj = getEarnCode(earnCode, asOfDate);
119            return earnCodeObj.getEarnCodeType();
120        }
121    
122            @Override
123            public EarnCode getEarnCodeById(String earnCodeId) {
124                    return earnCodeDao.getEarnCodeById(earnCodeId);
125            }
126            
127            public List<EarnCode> getOvertimeEarnCodes(Date asOfDate){
128                    return earnCodeDao.getOvertimeEarnCodes(asOfDate);
129            }
130            
131            public List<String> getOvertimeEarnCodesStrs(Date asOfDate){
132                    List<String> ovtEarnCodeStrs = new ArrayList<String>();
133                    List<EarnCode> ovtEarnCodes = getOvertimeEarnCodes(asOfDate);
134                    if(ovtEarnCodes != null){
135                            for(EarnCode ovtEc : ovtEarnCodes){
136                                    ovtEarnCodeStrs.add(ovtEc.getEarnCode());
137                            }
138                    }
139                    return ovtEarnCodeStrs;
140            }
141            
142            @Override
143            public int getEarnCodeCount(String earnCode) {
144                    return earnCodeDao.getEarnCodeCount(earnCode);
145            }
146            
147            @Override
148            public int getNewerEarnCodeCount(String earnCode, Date effdt) {
149                    return earnCodeDao.getNewerEarnCodeCount(earnCode, effdt);
150            }
151    
152        @Override
153        public List<EarnCode> getEarnCodes(String earnCode, String ovtEarnCode, String descr, Date fromEffdt, Date toEffdt, String active, String showHist) {
154            return earnCodeDao.getEarnCodes(earnCode, ovtEarnCode, descr, fromEffdt, toEffdt, active, showHist);
155        }
156    
157    
158        public List<EarnCode> getEarnCodesForTime(Assignment a, Date asOfDate) {
159            // code moved over from earncodeservice in kpme 1.5
160            if (a == null) throw new RuntimeException("No assignment parameter.");
161            Job job = a.getJob();
162            if (job == null || job.getPayTypeObj() == null) throw new RuntimeException("Null job or null job pay type on assignment.");
163    
164            List<EarnCode> earnCodes = new LinkedList<EarnCode>();
165            String earnTypeCode = EarnCodeType.TIME.getCode();
166    
167            boolean isClockUser = a.getTimeCollectionRule().isClockUserFl();
168            boolean isUsersTimesheet = StringUtils.equals(TKContext.getPrincipalId(),a.getPrincipalId());
169    
170            // Reg earn codes will typically not be defined in the earn code security table
171            EarnCode regularEarnCode = getEarnCode(job.getPayTypeObj().getRegEarnCode(), asOfDate);
172            if (regularEarnCode == null) {
173                throw new RuntimeException("No regular earn code defined for job pay type.");
174            } else {
175                //  if you are a clock user and this is your timesheet and you are processing the reg earn code, do not add this earn code. Use the clock in/out mechanism.
176                if (isClockUser && isUsersTimesheet) {
177                    // do not add reg earn code. use clock.
178                } else {
179                    earnCodes.add(regularEarnCode);
180                }
181            }
182            List<EarnCodeSecurity> decs = TkServiceLocator.getEarnCodeSecurityService().getEarnCodeSecurities(job.getDept(), job.getHrSalGroup(), job.getLocation(), asOfDate);
183            for (EarnCodeSecurity dec : decs) {
184                if (earnTypeCode.equals(dec.getEarnCodeType())
185                        || EarnCodeType.BOTH.getCode().equals(dec.getEarnCodeType())) {
186    
187                    boolean addEarnCode = addEarnCodeBasedOnEmployeeApproverSettings(dec, a, asOfDate);
188                    //  this earn code could possibly be for reg earnings. reg was processed before the earn code security table of earn codes, so it should be skipped here.
189                    if ( StringUtils.equals(job.getPayTypeObj().getRegEarnCode(),dec.getEarnCode()) ){
190                        addEarnCode = false;
191                    }
192                    if (!dec.isEmployee()){
193                        addEarnCode = false;
194                    }
195                    if (addEarnCode) {
196                        EarnCode ec = getEarnCode(dec.getEarnCode(), asOfDate);
197                        if(ec!=null){
198                            earnCodes.add(ec);
199                        }
200                    }
201                }
202            }
203    
204            return earnCodes;
205            //        if (a == null) throw new RuntimeException("No assignment parameter.");
206    //        Job job = a.getJob();
207    //        if (job == null || job.getPayTypeObj() == null) throw new RuntimeException("Null job or null job pay type on assignment.");
208    //
209    //        List<EarnCode> earnCodes = new LinkedList<EarnCode>();
210    //
211    //        EarnCode regularEarnCode = getEarnCode(job.getPayTypeObj().getRegEarnCode(), asOfDate);
212    //        if (regularEarnCode == null) {
213    //            throw new RuntimeException("No regular earn code defined for job pay type.");
214    //        } else {
215    //            earnCodes.add(regularEarnCode);
216    //        }
217    //        List<EarnCodeSecurity> decs = TkServiceLocator.getEarnCodeSecurityService().getEarnCodeSecurities(job.getDept(), job.getHrSalGroup(), job.getLocation(), asOfDate);
218    //        for (EarnCodeSecurity dec : decs) {
219    //                boolean addEarnCode = false;
220    //                // Check employee flag
221    //                if (dec.isEmployee() &&
222    //                        (StringUtils.equals(TKUser.getCurrentTargetPerson().getEmployeeId(), GlobalVariables.getUserSession().getPerson().getEmployeeId()))) {
223    //                    addEarnCode = true;
224    //                }
225    //                // Check approver flag
226    //                if (!addEarnCode && dec.isApprover()) {
227    //                    Set<Long> workAreas = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).getApproverWorkAreas();
228    //                    for (Long wa : workAreas) {
229    //                        WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(wa, asOfDate);
230    //                        if (workArea!= null && a.getWorkArea().compareTo(workArea.getWorkArea())==0) {
231    //                            addEarnCode = true;
232    //                            break;
233    //                        }
234    //                    }
235    //                }
236    //                if (addEarnCode) {
237    //                    EarnCode ec = getEarnCode(dec.getEarnCode(), asOfDate);
238    //                    if(ec!=null){
239    //                        earnCodes.add(ec);
240    //                    }
241    //                }
242    //        }
243            // return earnCodes;
244        }
245    
246        private boolean addEarnCodeBasedOnEmployeeApproverSettings(EarnCodeSecurity security, Assignment a, Date asOfDate) {
247            boolean addEarnCode = false;
248            if (security.isEmployee() &&
249                    (StringUtils.equals(TKUser.getCurrentTargetPerson().getEmployeeId(), GlobalVariables.getUserSession().getPerson().getEmployeeId()))) {
250                addEarnCode = true;
251            }
252            // Check approver flag
253            if (!addEarnCode && security.isApprover()) {
254                Set<Long> workAreas = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).getApproverWorkAreas();
255                for (Long wa : workAreas) {
256                    WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(wa, asOfDate);
257                    if (workArea!= null && a.getWorkArea().compareTo(workArea.getWorkArea())==0) {
258                        addEarnCode = true;
259                        break;
260                    }
261                }
262            }
263            return addEarnCode;
264        }
265    
266        /* not using yet, may not be needed
267        @Override
268        public Map<String, String> getEarnCod e s ForDisplayWithAssignment(Assignment assignment, Date asOfDate) {
269            List<EarnCode> earnCodes = this.getEarnCod e s ( assignment, asOfDate);
270    
271            Date currentDate = TKUtils.getCurrentDate();
272            boolean futureDate = asOfDate.after(currentDate);
273            List<EarnCode> copyList = new ArrayList<EarnCode>();
274            copyList.addAll(earnCodes);
275            for (EarnCode earnCode : copyList) {
276                if ( futureDate
277                        && !earnCode.getAllowScheduledLeave().equalsIgnoreCase("Y")) {
278                    earnCodes.remove(earnCode);
279                }
280            }
281            Comparator<EarnCode> earnCodeComparator = new Comparator<EarnCode>() {
282                @Override
283                public int compare(EarnCode ec1, EarnCode ec2) {
284                    return ec1.getEarnCode().compareToIgnoreCase(ec2.getEarnCode());
285                }
286            };
287            // Order by leaveCode ascending
288            Ordering<EarnCode> ordering = Ordering.from(earnCodeComparator);
289    
290            Map<String, String> earnCodesForDisplay = new LinkedHashMap<String, String>();
291            for (EarnCode earnCode : ordering.sortedCopy(earnCodes)) {
292                earnCodesForDisplay.put(earnCode.getEarnCodeKeyForDisplay(), earnCode.getEarnCodeValueForDisplay());
293            }
294            return earnCodesForDisplay;
295        }
296    
297        */
298    }