View Javadoc
1   /*
2    * Copyright 2008 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.sys.document.authorization;
17  
18  import java.util.Set;
19  
20  import org.joda.time.DateTime;
21  import org.kuali.ole.sys.OLEConstants;
22  import org.kuali.ole.sys.context.SpringContext;
23  import org.kuali.ole.sys.document.AmountTotaling;
24  import org.kuali.ole.sys.document.Correctable;
25  import org.kuali.ole.sys.document.FinancialSystemTransactionalDocument;
26  import org.kuali.ole.sys.document.LedgerPostingDocument;
27  import org.kuali.ole.sys.document.datadictionary.FinancialSystemTransactionalDocumentEntry;
28  import org.kuali.ole.sys.service.BankService;
29  import org.kuali.ole.sys.service.UniversityDateService;
30  import org.kuali.ole.sys.service.impl.OleParameterConstants;
31  import org.kuali.rice.core.api.parameter.ParameterEvaluator;
32  import org.kuali.rice.core.api.parameter.ParameterEvaluatorService;
33  import org.kuali.rice.kew.api.WorkflowDocument;
34  import org.kuali.rice.kns.document.authorization.TransactionalDocumentPresentationControllerBase;
35  import org.kuali.rice.kns.service.DataDictionaryService;
36  import org.kuali.rice.krad.datadictionary.DocumentEntry;
37  import org.kuali.rice.krad.document.Document;
38  import org.springframework.util.ObjectUtils;
39  
40  /**
41   * Base class for all FinancialSystemDocumentPresentationControllers.
42   */
43  public class FinancialSystemTransactionalDocumentPresentationControllerBase extends TransactionalDocumentPresentationControllerBase implements FinancialSystemTransactionalDocumentPresentationController {
44      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FinancialSystemTransactionalDocumentPresentationControllerBase.class);
45  
46      private static ParameterEvaluatorService parameterEvaluatorService;
47      private static BankService bankService;
48      private static UniversityDateService universityDateService;
49      private static DataDictionaryService dataDictionaryService;
50  
51      /**
52       * Makes sure that the given document implements error correction, that error correction is turned on for the document in the
53       * data dictionary, and that the document is in a workflow state that allows error correction.
54       *
55       * @see org.kuali.ole.sys.document.authorization.FinancialSystemTransactionalDocumentPresentationController#canErrorCorrect(org.kuali.ole.sys.document.FinancialSystemTransactionalDocument)
56       */
57      @Override
58      public boolean canErrorCorrect(FinancialSystemTransactionalDocument document) {
59          if (!(document instanceof Correctable)) {
60              return false;
61          }
62  
63          if (!this.canCopy(document)) {
64              return false;
65          }
66          DocumentEntry documentEntry = getDataDictionaryService().getDataDictionary().getDocumentEntry(document.getClass().getName());
67          //FinancialSystemTransactionalDocumentEntry documentEntry = (FinancialSystemTransactionalDocumentEntry) ();
68  
69          if ( !(documentEntry instanceof FinancialSystemTransactionalDocumentEntry)
70                  || !((FinancialSystemTransactionalDocumentEntry)documentEntry).getAllowsErrorCorrection()) {
71              return false;
72          }
73  
74          if (document.getFinancialSystemDocumentHeader().getCorrectedByDocumentId() != null) {
75              return false;
76          }
77  
78          if (document.getFinancialSystemDocumentHeader().getFinancialDocumentInErrorNumber() != null) {
79              return false;
80          }
81  
82          WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
83          if (!isApprovalDateWithinFiscalYear(workflowDocument)) {
84              return false;
85          }
86  
87          return workflowDocument.isApproved();
88      }
89  
90      protected boolean isApprovalDateWithinFiscalYear(WorkflowDocument workflowDocument) {
91          if ( workflowDocument != null ) {
92              DateTime approvalDate = workflowDocument.getDateApproved();
93              if ( approvalDate != null ) {
94              // compare approval fiscal year with current fiscal year
95                  Integer approvalYear = getUniversityDateService().getFiscalYear(approvalDate.toDate());
96                  Integer currentFiscalYear = getUniversityDateService().getCurrentFiscalYear();
97                  return ObjectUtils.nullSafeEquals(currentFiscalYear, approvalYear);
98              }
99          }
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 }