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;
017    
018    import java.util.Map;
019    
020    import org.apache.commons.lang.StringUtils;
021    import org.kuali.kfs.module.purap.PurapKeyConstants;
022    import org.kuali.kfs.sys.document.FinancialSystemMaintainable;
023    import org.kuali.kfs.vnd.VendorUtils;
024    import org.kuali.rice.kns.document.MaintenanceDocument;
025    import org.kuali.rice.kns.util.GlobalVariables;
026    import org.kuali.rice.kns.util.KNSConstants;
027    
028    /* 
029     * A special implementation of Maintainable specifically for PurchaseOrderQuoteLanguage
030     * maintenance page to override the behavior when the PurchaseOrderQuoteLanguage 
031     * maintenance document is copied.
032    */
033    public class PurchaseOrderQuoteListMaintainableImpl extends FinancialSystemMaintainable {
034        @Override
035        public Map<String, String> populateNewCollectionLines( Map<String, String> fieldValues, MaintenanceDocument maintenanceDocument, String methodToCall ) {
036            String collName = "quoteListVendors.vendorDetail.vendorNumber";
037            String vendorNumber = (String)fieldValues.get(collName);
038            if (StringUtils.isNotBlank(vendorNumber)) {
039                if (!VendorUtils.validVendorNumberFormat(vendorNumber)) {
040                    GlobalVariables.getMessageMap().putError(KNSConstants.MAINTENANCE_ADD_PREFIX + collName , PurapKeyConstants.ERROR_PURCHASE_ORDER_QUOTE_LIST_NON_EXISTENCE_VENDOR);
041                    return fieldValues;
042                }
043                else {
044                    Integer headerId = VendorUtils.getVendorHeaderId(vendorNumber);
045                    Integer detailId = VendorUtils.getVendorDetailId(vendorNumber);
046                    resetPreviousVendorInformationOnAddLine(fieldValues, headerId, detailId);
047                }
048            }
049            
050            return super.populateNewCollectionLines(fieldValues, maintenanceDocument, methodToCall);
051        }
052        
053        private void resetPreviousVendorInformationOnAddLine(Map fieldValues, Integer headerId, Integer detailId) {        
054            if (fieldValues.get("quoteListVendors.vendorHeaderGeneratedIdentifier") != null) {
055                fieldValues.put("quoteListVendors.vendorHeaderGeneratedIdentifier", headerId);
056            }
057            
058            if (fieldValues.get("quoteListVendors.vendorDetailAssignedIdentifier") != null) {
059                fieldValues.put("quoteListVendors.vendorDetailAssignedIdentifier", detailId);
060            }
061            
062        }
063    }