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.ld.document.validation.impl;
017    
018    import java.util.Collection;
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.kuali.kfs.module.ld.LaborConstants;
024    import org.kuali.kfs.module.ld.businessobject.LaborJournalVoucherDetail;
025    import org.kuali.kfs.module.ld.businessobject.PositionData;
026    import org.kuali.kfs.sys.KFSKeyConstants;
027    import org.kuali.kfs.sys.KFSPropertyConstants;
028    import org.kuali.kfs.sys.businessobject.AccountingLine;
029    import org.kuali.kfs.sys.context.SpringContext;
030    import org.kuali.kfs.sys.document.validation.GenericValidation;
031    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
032    import org.kuali.rice.kns.service.BusinessObjectService;
033    import org.kuali.rice.kns.service.DataDictionaryService;
034    import org.kuali.rice.kns.util.GlobalVariables;
035    
036    /**
037     * Validates that a labor journal voucher document's accounting lines have valid Position Code
038     */
039    public class LaborJournalVoucherPositionCodeExistenceCheckValidation extends GenericValidation {
040        private AccountingLine accountingLineForValidation;
041    
042        /**
043         * Validates that the accounting line in the labor journal voucher document for valid position code
044         * 
045         * @see org.kuali.kfs.validation.Validation#validate(java.lang.Object[])
046         */
047        public boolean validate(AttributedDocumentEvent event) {
048            boolean result = true;
049    
050            LaborJournalVoucherDetail laborJournalVoucherDetail = (LaborJournalVoucherDetail) getAccountingLineForValidation();
051            String positionNumber = laborJournalVoucherDetail.getPositionNumber();
052    
053            if (StringUtils.isBlank(positionNumber) || LaborConstants.getDashPositionNumber().equals(positionNumber)) {
054                return true;
055            }
056    
057            if (!positionCodeExistenceCheck(positionNumber)) {
058                result = false;
059            }
060            return result;
061        }
062    
063        /**
064         * Checks whether employee id exists
065         * 
066         * @param positionNumber positionNumber is checked against the collection of position number matches
067         * @return True if the given position number exists, false otherwise.
068         */
069        protected boolean positionCodeExistenceCheck(String positionNumber) {
070    
071            boolean positionNumberExists = true;
072    
073            Map<String, Object> criteria = new HashMap<String, Object>();
074            criteria.put(KFSPropertyConstants.POSITION_NUMBER, positionNumber);
075    
076            Collection<PositionData> positionNumberMatches = SpringContext.getBean(BusinessObjectService.class).findMatching(PositionData.class, criteria);
077            if (positionNumberMatches == null || positionNumberMatches.isEmpty()) {
078                String label = SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getBusinessObjectEntry(PositionData.class.getName()).getAttributeDefinition(KFSPropertyConstants.POSITION_NUMBER).getLabel();
079                GlobalVariables.getMessageMap().putError(KFSPropertyConstants.POSITION_NUMBER, KFSKeyConstants.ERROR_EXISTENCE, label);
080                positionNumberExists = false;
081            }
082    
083            return positionNumberExists;
084        }
085    
086        /**
087         * Gets the accountingLineForValidation attribute.
088         * 
089         * @return Returns the accountingLineForValidation.
090         */
091        public AccountingLine getAccountingLineForValidation() {
092            return accountingLineForValidation;
093        }
094    
095        /**
096         * Sets the accountingLineForValidation attribute value.
097         * 
098         * @param accountingLineForValidation The accountingLineForValidation to set.
099         */
100        public void setAccountingLineForValidation(AccountingLine accountingLineForValidation) {
101            this.accountingLineForValidation = accountingLineForValidation;
102        }
103    }