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.CustomerCreditMemoDocument;
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.util.KNSConstants;
033    import org.kuali.rice.kns.web.ui.ExtraButton;
034    
035    public class CustomerCreditMemoDocumentForm extends FinancialSystemTransactionalDocumentFormBase {
036       
037        public CustomerCreditMemoDocumentForm() {
038            super();
039        }
040        
041        @Override
042        protected String getDefaultDocumentTypeName() {
043            return "CRM";
044        }
045        
046        /**
047         * Setup workflow doc in the document.
048         */
049        @Override
050        public void populate(HttpServletRequest request) {
051            
052            //populate document using request
053            super.populate(request);
054            
055            CustomerCreditMemoDocument customerCreditMemoDocument = (CustomerCreditMemoDocument)getDocument();
056            String customerInvoiceNumber = customerCreditMemoDocument.getFinancialDocumentReferenceInvoiceNumber();
057            
058            //this will make sure that every action has fully populated invoice
059            if(StringUtils.isNotEmpty(customerInvoiceNumber) /*&& customerCreditMemoDocument.getInvoice() == null*/){
060                customerCreditMemoDocument.refreshReferenceObject("invoice");
061            }        
062        }
063    
064        /**
065         * Build additional customer credit memo specific buttons and set extraButtons list.
066         * 
067         * @return - list of extra buttons to be displayed to the user
068         */
069        @Override
070        public List<ExtraButton> getExtraButtons() {
071            
072            // clear out the extra buttons array
073            extraButtons.clear();
074    
075            CustomerCreditMemoDocument creditMemoDoc = (CustomerCreditMemoDocument) getDocument();
076            DocumentHelperService docHelperService = SpringContext.getBean(DocumentHelperService.class);
077            TransactionalDocumentPresentationController presoController = 
078                    (TransactionalDocumentPresentationController) docHelperService.getDocumentPresentationController(creditMemoDoc);
079            Set<String> editModes = presoController.getEditModes(creditMemoDoc);
080    
081            //  special buttons for the first 'init' screen
082            if (editModes.contains(ArAuthorizationConstants.CustomerCreditMemoEditMode.DISPLAY_INIT_TAB)) {
083                String externalImageURL = SpringContext.getBean(KualiConfigurationService.class).getPropertyString(KFSConstants.RICE_EXTERNALIZABLE_IMAGES_URL_KEY);
084                addExtraButton("methodToCall.continueCreditMemo", externalImageURL + "buttonsmall_continue.gif", "Continue");
085                addExtraButton("methodToCall.clearInitTab", externalImageURL + "buttonsmall_clear.gif", "Clear");
086            }
087            
088            //  show the print button if appropriate 
089            if (editModes.contains(ArAuthorizationConstants.CustomerCreditMemoEditMode.DISPLAY_PRINT_BUTTON)) {
090                String printButtonURL = SpringContext.getBean(KualiConfigurationService.class).getPropertyString(KFSConstants.EXTERNALIZABLE_IMAGES_URL_KEY);
091                addExtraButton("methodToCall.print", printButtonURL + "buttonsmall_genprintfile.gif", "Print");
092            }
093    
094            return extraButtons;
095        }
096        
097        /**
098         * Adds a new button to the extra buttons collection.
099         * 
100         * @param property - property for button
101         * @param source - location of image
102         * @param altText - alternate text for button if images don't appear
103         */
104        protected void addExtraButton(String property, String source, String altText) {
105    
106            ExtraButton newButton = new ExtraButton();
107    
108            newButton.setExtraButtonProperty(property);
109            newButton.setExtraButtonSource(source);
110            newButton.setExtraButtonAltText(altText);
111    
112            extraButtons.add(newButton);
113        }
114    
115        /**
116         * @see org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase#shouldMethodToCallParameterBeUsed(java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest)
117         */
118        @Override
119        public boolean shouldMethodToCallParameterBeUsed(String methodToCallParameterName, String methodToCallParameterValue, HttpServletRequest request) {
120            if (KNSConstants.DISPATCH_REQUEST_PARAMETER.equals(methodToCallParameterName) && "printCreditMemoPDF".equals(methodToCallParameterValue)) {
121                return true;
122            }
123            return super.shouldMethodToCallParameterBeUsed(methodToCallParameterName, methodToCallParameterValue, request);
124        }
125    
126    }