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.rules;
017:
018: import static org.kuali.kfs.KFSConstants.GL_DEBIT_CODE;
019:
020: import org.apache.commons.lang.StringUtils;
021: import org.kuali.core.bo.user.UniversalUser;
022: import org.kuali.core.document.Document;
023: import org.kuali.core.exceptions.UserNotFoundException;
024: import org.kuali.core.exceptions.ValidationException;
025: import org.kuali.core.rule.event.ApproveDocumentEvent;
026: import org.kuali.core.service.UniversalUserService;
027: import org.kuali.core.util.GlobalVariables;
028: import org.kuali.core.util.ObjectUtils;
029: import org.kuali.kfs.KFSConstants;
030: import org.kuali.kfs.bo.AccountingLine;
031: import org.kuali.kfs.bo.GeneralLedgerPendingEntry;
032: import org.kuali.kfs.context.SpringContext;
033: import org.kuali.kfs.document.AccountingDocument;
034: import org.kuali.kfs.service.ParameterService;
035: import org.kuali.kfs.service.impl.ParameterConstants;
036: import org.kuali.module.purap.PurapConstants;
037: import org.kuali.module.purap.PurapKeyConstants;
038: import org.kuali.module.purap.PurapParameterConstants;
039: import org.kuali.module.purap.PurapPropertyConstants;
040: import org.kuali.module.purap.PurapConstants.PurapDocTypeCodes;
041: import org.kuali.module.purap.document.PurchaseOrderDocument;
042: import org.kuali.module.purap.service.PurapGeneralLedgerService;
043: import org.kuali.module.purap.service.PurchaseOrderService;
044:
045: /**
046: * Rules for Purchase Order Reopen document creation.
047: */
048: public class PurchaseOrderReopenDocumentRule extends
049: PurchasingDocumentRuleBase {
050:
051: /**
052: * @see org.kuali.module.financial.rules.TransactionalDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.core.document.Document)
053: */
054: @Override
055: protected boolean processCustomRouteDocumentBusinessRules(
056: Document document) {
057: boolean isValid = true;
058: PurchaseOrderDocument porDocument = (PurchaseOrderDocument) document;
059: return isValid &= processValidation(porDocument);
060: }
061:
062: /**
063: * @see org.kuali.core.rules.DocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.core.document.Document)
064: */
065: @Override
066: protected boolean processCustomSaveDocumentBusinessRules(
067: Document document) {
068: boolean isValid = true;
069: PurchaseOrderDocument porDocument = (PurchaseOrderDocument) document;
070: return isValid &= processValidation(porDocument);
071: }
072:
073: /**
074: * @see org.kuali.core.rules.DocumentRuleBase#processCustomApproveDocumentBusinessRules(org.kuali.core.rule.event.ApproveDocumentEvent)
075: */
076: @Override
077: protected boolean processCustomApproveDocumentBusinessRules(
078: ApproveDocumentEvent approveEvent) {
079: boolean isValid = true;
080: PurchaseOrderDocument porDocument = (PurchaseOrderDocument) approveEvent
081: .getDocument();
082: return isValid &= processValidation(porDocument);
083: }
084:
085: /**
086: * Central method to control the processing of rule checks. Checks that the purchase order document is not null, that it is in
087: * the correct status, and that the user is in the purchasing workgroup.
088: *
089: * @param document A PurchaseOrderDocument. (A PurchasePaymentCloseDocument at this point.)
090: * @return True if the document passes all the validations.
091: */
092: private boolean processValidation(PurchaseOrderDocument document) {
093: boolean valid = true;
094:
095: // Check that the PO is not null
096: if (ObjectUtils.isNull(document)) {
097: throw new ValidationException(
098: "Purchase Order Reopen document was null on validation.");
099: } else {
100: // Check the PO status.
101: PurchaseOrderDocument currentPO = SpringContext.getBean(
102: PurchaseOrderService.class)
103: .getCurrentPurchaseOrder(
104: document.getPurapDocumentIdentifier());
105: if (!StringUtils.equalsIgnoreCase(
106: currentPO.getStatusCode(),
107: PurapConstants.PurchaseOrderStatuses.CLOSED)
108: && !StringUtils
109: .equalsIgnoreCase(
110: currentPO.getStatusCode(),
111: PurapConstants.PurchaseOrderStatuses.PENDING_REOPEN)) {
112: valid = false;
113: GlobalVariables
114: .getErrorMap()
115: .putError(
116: PurapPropertyConstants.STATUS_CODE,
117: PurapKeyConstants.ERROR_PURCHASE_ORDER_STATUS_INCORRECT,
118: PurapConstants.PurchaseOrderStatuses.CLOSED);
119: }
120:
121: // Check that the user is in purchasing workgroup.
122: String initiatorNetworkId = document.getDocumentHeader()
123: .getWorkflowDocument().getInitiatorNetworkId();
124: UniversalUserService uus = SpringContext
125: .getBean(UniversalUserService.class);
126: UniversalUser user = null;
127: try {
128: user = uus
129: .getUniversalUserByAuthenticationUserId(initiatorNetworkId);
130: String purchasingGroup = SpringContext
131: .getBean(ParameterService.class)
132: .getParameterValue(
133: ParameterConstants.PURCHASING_DOCUMENT.class,
134: PurapParameterConstants.Workgroups.WORKGROUP_PURCHASING);
135: if (!uus.isMember(user, purchasingGroup)) {
136: valid = false;
137: GlobalVariables.getErrorMap().putError(
138: PurapPropertyConstants.PURAP_DOC_ID,
139: PurapKeyConstants.ERROR_USER_NONPURCHASING);
140: }
141: } catch (UserNotFoundException ue) {
142: valid = false;
143: }
144: }
145: return valid;
146: }
147:
148: /**
149: * @see org.kuali.module.purap.rules.PurapAccountingDocumentRuleBase#customizeExplicitGeneralLedgerPendingEntry(org.kuali.kfs.document.AccountingDocument,
150: * org.kuali.kfs.bo.AccountingLine, org.kuali.kfs.bo.GeneralLedgerPendingEntry)
151: */
152: @Override
153: protected void customizeExplicitGeneralLedgerPendingEntry(
154: AccountingDocument accountingDocument,
155: AccountingLine accountingLine,
156: GeneralLedgerPendingEntry explicitEntry) {
157: super .customizeExplicitGeneralLedgerPendingEntry(
158: accountingDocument, accountingLine, explicitEntry);
159: PurchaseOrderDocument po = (PurchaseOrderDocument) accountingDocument;
160: SpringContext.getBean(PurapGeneralLedgerService.class)
161: .customizeGeneralLedgerPendingEntry(po, accountingLine,
162: explicitEntry, po.getPurapDocumentIdentifier(),
163: GL_DEBIT_CODE, PurapDocTypeCodes.PO_DOCUMENT,
164: true);
165:
166: // don't think i should have to override this, but default isn't getting the right PO doc
167: explicitEntry
168: .setFinancialDocumentTypeCode(PurapDocTypeCodes.PO_REOPEN_DOCUMENT);
169: explicitEntry
170: .setFinancialDocumentApprovedCode(KFSConstants.PENDING_ENTRY_APPROVED_STATUS_CODE.APPROVED);
171: }
172:
173: }
|