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.purap.document.authorization;
017:
018: import java.util.ArrayList;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Map;
022:
023: import org.apache.commons.lang.StringUtils;
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.exceptions.GroupNotFoundException;
029: import org.kuali.core.service.KualiGroupService;
030: import org.kuali.core.util.ObjectUtils;
031: import org.kuali.core.workflow.service.KualiWorkflowDocument;
032: import org.kuali.kfs.KFSConstants;
033: import org.kuali.kfs.context.SpringContext;
034: import org.kuali.kfs.document.authorization.AccountingDocumentAuthorizerBase;
035: import org.kuali.kfs.service.ParameterService;
036: import org.kuali.kfs.service.impl.ParameterConstants;
037: import org.kuali.module.purap.PurapAuthorizationConstants;
038: import org.kuali.module.purap.PurapConstants;
039: import org.kuali.module.purap.PurapParameterConstants;
040: import org.kuali.module.purap.PurapWorkflowConstants.PaymentRequestDocument.NodeDetailEnum;
041: import org.kuali.module.purap.bo.PaymentRequestItem;
042: import org.kuali.module.purap.document.PaymentRequestDocument;
043: import org.kuali.module.purap.service.PurapService;
044:
045: /**
046: * Document Authorizer for the PREQ document.
047: */
048: public class PaymentRequestDocumentAuthorizer extends
049: AccountingDocumentAuthorizerBase {
050:
051: /**
052: * @see org.kuali.core.document.authorization.DocumentAuthorizerBase#hasInitiateAuthorization(org.kuali.core.document.Document,
053: * org.kuali.core.bo.user.UniversalUser)
054: */
055: @Override
056: public boolean hasInitiateAuthorization(Document document,
057: UniversalUser user) {
058: String authorizedWorkgroup = SpringContext
059: .getBean(ParameterService.class)
060: .getParameterValue(
061: ParameterConstants.PURCHASING_DOCUMENT.class,
062: PurapParameterConstants.Workgroups.WORKGROUP_ACCOUNTS_PAYABLE);
063: try {
064: return SpringContext.getBean(KualiGroupService.class)
065: .getByGroupName(authorizedWorkgroup)
066: .hasMember(user);
067: } catch (GroupNotFoundException e) {
068: throw new RuntimeException("Workgroup "
069: + authorizedWorkgroup + " not found", e);
070: }
071: }
072:
073: /**
074: * @see org.kuali.core.document.authorization.DocumentAuthorizerBase#getEditMode(org.kuali.core.document.Document,
075: * org.kuali.core.bo.user.UniversalUser)
076: */
077: @Override
078: public Map getEditMode(Document document, UniversalUser user,
079: List sourceAccountingLines, List targetAccountingLines) {
080: Map editModeMap = super .getEditMode(document, user,
081: sourceAccountingLines, targetAccountingLines);
082: PaymentRequestDocument preq = (PaymentRequestDocument) document;
083:
084: PaymentRequestDocumentActionAuthorizer preqDocAuth = new PaymentRequestDocumentActionAuthorizer(
085: preq);
086:
087: String editMode = AuthorizationConstants.EditMode.VIEW_ONLY;
088:
089: boolean fullDocumentEntryCompleted = SpringContext.getBean(
090: PurapService.class).isFullDocumentEntryCompleted(
091: (PaymentRequestDocument) document);
092:
093: KualiWorkflowDocument workflowDocument = document
094: .getDocumentHeader().getWorkflowDocument();
095: if (workflowDocument.stateIsInitiated()
096: || workflowDocument.stateIsSaved()) {
097: if (hasInitiateAuthorization(document, user)) {
098: editMode = AuthorizationConstants.EditMode.FULL_ENTRY;
099: }
100: } else if (workflowDocument.stateIsEnroute()
101: && workflowDocument.isApprovalRequested()) {
102: List currentRouteLevels = getCurrentRouteLevels(workflowDocument);
103: // only allow full entry if status allows it
104:
105: if (!fullDocumentEntryCompleted) {
106: editMode = AuthorizationConstants.EditMode.FULL_ENTRY;
107: }
108:
109: if (currentRouteLevels
110: .contains(NodeDetailEnum.ACCOUNT_REVIEW.getName())) {
111: editModeMap
112: .remove(AuthorizationConstants.EditMode.FULL_ENTRY);
113: // expense_entry was already added in super
114: // add amount edit mode
115: editMode = PurapAuthorizationConstants.PaymentRequestEditMode.SHOW_AMOUNT_ONLY;
116:
117: List lineList = new ArrayList();
118: for (Iterator iter = preq.getItems().iterator(); iter
119: .hasNext();) {
120: PaymentRequestItem item = (PaymentRequestItem) iter
121: .next();
122: lineList.addAll(item.getSourceAccountingLines());
123: // If FO has deleted the last accounting line for an item, set entry mode to full so they can add another one
124: if (item.getItemType()
125: .isItemTypeAboveTheLineIndicator()
126: && item.getSourceAccountingLines().size() == 0) {
127: editModeMap
128: .remove(AuthorizationConstants.EditMode.VIEW_ONLY);
129: editModeMap
130: .put(
131: AuthorizationConstants.TransactionalEditMode.EXPENSE_ENTRY,
132: "TRUE");
133: }
134: }
135: }
136: }
137:
138: editModeMap.put(editMode, "TRUE");
139:
140: // always show amount after full entry
141: if (fullDocumentEntryCompleted) {
142: editModeMap
143: .put(
144: PurapAuthorizationConstants.PaymentRequestEditMode.SHOW_AMOUNT_ONLY,
145: "TRUE");
146: }
147:
148: // make sure ap user can edit certain fields
149: if (preqDocAuth.canEditPreExtractFields()) {
150: editModeMap
151: .put(
152: PurapAuthorizationConstants.PaymentRequestEditMode.EDIT_PRE_EXTRACT,
153: "TRUE");
154: }
155:
156: PaymentRequestDocument paymentRequestDocument = (PaymentRequestDocument) document;
157: if (StringUtils.equals(paymentRequestDocument.getStatusCode(),
158: PurapConstants.PaymentRequestStatuses.INITIATE)) {
159: editModeMap
160: .put(
161: PurapAuthorizationConstants.PaymentRequestEditMode.DISPLAY_INIT_TAB,
162: "TRUE");
163: } else {
164: editModeMap
165: .put(
166: PurapAuthorizationConstants.PaymentRequestEditMode.DISPLAY_INIT_TAB,
167: "FALSE");
168: }
169: if (ObjectUtils.isNotNull(paymentRequestDocument
170: .getVendorHeaderGeneratedIdentifier())) {
171: editModeMap
172: .put(
173: PurapAuthorizationConstants.PaymentRequestEditMode.LOCK_VENDOR_ENTRY,
174: "TRUE");
175: }
176: return editModeMap;
177: }
178:
179: /**
180: * @see org.kuali.core.document.authorization.DocumentAuthorizer#getDocumentActionFlags(org.kuali.core.document.Document,
181: * org.kuali.core.bo.user.UniversalUser)
182: */
183: @Override
184: public DocumentActionFlags getDocumentActionFlags(
185: Document document, UniversalUser user) {
186: DocumentActionFlags flags = super .getDocumentActionFlags(
187: document, user);
188: KualiWorkflowDocument workflowDocument = document
189: .getDocumentHeader().getWorkflowDocument();
190:
191: if (KFSConstants.SYSTEM_USER.equalsIgnoreCase(user
192: .getPersonUserIdentifier())) {
193: flags.setCanBlanketApprove(true);
194: }
195:
196: PaymentRequestDocument paymentRequestDocument = (PaymentRequestDocument) document;
197: if (StringUtils.equals(paymentRequestDocument.getStatusCode(),
198: PurapConstants.PaymentRequestStatuses.INITIATE)) {
199: flags.setCanSave(false);
200: flags.setCanClose(true);
201: flags.setCanCancel(false);
202: flags.setCanDisapprove(false);
203: } else {
204: if (!getCurrentRouteLevels(workflowDocument).contains(
205: NodeDetailEnum.ACCOUNTS_PAYABLE_REVIEW.getName())) {
206: flags.setCanDisapprove(false);
207: }
208:
209: PaymentRequestDocumentActionAuthorizer preqDocAuth = new PaymentRequestDocumentActionAuthorizer(
210: paymentRequestDocument);
211:
212: flags.setCanApprove(preqDocAuth.canApprove());
213: flags.setCanCancel(preqDocAuth.canCancel());
214: flags.setCanSave(preqDocAuth.canSave());
215: }
216:
217: // NEED TO REDO ANNOTATE CHECK SINCE CHANGED THE VALUE OF FLAGS
218: this.setAnnotateFlag(flags);
219:
220: return flags;
221: }
222:
223: }
|