001/*
002 * Copyright 2008 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 */
016package org.kuali.ole.sys.document.authorization;
017
018import java.util.Set;
019
020import org.joda.time.DateTime;
021import org.kuali.ole.sys.OLEConstants;
022import org.kuali.ole.sys.context.SpringContext;
023import org.kuali.ole.sys.document.AmountTotaling;
024import org.kuali.ole.sys.document.Correctable;
025import org.kuali.ole.sys.document.FinancialSystemTransactionalDocument;
026import org.kuali.ole.sys.document.LedgerPostingDocument;
027import org.kuali.ole.sys.document.datadictionary.FinancialSystemTransactionalDocumentEntry;
028import org.kuali.ole.sys.service.BankService;
029import org.kuali.ole.sys.service.UniversityDateService;
030import org.kuali.ole.sys.service.impl.OleParameterConstants;
031import org.kuali.rice.core.api.parameter.ParameterEvaluator;
032import org.kuali.rice.core.api.parameter.ParameterEvaluatorService;
033import org.kuali.rice.kew.api.WorkflowDocument;
034import org.kuali.rice.kns.document.authorization.TransactionalDocumentPresentationControllerBase;
035import org.kuali.rice.kns.service.DataDictionaryService;
036import org.kuali.rice.krad.datadictionary.DocumentEntry;
037import org.kuali.rice.krad.document.Document;
038import org.springframework.util.ObjectUtils;
039
040/**
041 * Base class for all FinancialSystemDocumentPresentationControllers.
042 */
043public class FinancialSystemTransactionalDocumentPresentationControllerBase extends TransactionalDocumentPresentationControllerBase implements FinancialSystemTransactionalDocumentPresentationController {
044    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FinancialSystemTransactionalDocumentPresentationControllerBase.class);
045
046    private static ParameterEvaluatorService parameterEvaluatorService;
047    private static BankService bankService;
048    private static UniversityDateService universityDateService;
049    private static DataDictionaryService dataDictionaryService;
050
051    /**
052     * Makes sure that the given document implements error correction, that error correction is turned on for the document in the
053     * data dictionary, and that the document is in a workflow state that allows error correction.
054     *
055     * @see org.kuali.ole.sys.document.authorization.FinancialSystemTransactionalDocumentPresentationController#canErrorCorrect(org.kuali.ole.sys.document.FinancialSystemTransactionalDocument)
056     */
057    @Override
058    public boolean canErrorCorrect(FinancialSystemTransactionalDocument document) {
059        if (!(document instanceof Correctable)) {
060            return false;
061        }
062
063        if (!this.canCopy(document)) {
064            return false;
065        }
066        DocumentEntry documentEntry = getDataDictionaryService().getDataDictionary().getDocumentEntry(document.getClass().getName());
067        //FinancialSystemTransactionalDocumentEntry documentEntry = (FinancialSystemTransactionalDocumentEntry) ();
068
069        if ( !(documentEntry instanceof FinancialSystemTransactionalDocumentEntry)
070                || !((FinancialSystemTransactionalDocumentEntry)documentEntry).getAllowsErrorCorrection()) {
071            return false;
072        }
073
074        if (document.getFinancialSystemDocumentHeader().getCorrectedByDocumentId() != null) {
075            return false;
076        }
077
078        if (document.getFinancialSystemDocumentHeader().getFinancialDocumentInErrorNumber() != null) {
079            return false;
080        }
081
082        WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
083        if (!isApprovalDateWithinFiscalYear(workflowDocument)) {
084            return false;
085        }
086
087        return workflowDocument.isApproved();
088    }
089
090    protected boolean isApprovalDateWithinFiscalYear(WorkflowDocument workflowDocument) {
091        if ( workflowDocument != null ) {
092            DateTime approvalDate = workflowDocument.getDateApproved();
093            if ( approvalDate != null ) {
094            // compare approval fiscal year with current fiscal year
095                Integer approvalYear = getUniversityDateService().getFiscalYear(approvalDate.toDate());
096                Integer currentFiscalYear = getUniversityDateService().getCurrentFiscalYear();
097                return ObjectUtils.nullSafeEquals(currentFiscalYear, approvalYear);
098            }
099        }
100        return true;
101    }
102
103    /**
104     * @see org.kuali.rice.krad.document.authorization.DocumentPresentationControllerBase#getDocumentActions(org.kuali.rice.krad.document.Document)
105     */
106    @Override
107    public Set<String> getDocumentActions(Document document) {
108        Set<String> documentActions = super.getDocumentActions(document);
109
110        if (document instanceof FinancialSystemTransactionalDocument) {
111            if (canErrorCorrect((FinancialSystemTransactionalDocument) document)) {
112                documentActions.add(OLEConstants.KFS_ACTION_CAN_ERROR_CORRECT);
113            }
114
115            if (canHaveBankEntry(document)) {
116                documentActions.add(OLEConstants.KFS_ACTION_CAN_EDIT_BANK);
117            }
118        }
119
120        // CSU 6702 BEGIN
121        // rSmart-jkneal-KFSCSU-199-begin mod for adding accounting period view action
122        if (document instanceof LedgerPostingDocument) {
123            // check account period selection is enabled
124            // PERFORMANCE: cache this setting - move call to service
125            boolean accountingPeriodEnabled = getParameterService().getParameterValueAsBoolean(OLEConstants.CoreModuleNamespaces.OLE, OleParameterConstants.YEAR_END_ACCOUNTING_PERIOD_PARAMETER_NAMES.DETAIL_PARAMETER_TYPE, OleParameterConstants.YEAR_END_ACCOUNTING_PERIOD_PARAMETER_NAMES.ENABLE_FISCAL_PERIOD_SELECTION_IND, false);
126            if ( accountingPeriodEnabled) {
127                // check accounting period is enabled for doc type in system parameter
128                String docType = document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName();
129                // PERFORMANCE: cache this setting - move call to service
130                ParameterEvaluator evaluator = getParameterEvaluatorService().getParameterEvaluator(OLEConstants.CoreModuleNamespaces.OLE, OleParameterConstants.YEAR_END_ACCOUNTING_PERIOD_PARAMETER_NAMES.DETAIL_PARAMETER_TYPE, OleParameterConstants.YEAR_END_ACCOUNTING_PERIOD_PARAMETER_NAMES.FISCAL_PERIOD_SELECTION_DOCUMENT_TYPES, docType);
131                if (evaluator.evaluationSucceeds()) {
132                    documentActions.add(OLEConstants.YEAR_END_ACCOUNTING_PERIOD_VIEW_DOCUMENT_ACTION);
133                }
134            }
135        }
136        // rSmart-jkneal-KFSCSU-199-end mod
137        // CSU 6702 END
138
139        return documentActions;
140    }
141
142    /**
143     * @see org.kuali.rice.krad.document.authorization.TransactionalDocumentPresentationControllerBase#getEditModes(org.kuali.rice.krad.document.Document)
144     */
145    @Override
146    public Set<String> getEditModes(Document document) {
147        Set<String> editModes = super.getEditModes(document);
148
149        if (document instanceof AmountTotaling) {
150            editModes.add(OLEConstants.AMOUNT_TOTALING_EDITING_MODE);
151        }
152
153        if (this.canHaveBankEntry(document)) {
154            editModes.add(OLEConstants.BANK_ENTRY_VIEWABLE_EDITING_MODE);
155        }
156
157        return editModes;
158    }
159
160    // check if bank entry should be viewable for the given document
161    protected boolean canHaveBankEntry(Document document) {
162        boolean bankSpecificationEnabled = getBankService().isBankSpecificationEnabled();
163
164        if (bankSpecificationEnabled) {
165            String documentTypeName = document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName();
166
167            return getBankService().isBankSpecificationEnabledForDocument(document.getClass());
168        }
169
170        return false;
171    }
172
173    protected ParameterEvaluatorService getParameterEvaluatorService() {
174        if (parameterEvaluatorService == null) {
175            parameterEvaluatorService = SpringContext.getBean(ParameterEvaluatorService.class);
176        }
177        return parameterEvaluatorService;
178    }
179
180    protected BankService getBankService() {
181        if (bankService == null) {
182            bankService = SpringContext.getBean(BankService.class);
183        }
184        return bankService;
185    }
186
187    public DataDictionaryService getDataDictionaryService() {
188        if (dataDictionaryService == null) {
189            dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
190        }
191        return dataDictionaryService;
192    }
193
194    public UniversityDateService getUniversityDateService() {
195        if (universityDateService == null) {
196            universityDateService = SpringContext.getBean(UniversityDateService.class);
197        }
198        return universityDateService;
199    }
200}