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.sys.document.web.struts;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    import java.util.Set;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.kuali.kfs.sys.KFSConstants;
024    import org.kuali.kfs.sys.businessobject.FinancialSystemDocumentHeader;
025    import org.kuali.kfs.sys.context.SpringContext;
026    import org.kuali.rice.kns.service.DocumentHelperService;
027    import org.kuali.rice.kns.service.KualiConfigurationService;
028    import org.kuali.rice.kns.util.GlobalVariables;
029    import org.kuali.rice.kns.util.KNSConstants;
030    import org.kuali.rice.kns.web.struts.form.KualiTransactionalDocumentFormBase;
031    import org.kuali.rice.kns.web.ui.ExtraButton;
032    import org.kuali.rice.kns.web.ui.HeaderField;
033    import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;
034    
035    /**
036     * This class is a Financial System specific transactional document form base
037     */
038    public class FinancialSystemTransactionalDocumentFormBase extends KualiTransactionalDocumentFormBase {
039    
040        /**
041         * Constructs a FinancialSystemTransactionalDocumentFormBase.java.
042         */
043        public FinancialSystemTransactionalDocumentFormBase() {
044            super();
045        }
046    
047        /**
048         * @see org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase#populateHeaderFields(org.kuali.rice.kns.workflow.service.KualiWorkflowDocument)
049         */
050        @Override
051        public void populateHeaderFields(KualiWorkflowDocument workflowDocument) {
052            super.populateHeaderFields(workflowDocument);
053            if (getDocument().getDocumentHeader() instanceof FinancialSystemDocumentHeader) {
054                FinancialSystemDocumentHeader documentHeader = (FinancialSystemDocumentHeader)getDocument().getDocumentHeader();
055                if (StringUtils.isNotBlank(documentHeader.getFinancialDocumentInErrorNumber())) {
056                    extendDocInfoToThreeColumns();
057                    int insertIndex = 2;
058                    getDocInfo().remove(insertIndex);
059                    getDocInfo().add(insertIndex, new HeaderField("DataDictionary.FinancialSystemDocumentHeader.attributes.financialDocumentInErrorNumber", 
060                            documentHeader.getFinancialDocumentInErrorNumber(), buildHtmlLink(getDocumentHandlerUrl(documentHeader.getFinancialDocumentInErrorNumber()), documentHeader.getFinancialDocumentInErrorNumber())));
061                }
062                if (StringUtils.isNotBlank(documentHeader.getCorrectedByDocumentId())) {
063                    extendDocInfoToThreeColumns();
064                    int insertIndex = getNumColumns() + 2;
065                    getDocInfo().remove(insertIndex);
066                    getDocInfo().add(insertIndex, new HeaderField("DataDictionary.FinancialSystemDocumentHeader.attributes.correctedByDocumentId", 
067                            documentHeader.getCorrectedByDocumentId(), buildHtmlLink(getDocumentHandlerUrl(documentHeader.getCorrectedByDocumentId()), documentHeader.getCorrectedByDocumentId())));
068              
069                
070                }
071            }
072        }
073        
074        /**
075         * Extends the DocInfo on the form to 3 columns if it currently has less than 3 columns.
076         * If it has exactly 3 or more columns, no action will be taken.
077         */
078        protected void extendDocInfoToThreeColumns() {
079            List<HeaderField> newDocInfo = new ArrayList<HeaderField>();
080            int currentColumns = getNumColumns();
081            int targetColumns = 3;
082            if (getNumColumns() < targetColumns) {
083                int column = 0;
084                for (HeaderField headerField : getDocInfo()) {
085                    if (column + 1 > currentColumns) {
086                        newDocInfo.add(HeaderField.EMPTY_FIELD);
087                        column = (column + 1) % targetColumns;
088                    }
089                    newDocInfo.add(headerField);
090                    column = (column + 1) % targetColumns;
091                }
092                // fill out the final row with empty columns
093                while (newDocInfo.size() % targetColumns != 0) {
094                    newDocInfo.add(HeaderField.EMPTY_FIELD);
095                }
096                setDocInfo(newDocInfo);
097                setNumColumns(3);
098            }
099        }
100    
101        /**
102         * @see org.kuali.rice.kns.web.struts.form.KualiForm#getExtraButtons()
103         */
104        @Override
105        public List<ExtraButton> getExtraButtons() {
106            List<ExtraButton> buttons = super.getExtraButtons();
107            if (getDocumentActions().containsKey(KFSConstants.KFS_ACTION_CAN_ERROR_CORRECT)) {
108                buttons.add(generateErrorCorrectionButton());
109            }
110            return buttons;
111        }
112        
113        private ExtraButton errorCorrectionButton;
114        
115        /**
116         * Generates an ExtraButton which represents the error correction button
117         * 
118         * @return an ExtraButton representing an ErrorCorrection button
119         */
120        protected ExtraButton generateErrorCorrectionButton() {
121            if ( errorCorrectionButton == null ) {
122                ExtraButton button = new ExtraButton();
123                button.setExtraButtonAltText("Create error correction document from current document");
124                button.setExtraButtonProperty("methodToCall.correct");
125                button.setExtraButtonSource(SpringContext.getBean(KualiConfigurationService.class).getPropertyString("kr.externalizable.images.url")+"buttonsmall_errcorr.gif");
126                errorCorrectionButton = button;
127            }
128            return errorCorrectionButton;
129        }
130    }