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.earncode.service;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.hr.earncodesec.EarnCodeSecurity;
020    import org.kuali.hr.job.Job;
021    import org.kuali.hr.time.assignment.Assignment;
022    import org.kuali.hr.time.earncode.EarnCode;
023    import org.kuali.hr.time.earncode.dao.EarnCodeDao;
024    import org.kuali.hr.time.roles.TkUserRoles;
025    import org.kuali.hr.time.service.base.TkServiceLocator;
026    import org.kuali.hr.time.util.TKContext;
027    import org.kuali.hr.time.util.TKUser;
028    import org.kuali.hr.time.workarea.WorkArea;
029    import org.kuali.rice.krad.util.GlobalVariables;
030    
031    import java.sql.Date;
032    import java.util.*;
033    
034    public class EarnCodeServiceImpl implements EarnCodeService {
035    
036            private EarnCodeDao earnCodeDao;
037    
038            public void setEarnCodeDao(EarnCodeDao earnCodeDao) {
039                    this.earnCodeDao = earnCodeDao;
040            }
041    
042    
043        @Override
044        public List<EarnCode> getEarnCodes(Assignment a, Date asOfDate) {
045            return getEarnCodes(a, asOfDate, null);
046        }
047    
048        @Override
049        public List<EarnCode> getEarnCodes(Assignment a, Date asOfDate, String earnTypeCode) {
050            List<EarnCode> earnCodes = new LinkedList<EarnCode>();
051    
052            // Note: https://jira.kuali.org/browse/KPME-689
053            // We are grabbing a TkUser from the current thread local context here.
054            // really, this should probably be passed in..
055    
056            TKUser user = TKContext.getUser();
057            if (user == null) {
058                // TODO: Determine how to fail if there is no TkUser
059                throw new RuntimeException("No User on context.");
060            }
061    
062            if (a == null)
063                throw new RuntimeException("Can not get earn codes for null assignment");
064            Job job = a.getJob();
065            if (job == null || job.getPayTypeObj() == null)
066                throw new RuntimeException("Null job/job paytype on assignment!");
067    
068            EarnCode regularEc = getEarnCode(job.getPayTypeObj().getRegEarnCode(), asOfDate);
069            if (regularEc == null)
070                throw new RuntimeException("No regular earn code defined.");
071            earnCodes.add(regularEc);
072            List<EarnCodeSecurity> decs = TkServiceLocator.getEarnCodeSecurityService().getEarnCodeSecurities(job.getDept(), job.getHrSalGroup(), job.getLocation(), asOfDate);
073            for (EarnCodeSecurity dec : decs) {
074                if (StringUtils.isBlank(earnTypeCode)
075                        || earnTypeCode.equals(dec.getEarnCodeType())
076                        ) {
077    
078                    boolean addEarnCode = false;
079                    // Check employee flag
080                    if (dec.isEmployee() &&
081                            (StringUtils.equals(TKUser.getCurrentTargetPerson().getEmployeeId(), GlobalVariables.getUserSession().getPerson().getEmployeeId()))) {
082                        addEarnCode = true;
083                    }
084                    // Check approver flag
085                    if (!addEarnCode && dec.isApprover()) {
086                        Set<Long> workAreas = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).getApproverWorkAreas();
087                        for (Long wa : workAreas) {
088                            WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(wa, asOfDate);
089                            if (workArea!= null && a.getWorkArea().compareTo(workArea.getWorkArea())==0) {
090                                addEarnCode = true;
091                                break;
092                            }
093                        }
094                    }
095                    if (addEarnCode) {
096                        EarnCode ec = getEarnCode(dec.getEarnCode(), asOfDate);
097                        if(ec!=null){
098                            earnCodes.add(ec);
099                        }
100                    }
101                }
102            }
103    
104            return earnCodes;
105        }
106    
107        public EarnCode getEarnCode(String earnCode, Date asOfDate) {
108            EarnCode ec = null;
109    
110            ec = earnCodeDao.getEarnCode(earnCode, asOfDate);
111    
112            return ec;
113        }
114    
115        @Override
116        public String getEarnCodeType(String earnCode, Date asOfDate) {
117            EarnCode earnCodeObj = getEarnCode(earnCode, asOfDate);
118            return earnCodeObj.getEarnCodeType();
119        }
120    
121            @Override
122            public EarnCode getEarnCodeById(String earnCodeId) {
123                    return earnCodeDao.getEarnCodeById(earnCodeId);
124            }
125            
126            public List<EarnCode> getOvertimeEarnCodes(Date asOfDate){
127                    return earnCodeDao.getOvertimeEarnCodes(asOfDate);
128            }
129            
130            public List<String> getOvertimeEarnCodesStrs(Date asOfDate){
131                    List<String> ovtEarnCodeStrs = new ArrayList<String>();
132                    List<EarnCode> ovtEarnCodes = getOvertimeEarnCodes(asOfDate);
133                    if(ovtEarnCodes != null){
134                            for(EarnCode ovtEc : ovtEarnCodes){
135                                    ovtEarnCodeStrs.add(ovtEc.getEarnCode());
136                            }
137                    }
138                    return ovtEarnCodeStrs;
139            }
140            
141            @Override
142            public int getEarnCodeCount(String earnCode) {
143                    return earnCodeDao.getEarnCodeCount(earnCode);
144            }
145            
146            @Override
147            public int getNewerEarnCodeCount(String earnCode, Date effdt) {
148                    return earnCodeDao.getNewerEarnCodeCount(earnCode, effdt);
149            }
150    
151    
152        public List<EarnCode> getEarnCodesForTime(Assignment a, Date asOfDate) {
153            if (a == null) throw new RuntimeException("No assignment parameter.");
154            Job job = a.getJob();
155            if (job == null || job.getPayTypeObj() == null) throw new RuntimeException("Null job or null job pay type on assignment.");
156    
157            List<EarnCode> earnCodes = new LinkedList<EarnCode>();
158    
159            EarnCode regularEarnCode = getEarnCode(job.getPayTypeObj().getRegEarnCode(), asOfDate);
160            if (regularEarnCode == null) {
161                throw new RuntimeException("No regular earn code defined for job pay type.");
162            } else {
163                earnCodes.add(regularEarnCode);
164            }
165            List<EarnCodeSecurity> decs = TkServiceLocator.getEarnCodeSecurityService().getEarnCodeSecurities(job.getDept(), job.getHrSalGroup(), job.getLocation(), asOfDate);
166            for (EarnCodeSecurity dec : decs) {
167                    boolean addEarnCode = false;
168                    // Check employee flag
169                    if (dec.isEmployee() &&
170                            (StringUtils.equals(TKUser.getCurrentTargetPerson().getEmployeeId(), GlobalVariables.getUserSession().getPerson().getEmployeeId()))) {
171                        addEarnCode = true;
172                    }
173                    // Check approver flag
174                    if (!addEarnCode && dec.isApprover()) {
175                        Set<Long> workAreas = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).getApproverWorkAreas();
176                        for (Long wa : workAreas) {
177                            WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(wa, asOfDate);
178                            if (workArea!= null && a.getWorkArea().compareTo(workArea.getWorkArea())==0) {
179                                addEarnCode = true;
180                                break;
181                            }
182                        }
183                    }
184                    if (addEarnCode) {
185                        EarnCode ec = getEarnCode(dec.getEarnCode(), asOfDate);
186                        if(ec!=null){
187                            earnCodes.add(ec);
188                        }
189                    }
190            }
191    
192    
193            return earnCodes;
194        }
195    
196    
197        /* not using yet, may not be needed
198        @Override
199        public Map<String, String> getEarnCod e s ForDisplayWithAssignment(Assignment assignment, Date asOfDate) {
200            List<EarnCode> earnCodes = this.getEarnCod e s ( assignment, asOfDate);
201    
202            Date currentDate = TKUtils.getCurrentDate();
203            boolean futureDate = asOfDate.after(currentDate);
204            List<EarnCode> copyList = new ArrayList<EarnCode>();
205            copyList.addAll(earnCodes);
206            for (EarnCode earnCode : copyList) {
207                if ( futureDate
208                        && !earnCode.getAllowScheduledLeave().equalsIgnoreCase("Y")) {
209                    earnCodes.remove(earnCode);
210                }
211            }
212            Comparator<EarnCode> earnCodeComparator = new Comparator<EarnCode>() {
213                @Override
214                public int compare(EarnCode ec1, EarnCode ec2) {
215                    return ec1.getEarnCode().compareToIgnoreCase(ec2.getEarnCode());
216                }
217            };
218            // Order by leaveCode ascending
219            Ordering<EarnCode> ordering = Ordering.from(earnCodeComparator);
220    
221            Map<String, String> earnCodesForDisplay = new LinkedHashMap<String, String>();
222            for (EarnCode earnCode : ordering.sortedCopy(earnCodes)) {
223                earnCodesForDisplay.put(earnCode.getEarnCodeKeyForDisplay(), earnCode.getEarnCodeValueForDisplay());
224            }
225            return earnCodesForDisplay;
226        }
227    
228        */
229    }