001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.financial.document.authorization;
017:
018: import java.util.HashMap;
019: import java.util.List;
020: import java.util.Map;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.kuali.core.authorization.AuthorizationConstants;
025: import org.kuali.core.bo.user.UniversalUser;
026: import org.kuali.core.document.Document;
027: import org.kuali.core.document.authorization.DocumentActionFlags;
028: import org.kuali.core.document.authorization.DocumentAuthorizerBase;
029: import org.kuali.core.exceptions.DocumentInitiationAuthorizationException;
030: import org.kuali.core.exceptions.DocumentTypeAuthorizationException;
031: import org.kuali.core.exceptions.GroupNotFoundException;
032: import org.kuali.core.service.KualiGroupService;
033: import org.kuali.core.workflow.service.KualiWorkflowDocument;
034: import org.kuali.kfs.KFSConstants;
035: import org.kuali.kfs.KFSConstants.CashDrawerConstants;
036: import org.kuali.kfs.context.SpringContext;
037: import org.kuali.module.financial.document.CashManagementDocument;
038: import org.kuali.module.financial.document.CashReceiptDocument;
039: import org.kuali.module.financial.service.CashManagementService;
040: import org.kuali.module.financial.service.CashReceiptService;
041:
042: import edu.iu.uis.eden.EdenConstants;
043: import edu.iu.uis.eden.clientapp.vo.ValidActionsVO;
044:
045: /**
046: * DocumentAuthorizer containing authorization code for CashManagement documents
047: */
048: public class CashManagementDocumentAuthorizer extends
049: DocumentAuthorizerBase {
050: private static Log LOG = LogFactory
051: .getLog(CashManagementDocumentAuthorizer.class);
052:
053: /**
054: * Overrides to implemented some document specific views.
055: *
056: * @see org.kuali.core.authorization.DocumentAuthorizer#getEditMode(org.kuali.core.document.Document,
057: * org.kuali.core.bo.user.KualiUser)
058: */
059: @Override
060: public Map getEditMode(Document document, UniversalUser user) {
061: // default is UNVIEWABLE for this doctype
062: Map editModeMap = new HashMap();
063: editModeMap
064: .put(
065: AuthorizationConstants.CashManagementEditMode.UNVIEWABLE,
066: "TRUE");
067:
068: // update editMode if possible
069: try {
070: CashManagementDocument cmDoc = (CashManagementDocument) document;
071:
072: if (SpringContext.getBean(KualiGroupService.class)
073: .getByGroupName(cmDoc.getWorkgroupName())
074: .hasMember(user)) {
075: editModeMap.clear();
076:
077: KualiWorkflowDocument workflowDocument = document
078: .getDocumentHeader().getWorkflowDocument();
079:
080: if (workflowDocument.stateIsInitiated()) {
081: editModeMap
082: .put(
083: AuthorizationConstants.CashManagementEditMode.FULL_ENTRY,
084: "TRUE");
085: } else if (workflowDocument.stateIsSaved()) {
086: editModeMap
087: .put(
088: AuthorizationConstants.CashManagementEditMode.FULL_ENTRY,
089: "TRUE");
090:
091: editModeMap
092: .put(
093: AuthorizationConstants.CashManagementEditMode.ALLOW_CANCEL_DEPOSITS,
094: "TRUE");
095: if (!cmDoc.hasFinalDeposit()) {
096: editModeMap
097: .put(
098: AuthorizationConstants.CashManagementEditMode.ALLOW_ADDITIONAL_DEPOSITS,
099: "TRUE");
100: }
101: } else {
102: editModeMap
103: .put(
104: AuthorizationConstants.CashManagementEditMode.VIEW_ONLY,
105: "TRUE");
106: }
107: }
108: } catch (GroupNotFoundException e) {
109: // leave editModeMap UNVIEWABLE
110: }
111:
112: return editModeMap;
113: }
114:
115: /**
116: * @see org.kuali.core.document.DocumentAuthorizerBase#getDocumentActionFlags(org.kuali.core.document.Document,
117: * org.kuali.core.bo.user.KualiUser)
118: */
119: @Override
120: public DocumentActionFlags getDocumentActionFlags(
121: Document document, UniversalUser user) {
122: DocumentActionFlags flags = super .getDocumentActionFlags(
123: document, user);
124:
125: CashManagementDocument cmDoc = (CashManagementDocument) document;
126: KualiWorkflowDocument workflowDocument = document
127: .getDocumentHeader().getWorkflowDocument();
128: ValidActionsVO validActions = workflowDocument.getRouteHeader()
129: .getValidActions();
130:
131: if (workflowDocument.stateIsInitiated()
132: || workflowDocument.stateIsSaved()) {
133: // CM document can only be saved (via the save button) if the CashDrawer is not closed
134: if (cmDoc.getCashDrawerStatus() == null
135: || cmDoc.getCashDrawerStatus().equals(
136: CashDrawerConstants.STATUS_CLOSED)) {
137: flags.setCanSave(false);
138: } else {
139: flags.setCanSave(validActions
140: .contains(EdenConstants.ACTION_TAKEN_SAVED_CD));
141: }
142:
143: // CM document can only be routed if it contains a Final Deposit
144: if (!cmDoc.hasFinalDeposit()
145: || !SpringContext.getBean(
146: CashManagementService.class)
147: .allVerifiedCashReceiptsAreDeposited(cmDoc)) {
148: flags.setCanRoute(false);
149: flags.setCanBlanketApprove(false);
150: } else {
151: flags
152: .setCanRoute(validActions
153: .contains(EdenConstants.ACTION_TAKEN_ROUTED_CD));
154: flags
155: .setCanBlanketApprove(validActions
156: .contains(EdenConstants.ACTION_TAKEN_BLANKET_APPROVE_CD));
157: }
158:
159: if (!SpringContext.getBean(CashManagementService.class)
160: .allowDocumentCancellation(cmDoc)) {
161: flags.setCanCancel(false);
162: } else {
163: flags
164: .setCanCancel(validActions
165: .contains(EdenConstants.ACTION_TAKEN_CANCELED_CD));
166: }
167: }
168:
169: if (workflowDocument.stateIsEnroute()) {
170: flags.setCanApprove(validActions
171: .contains(EdenConstants.ACTION_TAKEN_APPROVED_CD));
172: flags.setCanDisapprove(validActions
173: .contains(EdenConstants.ACTION_TAKEN_DENIED_CD));
174: flags.setCanFYI(validActions
175: .contains(EdenConstants.ACTION_TAKEN_FYI_CD));
176: }
177:
178: return flags;
179: }
180:
181: /**
182: * This method checks that all verified cash receipts are deposited
183: *
184: * @param cmDoc the cash management document to check
185: * @return true if all verified cash receipts are deposited, false if otherwise
186: */
187: private boolean areAllVerifiedCashReceiptsDeposited(
188: CashManagementDocument cmDoc) {
189: boolean theyAre = true;
190: List verifiedReceipts = SpringContext.getBean(
191: CashReceiptService.class).getCashReceipts(
192: cmDoc.getWorkgroupName(),
193: KFSConstants.DocumentStatusCodes.CashReceipt.VERIFIED);
194: CashManagementService cms = SpringContext
195: .getBean(CashManagementService.class);
196: for (Object o : verifiedReceipts) {
197: if (!cms.verifyCashReceiptIsDeposited(cmDoc,
198: (CashReceiptDocument) o)) {
199: theyAre = false;
200: break;
201: }
202: }
203: return theyAre;
204: }
205:
206: /**
207: * @see org.kuali.core.document.DocumentAuthorizerBase#canInitiate(java.lang.String, org.kuali.core.bo.user.KualiUser)
208: */
209: @Override
210: public void canInitiate(String documentTypeName, UniversalUser user)
211: throws DocumentTypeAuthorizationException {
212: try {
213: super .canInitiate(documentTypeName, user);
214: // to initiate, you have to be a member of the bursar's group for your campus
215: String unitName = SpringContext.getBean(
216: CashReceiptService.class)
217: .getCashReceiptVerificationUnitForUser(user);
218: if (!user.isMember(unitName)) {
219: throw new DocumentTypeAuthorizationException(user
220: .getPersonUserIdentifier(), "initiate",
221: documentTypeName);
222: }
223: } catch (DocumentInitiationAuthorizationException e) {
224: throw new DocumentTypeAuthorizationException(user
225: .getPersonUserIdentifier(), "add deposits to",
226: documentTypeName);
227: }
228: }
229: }
|