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