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.authorization;
017    
018    import java.util.Set;
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import org.kuali.kfs.sys.KFSConstants;
023    import org.kuali.kfs.sys.KFSParameterKeyConstants;
024    import org.kuali.kfs.sys.businessobject.Bank;
025    import org.kuali.kfs.sys.context.SpringContext;
026    import org.kuali.kfs.sys.document.AmountTotaling;
027    import org.kuali.kfs.sys.document.Correctable;
028    import org.kuali.kfs.sys.document.FinancialSystemTransactionalDocument;
029    import org.kuali.kfs.sys.document.datadictionary.FinancialSystemTransactionalDocumentEntry;
030    import org.kuali.kfs.sys.service.BankService;
031    import org.kuali.rice.kns.datadictionary.DataDictionary;
032    import org.kuali.rice.kns.document.Document;
033    import org.kuali.rice.kns.document.authorization.TransactionalDocumentPresentationControllerBase;
034    import org.kuali.rice.kns.service.DataDictionaryService;
035    import org.kuali.rice.kns.service.ParameterEvaluator;
036    import org.kuali.rice.kns.service.ParameterService;
037    import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;
038    
039    /**
040     * Base class for all FinancialSystemDocumentPresentationControllers.
041     */
042    public class FinancialSystemTransactionalDocumentPresentationControllerBase extends TransactionalDocumentPresentationControllerBase implements FinancialSystemTransactionalDocumentPresentationController {
043        private static Log LOG = LogFactory.getLog(FinancialSystemTransactionalDocumentPresentationControllerBase.class);
044                
045        /**
046         * Makes sure that the given document implements error correction, that error correction is turned on for the document in the
047         * data dictionary, and that the document is in a workflow state that allows error correction.
048         * 
049         * @see org.kuali.kfs.sys.document.authorization.FinancialSystemTransactionalDocumentPresentationController#canErrorCorrect(org.kuali.kfs.sys.document.FinancialSystemTransactionalDocument)
050         */
051        public boolean canErrorCorrect(FinancialSystemTransactionalDocument document) {
052            if (!(document instanceof Correctable)) {
053                return false;
054            }
055            
056            if (!this.canCopy(document)) {
057                return false;
058            }
059    
060            DataDictionary dataDictionary = SpringContext.getBean(DataDictionaryService.class).getDataDictionary();
061            FinancialSystemTransactionalDocumentEntry documentEntry = (FinancialSystemTransactionalDocumentEntry) (dataDictionary.getDocumentEntry(document.getClass().getName()));
062    
063            if (!documentEntry.getAllowsErrorCorrection()) {
064                return false;
065            }
066    
067            if (document.getDocumentHeader().getCorrectedByDocumentId() != null) {
068                return false;
069            }
070    
071            if (document.getDocumentHeader().getFinancialDocumentInErrorNumber() != null) {
072                return false;
073            }
074    
075            final KualiWorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
076               
077            return (workflowDocument.stateIsApproved() || workflowDocument.stateIsProcessed() || workflowDocument.stateIsFinal());
078        }
079    
080        /**
081         * @see org.kuali.rice.kns.document.authorization.DocumentPresentationControllerBase#getDocumentActions(org.kuali.rice.kns.document.Document)
082         */
083        @Override
084        public Set<String> getDocumentActions(Document document) {
085            Set<String> documentActions = super.getDocumentActions(document);
086            
087            if (document instanceof FinancialSystemTransactionalDocument) {
088                if (canErrorCorrect((FinancialSystemTransactionalDocument) document)) {
089                    documentActions.add(KFSConstants.KFS_ACTION_CAN_ERROR_CORRECT);
090                }
091                
092                if (canHaveBankEntry(document)) {
093                    documentActions.add(KFSConstants.KFS_ACTION_CAN_EDIT_BANK);
094                }
095            }
096            
097            return documentActions;
098        }
099    
100        /**
101         * @see org.kuali.rice.kns.document.authorization.TransactionalDocumentPresentationControllerBase#getEditModes(org.kuali.rice.kns.document.Document)
102         */
103        @Override
104        public Set<String> getEditModes(Document document) {
105            Set<String> editModes = super.getEditModes(document);
106    
107            if (document instanceof AmountTotaling) {
108                editModes.add(KFSConstants.AMOUNT_TOTALING_EDITING_MODE);
109            }
110            
111            if (this.canHaveBankEntry(document)) {
112                editModes.add(KFSConstants.BANK_ENTRY_VIEWABLE_EDITING_MODE);
113            }
114    
115            return editModes;
116        }
117    
118        // check if bank entry should be viewable for the given document
119        protected boolean canHaveBankEntry(Document document) {        
120            boolean bankSpecificationEnabled = getBankService().isBankSpecificationEnabled();
121            
122            if (bankSpecificationEnabled) {            
123                String documentTypeName = document.getDocumentHeader().getWorkflowDocument().getDocumentType();
124    
125                ParameterEvaluator evaluator = getParameterService().getParameterEvaluator(Bank.class, KFSParameterKeyConstants.BANK_CODE_DOCUMENT_TYPES, documentTypeName);
126                return evaluator.evaluationSucceeds();
127            }
128    
129            return false;
130        }
131        
132        private static BankService bankService;
133        protected BankService getBankService() {
134            if ( bankService == null ) {
135                bankService = SpringContext.getBean(BankService.class);
136            }
137            return bankService;
138        }
139    }