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 org.apache.commons.lang.StringUtils;
019: import org.kuali.core.bo.user.UniversalUser;
020: import org.kuali.core.document.Document;
021: import org.kuali.core.exceptions.UserNotFoundException;
022: import org.kuali.core.exceptions.ValidationException;
023: import org.kuali.core.rule.event.ApproveDocumentEvent;
024: import org.kuali.core.rules.TransactionalDocumentRuleBase;
025: import org.kuali.core.service.UniversalUserService;
026: import org.kuali.core.util.GlobalVariables;
027: import org.kuali.core.util.ObjectUtils;
028: import org.kuali.kfs.context.SpringContext;
029: import org.kuali.kfs.service.ParameterService;
030: import org.kuali.kfs.service.impl.ParameterConstants;
031: import org.kuali.module.purap.PurapKeyConstants;
032: import org.kuali.module.purap.PurapParameterConstants;
033: import org.kuali.module.purap.PurapPropertyConstants;
034: import org.kuali.module.purap.PurapConstants.PurchaseOrderStatuses;
035: import org.kuali.module.purap.document.PurchaseOrderDocument;
036: import org.kuali.module.purap.service.PurchaseOrderService;
037:
038: /**
039: * Rules for Purchase Order Remove Hold document creation. Should not extend <code>PurchaseOrderDocumentRule</code>, since it
040: * does not allow the purchase order to be edited, nor should it create GL entries.
041: */
042: public class PurchaseOrderRemoveHoldDocumentRule extends
043: TransactionalDocumentRuleBase {
044:
045: /**
046: * @see org.kuali.module.financial.rules.TransactionalDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.core.document.Document)
047: */
048: @Override
049: protected boolean processCustomRouteDocumentBusinessRules(
050: Document document) {
051: boolean isValid = true;
052: PurchaseOrderDocument porDocument = (PurchaseOrderDocument) document;
053: return isValid &= processValidation(porDocument);
054: }
055:
056: /**
057: * @see org.kuali.core.rules.DocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.core.document.Document)
058: */
059: @Override
060: protected boolean processCustomSaveDocumentBusinessRules(
061: Document document) {
062: boolean isValid = true;
063: PurchaseOrderDocument porDocument = (PurchaseOrderDocument) document;
064: return isValid &= processValidation(porDocument);
065: }
066:
067: /**
068: * @see org.kuali.core.rules.DocumentRuleBase#processCustomApproveDocumentBusinessRules(org.kuali.core.rule.event.ApproveDocumentEvent)
069: */
070: @Override
071: protected boolean processCustomApproveDocumentBusinessRules(
072: ApproveDocumentEvent approveEvent) {
073: boolean isValid = true;
074: PurchaseOrderDocument porDocument = (PurchaseOrderDocument) approveEvent
075: .getDocument();
076: return isValid;
077: }
078:
079: /**
080: * Central method to control the processing of rule checks. Checks that the purchase order document is not null, that it is in
081: * the correct status, and checks that the user is in the correct user group.
082: *
083: * @param document A PurchaseOrderDocument. (A PurchaseOrderPaymentHoldDocument at this point.)
084: * @return True if the document passes all the validations.
085: */
086: boolean processValidation(PurchaseOrderDocument document) {
087: boolean valid = true;
088:
089: // Check that the PO is not null
090: if (ObjectUtils.isNull(document)) {
091: throw new ValidationException(
092: "Purchase Order Remove Hold document was null on validation.");
093: } else {
094: PurchaseOrderDocument currentPO = SpringContext.getBean(
095: PurchaseOrderService.class)
096: .getCurrentPurchaseOrder(
097: document.getPurapDocumentIdentifier());
098: // Check the PO status
099: if (!StringUtils.equalsIgnoreCase(
100: currentPO.getStatusCode(),
101: PurchaseOrderStatuses.PAYMENT_HOLD)
102: && !StringUtils.equalsIgnoreCase(currentPO
103: .getStatusCode(),
104: PurchaseOrderStatuses.PENDING_REMOVE_HOLD)) {
105: valid = false;
106: GlobalVariables
107: .getErrorMap()
108: .putError(
109: PurapPropertyConstants.STATUS_CODE,
110: PurapKeyConstants.ERROR_PURCHASE_ORDER_STATUS_NOT_REQUIRED_STATUS,
111: PurchaseOrderStatuses.PAYMENT_HOLD);
112: }
113:
114: // Check that the user is in purchasing workgroup.
115: String initiatorNetworkId = document.getDocumentHeader()
116: .getWorkflowDocument().getInitiatorNetworkId();
117: UniversalUserService uus = SpringContext
118: .getBean(UniversalUserService.class);
119: UniversalUser user = null;
120: try {
121: user = uus
122: .getUniversalUserByAuthenticationUserId(initiatorNetworkId);
123: String purchasingGroup = SpringContext
124: .getBean(ParameterService.class)
125: .getParameterValue(
126: ParameterConstants.PURCHASING_DOCUMENT.class,
127: PurapParameterConstants.Workgroups.WORKGROUP_PURCHASING);
128: if (!uus.isMember(user, purchasingGroup)) {
129: valid = false;
130: }
131: } catch (UserNotFoundException ue) {
132: valid = false;
133: }
134: }
135: return valid;
136: }
137: }
|