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