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.ar.document.web.struts;
017    
018    import java.util.List;
019    import java.util.Set;
020    
021    import javax.servlet.http.HttpServletRequest;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.kuali.kfs.module.ar.ArAuthorizationConstants;
025    import org.kuali.kfs.module.ar.document.CustomerInvoiceWriteoffDocument;
026    import org.kuali.kfs.sys.KFSConstants;
027    import org.kuali.kfs.sys.context.SpringContext;
028    import org.kuali.kfs.sys.document.web.struts.FinancialSystemTransactionalDocumentFormBase;
029    import org.kuali.rice.kns.document.authorization.TransactionalDocumentPresentationController;
030    import org.kuali.rice.kns.service.DocumentHelperService;
031    import org.kuali.rice.kns.service.KualiConfigurationService;
032    import org.kuali.rice.kns.web.ui.ExtraButton;
033    
034    public class CustomerInvoiceWriteoffDocumentForm extends FinancialSystemTransactionalDocumentFormBase {
035        
036        public CustomerInvoiceWriteoffDocumentForm() {
037            super();
038        }
039    
040        @Override
041        protected String getDefaultDocumentTypeName() {
042            return "INVW";
043        }
044        
045        /**
046         * Setup workflow doc in the document.
047         */
048        @Override
049        public void populate(HttpServletRequest request) {
050            
051            //populate document using request
052            super.populate(request);
053            
054            CustomerInvoiceWriteoffDocument customerInvoiceWriteoffDocument = (CustomerInvoiceWriteoffDocument)getDocument();
055            String customerInvoiceNumber = customerInvoiceWriteoffDocument.getFinancialDocumentReferenceInvoiceNumber();
056            
057            //this will make sure that every action has fully populated invoice
058            if(StringUtils.isNotEmpty(customerInvoiceNumber)){
059                customerInvoiceWriteoffDocument.refreshReferenceObject("customerInvoiceDocument");
060            }        
061        }
062    
063        /**
064         * Build additional customer credit memo specific buttons and set extraButtons list.
065         * 
066         * @return - list of extra buttons to be displayed to the user
067         */
068        @Override
069        public List<ExtraButton> getExtraButtons() {
070            
071            // clear out the extra buttons array
072            extraButtons.clear();
073    
074            CustomerInvoiceWriteoffDocument writeoffDoc = (CustomerInvoiceWriteoffDocument) getDocument();
075            DocumentHelperService documentHelperService = SpringContext.getBean(DocumentHelperService.class);
076            TransactionalDocumentPresentationController presoController = 
077                    (TransactionalDocumentPresentationController) documentHelperService.getDocumentPresentationController(writeoffDoc);
078            Set<String> editModes = presoController.getEditModes(writeoffDoc);
079    
080            if (editModes.contains(ArAuthorizationConstants.CustomerCreditMemoEditMode.DISPLAY_INIT_TAB)) {
081                String externalImageURL = SpringContext.getBean(KualiConfigurationService.class).getPropertyString(KFSConstants.RICE_EXTERNALIZABLE_IMAGES_URL_KEY);
082                addExtraButton("methodToCall.continueCustomerInvoiceWriteoff", externalImageURL + "buttonsmall_continue.gif", "Continue");
083                addExtraButton("methodToCall.clearInitTab", externalImageURL + "buttonsmall_clear.gif", "Clear");
084            }
085            return extraButtons;
086        }
087        
088        /**
089         * Adds a new button to the extra buttons collection.
090         * 
091         * @param property - property for button
092         * @param source - location of image
093         * @param altText - alternate text for button if images don't appear
094         */
095        protected void addExtraButton(String property, String source, String altText) {
096    
097            ExtraButton newButton = new ExtraButton();
098    
099            newButton.setExtraButtonProperty(property);
100            newButton.setExtraButtonSource(source);
101            newButton.setExtraButtonAltText(altText);
102    
103            extraButtons.add(newButton);
104        }
105    }