001    /*
002     * Copyright 2011 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.kfs.module.bc.document.service.impl;
017    
018    import java.math.BigDecimal;
019    import java.util.HashMap;
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.kuali.kfs.module.bc.businessobject.BudgetConstructionPayRateHolding;
025    import org.kuali.kfs.module.bc.document.dataaccess.PayrateExportDao;
026    import org.kuali.kfs.module.bc.document.service.PayrateExportService;
027    import org.kuali.kfs.sys.KFSPropertyConstants;
028    import org.kuali.kfs.sys.service.NonTransactional;
029    import org.kuali.rice.kns.service.BusinessObjectService;
030    import org.springframework.transaction.annotation.Propagation;
031    import org.springframework.transaction.annotation.Transactional;
032    
033    public class PayrateExportServiceImpl implements PayrateExportService {
034        private BusinessObjectService businessObjectService;
035        private PayrateExportDao payrateExportDao;
036        private int exportCount;
037    
038        /**
039         * 
040         * @see org.kuali.kfs.module.bc.service.PayrateExportService#buildExportFile()
041         */
042        @Transactional(propagation=Propagation.REQUIRES_NEW)
043        public StringBuilder buildExportFile(Integer budgetYear, String positionUnionCode, String csfFreezeDate, String principalId) {
044            this.exportCount = 0;
045            Map payRateHoldingPersonUniversalIdentifierKey = new HashMap();
046            payRateHoldingPersonUniversalIdentifierKey.put(KFSPropertyConstants.PERSON_UNIVERSAL_IDENTIFIER, principalId);
047            
048            this.businessObjectService.deleteMatching(BudgetConstructionPayRateHolding.class, payRateHoldingPersonUniversalIdentifierKey);
049            
050            StringBuilder results = new StringBuilder();
051            
052            this.payrateExportDao.buildPayRateHoldingRows(budgetYear, positionUnionCode, principalId);
053            List<BudgetConstructionPayRateHolding> holdingRecords = (List<BudgetConstructionPayRateHolding>) this.businessObjectService.findMatching(BudgetConstructionPayRateHolding.class, payRateHoldingPersonUniversalIdentifierKey);
054            for (BudgetConstructionPayRateHolding record : holdingRecords) {
055                results.append(buildExportLine(record, csfFreezeDate));
056                exportCount++;
057            }
058            results.append("\r\n");
059            results.append("\r\n");
060            results.append("Export complete. Export Count: " + exportCount + "\r\n");
061            return results;
062        }
063        
064        /**
065         * 
066         * @see org.kuali.kfs.module.bc.document.service.PayrateExportService#isValidPositionUnionCode(java.lang.String)
067         */
068        @Transactional
069        public boolean isValidPositionUnionCode(String positionUnionCode) {
070            
071            return this.payrateExportDao.isValidPositionUnionCode(positionUnionCode);
072        }
073    
074        /**
075         * 
076         * @param businessObjectService
077         */
078        @NonTransactional
079        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
080            this.businessObjectService = businessObjectService;
081        }
082        
083        /**
084         * 
085         * 
086         * @param payrateExportDao
087         */
088        @NonTransactional
089        public void setPayrateExportDao(PayrateExportDao payrateExportDao) {
090            this.payrateExportDao = payrateExportDao;
091        }
092        
093        /**
094         * Creates the export line
095         * 
096         * @param holdingRecord
097         * @return
098         */
099        @NonTransactional
100        protected StringBuilder buildExportLine(BudgetConstructionPayRateHolding holdingRecord, String csfFreezeDate) {
101            StringBuilder line = new StringBuilder();
102            String emplid = padString(holdingRecord.getEmplid(), 11, true);
103            String positionNumber = padString(holdingRecord.getPositionNumber(), 8, true);
104            String name = padString(holdingRecord.getName(), 50, true);
105            String setIdSalary = padString(holdingRecord.getSetidSalary(), 5, true);
106            String salAdminPlan = padString(holdingRecord.getSalaryAdministrationPlan(), 4, true);
107            String grade = padString(holdingRecord.getGrade(), 3, true);
108            String unionCode = padString(holdingRecord.getUnionCode(), 3, true);
109            String payRate = padString(String.valueOf(holdingRecord.getAppointmentRequestedPayRate().multiply(new BigDecimal(100)).intValue()), 10, false);
110            csfFreezeDate = padString(csfFreezeDate, 8, true);
111            
112            line.append(emplid);
113            line.append(positionNumber);
114            line.append(name);
115            line.append(setIdSalary);
116            line.append(salAdminPlan);
117            line.append(grade);
118            line.append(unionCode);
119            line.append(payRate);
120            line.append(csfFreezeDate);
121            line.append("\r\n");
122            
123            return line;
124        }
125        
126        /**
127         * Returns a field that is the length of fieldSize, to facilitate formatting payrate export file
128         * 
129         * @param stringToPad
130         * @param fieldSize
131         * @return
132         */
133        @NonTransactional
134        protected String padString(String stringToPad, int fieldSize, boolean leftJustifiy) {
135            if (stringToPad.length() < fieldSize) {
136                if (leftJustifiy) return StringUtils.rightPad(stringToPad, fieldSize);
137                else return StringUtils.leftPad(stringToPad, fieldSize);
138            }
139            else if (stringToPad.length() > fieldSize) return stringToPad.substring(0, fieldSize - 1);
140            
141            return stringToPad;
142        }
143    }
144