View Javadoc
1   /*
2    * Copyright 2009 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.fp.document.authorization;
17  
18  import java.util.Set;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.kuali.ole.fp.businessobject.CashDrawer;
22  import org.kuali.ole.fp.document.CashManagementDocument;
23  import org.kuali.ole.fp.document.service.CashManagementService;
24  import org.kuali.ole.fp.service.CashDrawerService;
25  import org.kuali.ole.sys.OLEConstants;
26  import org.kuali.ole.sys.OLEConstants.CashDrawerConstants;
27  import org.kuali.ole.sys.OleAuthorizationConstants;
28  import org.kuali.ole.sys.context.SpringContext;
29  import org.kuali.ole.sys.document.authorization.LedgerPostingDocumentPresentationControllerBase;
30  import org.kuali.rice.kew.api.WorkflowDocument;
31  import org.kuali.rice.kew.api.action.ActionType;
32  import org.kuali.rice.kew.api.action.ValidActions;
33  import org.kuali.rice.kns.service.DataDictionaryService;
34  import org.kuali.rice.krad.document.Document;
35  import org.kuali.rice.krad.service.MaintenanceDocumentService;
36  
37  public class CashManagementDocumentPresentationControllerBase extends LedgerPostingDocumentPresentationControllerBase implements CashManagementDocumentPresentationController {
38  
39      /**
40       * @see org.kuali.ole.sys.document.authorization.FinancialSystemTransactionalDocumentPresentationControllerBase#getEditModes(org.kuali.rice.krad.document.Document)
41       */
42      @Override
43      public Set<String> getEditModes(Document document) {
44          Set<String> editModes = super.getEditModes(document);
45  
46          if (!this.canHaveBankEntry(document)) {
47              editModes.add(OLEConstants.BANK_ENTRY_VIEWABLE_EDITING_MODE);
48          }
49  
50          WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
51          if (workflowDocument.isSaved()) {
52              editModes.add(OleAuthorizationConstants.CashManagementEditMode.ALLOW_CANCEL_DEPOSITS);
53  
54              CashManagementDocument cashManagementDocument = (CashManagementDocument) document;
55              if (!cashManagementDocument.hasFinalDeposit()) {
56                  editModes.add(OleAuthorizationConstants.CashManagementEditMode.ALLOW_ADDITIONAL_DEPOSITS);
57              }
58          }
59  
60          return editModes;
61      }
62  
63      /**
64       * @see org.kuali.rice.krad.document.authorization.DocumentPresentationControllerBase#canApprove(org.kuali.rice.krad.document.Document)
65       */
66      @Override
67      public boolean canApprove(Document document) {
68          WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
69          if (workflowDocument.isEnroute()) {
70              ValidActions validActions = workflowDocument.getValidActions();
71              return validActions.getValidActions().contains(ActionType.APPROVE);
72          }
73  
74          return super.canApprove(document);
75      }
76  
77      /**
78       * @see org.kuali.rice.krad.document.authorization.DocumentPresentationControllerBase#canBlanketApprove(org.kuali.rice.krad.document.Document)
79       */
80      @Override
81      public boolean canBlanketApprove(Document document) {
82          WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
83          if (workflowDocument.isInitiated() || workflowDocument.isSaved()) {
84              CashManagementDocument cmDoc = (CashManagementDocument) document;
85              if (!cmDoc.hasFinalDeposit() || !SpringContext.getBean(CashManagementService.class).allVerifiedCashReceiptsAreDeposited(cmDoc)) {
86                  return false;
87              }
88  
89              // CM document can only be routed if it contains a Final Deposit
90              ValidActions validActions = workflowDocument.getValidActions();
91              return validActions.getValidActions().contains(ActionType.BLANKET_APPROVE);
92          }
93  
94          return super.canBlanketApprove(document);
95      }
96  
97      /**
98       * @see org.kuali.rice.krad.document.authorization.DocumentPresentationControllerBase#canCancel(org.kuali.rice.krad.document.Document)
99       */
100     @Override
101     public boolean canCancel(Document document) {
102         WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
103         if (workflowDocument.isInitiated() || workflowDocument.isSaved()) {
104             CashManagementDocument cmDoc = (CashManagementDocument) document;
105             if (!SpringContext.getBean(CashManagementService.class).allowDocumentCancellation(cmDoc)) {
106                 return false;
107             }
108 
109             // CM document can only be routed if it contains a Final Deposit
110             ValidActions validActions = workflowDocument.getValidActions();
111             return validActions.getValidActions().contains(ActionType.CANCEL);
112         }
113 
114         return super.canCancel(document);
115     }
116 
117     /**
118      * @see org.kuali.rice.krad.document.authorization.DocumentPresentationControllerBase#canDisapprove(org.kuali.rice.krad.document.Document)
119      */
120     @Override
121     public boolean canDisapprove(Document document) {
122         WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
123         if (workflowDocument.isEnroute()) {
124             ValidActions validActions = workflowDocument.getValidActions();
125             return validActions.getValidActions().contains(ActionType.DISAPPROVE);
126         }
127 
128         return super.canDisapprove(document);
129     }
130 
131     /**
132      * @see org.kuali.rice.krad.document.authorization.DocumentPresentationControllerBase#canRoute(org.kuali.rice.krad.document.Document)
133      */
134     @Override
135     public boolean canRoute(Document document) {
136         WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
137         if (workflowDocument.isInitiated() || workflowDocument.isSaved()) {
138             CashManagementDocument cmDoc = (CashManagementDocument) document;
139             if (!cmDoc.hasFinalDeposit() || !SpringContext.getBean(CashManagementService.class).allVerifiedCashReceiptsAreDeposited(cmDoc)) {
140                 return false;
141             }
142 
143             // CM document can only be routed if it contains a Final Deposit
144             ValidActions validActions = workflowDocument.getValidActions();
145             return validActions.getValidActions().contains(ActionType.ROUTE);
146         }
147 
148         return super.canRoute(document);
149     }
150 
151     /**
152      * @see org.kuali.rice.krad.document.authorization.DocumentPresentationControllerBase#canSave(org.kuali.rice.krad.document.Document)
153      */
154     @Override
155     public boolean canSave(Document document) {
156         WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
157         if (workflowDocument.isInitiated() || workflowDocument.isSaved()) {
158             CashManagementDocument cmDoc = (CashManagementDocument) document;
159             if (cmDoc.getCashDrawerStatus() == null || cmDoc.getCashDrawerStatus().equals(CashDrawerConstants.STATUS_CLOSED)) {
160                 return false;
161             }
162 
163             // CM document can only be saved (via the save button) if the CashDrawer is not closed
164             ValidActions validActions = workflowDocument.getValidActions();
165             return validActions.getValidActions().contains(ActionType.SAVE);
166         }
167 
168         return super.canRoute(document);
169     }
170 
171     /**
172      * @see org.kuali.rice.krad.document.authorization.DocumentPresentationControllerBase#canAdHocRoute(org.kuali.rice.krad.document.Document)
173      */
174     @Override
175     public boolean canAddAdhocRequests(Document document) {
176         WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
177         if (workflowDocument.isEnroute()) {
178             ValidActions validActions = workflowDocument.getValidActions();
179             return validActions.getValidActions().contains(ActionType.ADHOC_REQUEST);
180         }
181 
182         return super.canAddAdhocRequests(document);
183     }
184 
185     /**
186      * Determines if the cash drawer can be opened by testing two things:
187      * <ol>
188      *  <li>That the cash drawer is currently closed.</li>
189      *  <li>That no cash drawer maintenance documents have a lock on the cash drawer.</li>
190      * </ol>
191      * @param document the document that wishes to open the cash drawer
192      * @return true if the cash drawer can be opened, false otherwise
193      */
194     @Override
195     public boolean canOpenCashDrawer(Document document) {
196         final CashDrawer cashDrawer = retrieveCashDrawer(document);
197         return cashDrawer.isClosed() && noExistCashDrawerMaintLocks(cashDrawer, document.getDocumentNumber());
198     }
199 
200     /**
201      * Retrieves the cash drawer associated with the given cash management document
202      * @param document a CashManagementDocument with an associated cash drawer
203      * @return the associated cash drawer
204      */
205     protected CashDrawer retrieveCashDrawer(Document document) {
206         final CashManagementDocument cmDoc = (CashManagementDocument)document;
207         final CashDrawer cashDrawer = SpringContext.getBean(CashDrawerService.class).getByCampusCode(cmDoc.getCampusCode());
208         return cashDrawer;
209     }
210 
211     /**
212      * Determines that no maintenance documents have locks on the given cash drawer
213      * @param cashDrawer the cash drawer that may have locks on it
214      * @return true if there are no maintenance documents with locks on the cash drawer, false otherwise
215      */
216     protected boolean noExistCashDrawerMaintLocks(CashDrawer cashDrawer, String documentNumber) {
217        final org.kuali.rice.krad.datadictionary.MaintenanceDocumentEntry cashDrawerMaintDocEntry = SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getMaintenanceDocumentEntryForBusinessObjectClass(cashDrawer.getClass());
218        org.kuali.rice.krad.maintenance.Maintainable cashDrawerMaintainable = createCashDrawerMaintainable(cashDrawerMaintDocEntry);
219        cashDrawerMaintainable.setDataObjectClass(cashDrawer.getClass());
220        cashDrawerMaintainable.setDataObject(cashDrawer);
221        cashDrawerMaintainable.setDocumentNumber(documentNumber);
222 
223        final String lockingDocument = SpringContext.getBean(MaintenanceDocumentService.class).getLockingDocumentId(cashDrawerMaintainable, documentNumber);
224        return StringUtils.isBlank(lockingDocument);
225     }
226 
227     /**
228      * Builds an instance of the appropriate Maintainable implementation for the Cash Drawer Maintainable
229      * @param cashDrawerMaintenanceDocumentEntry the data dictionary entry from the Cash Drawer's maintenance document
230      * @return an appropriate Maintainable
231      */
232     protected org.kuali.rice.krad.maintenance.Maintainable createCashDrawerMaintainable(org.kuali.rice.krad.datadictionary.MaintenanceDocumentEntry cashDrawerMaintenanceDocumentEntry) {
233         org.kuali.rice.krad.maintenance.Maintainable cashDrawerMaintainable;
234         try {
235             cashDrawerMaintainable = cashDrawerMaintenanceDocumentEntry.getMaintainableClass().newInstance();
236         }
237         catch (InstantiationException ie) {
238             throw new RuntimeException("Cannot instantiate instance of maintainable implementation "+cashDrawerMaintenanceDocumentEntry.getMaintainableClass().getName(), ie);
239         }
240         catch (IllegalAccessException iae) {
241             throw new RuntimeException("Illegal access occurred while instantiating instance of maintainable implementation "+cashDrawerMaintenanceDocumentEntry.getMaintainableClass().getName(), iae);
242         }
243         return cashDrawerMaintainable;
244     }
245 
246     @Override
247     public Set<String> getDocumentActions(Document document) {
248         Set<String> documentActions = super.getDocumentActions(document);
249 
250         if (!canHaveBankEntry(document)) {
251             documentActions.add(OLEConstants.KFS_ACTION_CAN_EDIT_BANK);
252         }
253 
254         return documentActions;
255     }
256 }