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.purap.document.validation.impl;
017    
018    import java.util.List;
019    
020    import org.apache.commons.lang.StringUtils;
021    import org.kuali.kfs.module.purap.PurapKeyConstants;
022    import org.kuali.kfs.module.purap.PurapPropertyConstants;
023    import org.kuali.kfs.module.purap.businessobject.PurApItem;
024    import org.kuali.kfs.module.purap.document.PurchasingAccountsPayableDocumentBase;
025    import org.kuali.kfs.sys.document.validation.GenericValidation;
026    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
027    import org.kuali.rice.kns.service.DataDictionaryService;
028    import org.kuali.rice.kns.util.GlobalVariables;
029    import org.kuali.rice.kns.util.ObjectUtils;
030    
031    public class RequisitionAssignToTradeInValidation extends GenericValidation 
032    {
033        private DataDictionaryService dataDictionaryService;
034        
035        public boolean validate(AttributedDocumentEvent event) {
036            
037            // Initialize the valid and true, and get the requisition document.
038            boolean foundTradeIn = false;
039            boolean valid        = true;
040            
041            PurchasingAccountsPayableDocumentBase purapDoc = 
042                (PurchasingAccountsPayableDocumentBase) event.getDocument();
043            
044            // First, get all the items from the requisition document.  For each of
045            // the items, look for the ones that are assigned to a trade-in value.
046            // For these trade-in items, validate that the trade-in line has a valid
047            // description and amount.
048            List<PurApItem> items = (List<PurApItem>)purapDoc.getItems();
049            for (PurApItem item : items) {
050                item.refreshReferenceObject(PurapPropertyConstants.ITEM_TYPE);
051                if (item.getItemAssignedToTradeInIndicator()) {
052                    foundTradeIn = true;
053                    break;
054                }
055            }
056            
057            // Was a trade-in found for any of the above items?
058            if (foundTradeIn) {
059    
060                // Get the trade-in item.
061                PurApItem tradeInItem = purapDoc.getTradeInItem();
062                if (tradeInItem != null) {
063                    if (StringUtils.isEmpty(tradeInItem.getItemDescription())) {
064                        
065                        String attributeLabel = dataDictionaryService.getDataDictionary().getBusinessObjectEntry(tradeInItem.getClass().getName()).getAttributeDefinition(PurapPropertyConstants.ITEM_DESCRIPTION).getLabel();
066                        tradeInItem.getItemLineNumber();
067                        GlobalVariables.getMessageMap().putError(
068                                "document.item[" + (items.size() - 1) + "]." + PurapPropertyConstants.ITEM_DESCRIPTION, 
069                                PurapKeyConstants.ERROR_ITEM_BELOW_THE_LINE, 
070                                "The item description of " + tradeInItem.getItemType().getItemTypeDescription(), 
071                                "empty");
072                        
073                        valid = false;
074                    }
075                    else if (ObjectUtils.isNull(tradeInItem.getItemUnitPrice())) {
076                        
077                        String attributeLabel = dataDictionaryService.getDataDictionary().getBusinessObjectEntry(tradeInItem.getClass().getName()).getAttributeDefinition(PurapPropertyConstants.ITEM_UNIT_PRICE).getLabel();
078                        GlobalVariables.getMessageMap().putError(
079                                "document.item[" + (items.size() - 1) + "]." + PurapPropertyConstants.ITEM_UNIT_PRICE,
080                                PurapKeyConstants.ERROR_ITEM_BELOW_THE_LINE,
081                                tradeInItem.getItemType().getItemTypeDescription(),
082                                "zero");
083                        
084                        valid = false;
085                    }
086                }
087            }
088            
089            return valid;
090        }
091    
092        /**
093         * Sets the dataDictionaryService attribute value.
094         * @param dataDictionaryService The dataDictionaryService to set.
095         */
096        public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
097            this.dataDictionaryService = dataDictionaryService;
098        }
099    
100    }