001 /* 002 * Copyright 2011 The Kuali Foundation. 003 * 004 * Licensed under the Educational Community License, Version 2.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/ecl2.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.kfs.module.ar.document.validation.impl; 017 018 import static org.kuali.kfs.sys.document.validation.impl.AccountingDocumentRuleBaseConstants.ERROR_PATH.DOCUMENT_ERROR_PREFIX; 019 020 import java.util.Collection; 021 import java.util.HashMap; 022 import java.util.Map; 023 024 import org.apache.commons.lang.StringUtils; 025 import org.kuali.kfs.module.ar.ArConstants; 026 import org.kuali.kfs.module.ar.ArKeyConstants; 027 import org.kuali.kfs.module.ar.ArPropertyConstants; 028 import org.kuali.kfs.module.ar.document.CustomerCreditMemoDocument; 029 import org.kuali.kfs.module.ar.document.CustomerInvoiceDocument; 030 import org.kuali.kfs.module.ar.document.service.CustomerInvoiceDocumentService; 031 import org.kuali.kfs.sys.context.SpringContext; 032 import org.kuali.kfs.sys.document.validation.GenericValidation; 033 import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent; 034 import org.kuali.rice.kew.exception.WorkflowException; 035 import org.kuali.rice.kim.bo.Person; 036 import org.kuali.rice.kns.exception.UnknownDocumentIdException; 037 import org.kuali.rice.kns.service.BusinessObjectService; 038 import org.kuali.rice.kns.util.GlobalVariables; 039 import org.kuali.rice.kns.util.ObjectUtils; 040 import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument; 041 import org.kuali.rice.kns.workflow.service.WorkflowDocumentService; 042 043 public class CustomerCreditMemoNoOtherCRMInRouteForTheInvoiceValidation extends GenericValidation { 044 045 private CustomerCreditMemoDocument customerCreditMemoDocument; 046 private BusinessObjectService businessObjectService; 047 private WorkflowDocumentService workflowDocumentService; 048 049 public boolean validate(AttributedDocumentEvent event) { 050 051 String invoiceDocumentNumber = customerCreditMemoDocument.getFinancialDocumentReferenceInvoiceNumber(); 052 KualiWorkflowDocument workflowDocument; 053 boolean success = true; 054 055 Map<String, String> fieldValues = new HashMap<String, String>(); 056 fieldValues.put("financialDocumentReferenceInvoiceNumber", invoiceDocumentNumber); 057 058 BusinessObjectService businessObjectService = SpringContext.getBean(BusinessObjectService.class); 059 Collection<CustomerCreditMemoDocument> customerCreditMemoDocuments = 060 businessObjectService.findMatching(CustomerCreditMemoDocument.class, fieldValues); 061 062 // no CRMs associated with the invoice are found 063 if (customerCreditMemoDocuments.isEmpty()) 064 return true; 065 066 Person user = GlobalVariables.getUserSession().getPerson(); 067 068 for(CustomerCreditMemoDocument customerCreditMemoDocument : customerCreditMemoDocuments) { 069 try { 070 workflowDocument = SpringContext.getBean(WorkflowDocumentService.class).createWorkflowDocument(Long.valueOf(customerCreditMemoDocument.getDocumentNumber()), user); 071 } 072 catch (WorkflowException e) { 073 throw new UnknownDocumentIdException("no document found for documentHeaderId '" + customerCreditMemoDocument.getDocumentNumber() + "'", e); 074 } 075 076 if (!(workflowDocument.stateIsApproved() || workflowDocument.stateIsProcessed() || workflowDocument.stateIsCanceled() || workflowDocument.stateIsDisapproved())) { 077 GlobalVariables.getMessageMap().putError(ArPropertyConstants.CustomerCreditMemoDocumentFields.CREDIT_MEMO_DOCUMENT_REF_INVOICE_NUMBER, ArKeyConstants.ERROR_CUSTOMER_CREDIT_MEMO_DOCUMENT_ONE_CRM_IN_ROUTE_PER_INVOICE); 078 return false; 079 } 080 } 081 return true; 082 083 } 084 085 public CustomerCreditMemoDocument getCustomerCreditMemoDocument() { 086 return customerCreditMemoDocument; 087 } 088 089 public void setCustomerCreditMemoDocument(CustomerCreditMemoDocument customerCreditMemoDocument) { 090 this.customerCreditMemoDocument = customerCreditMemoDocument; 091 } 092 093 public WorkflowDocumentService getWorkflowDocumentService() { 094 return workflowDocumentService; 095 } 096 097 public void setWorkflowDocumentService(WorkflowDocumentService workflowDocumentService) { 098 this.workflowDocumentService = workflowDocumentService; 099 } 100 101 public BusinessObjectService getBusinessObjectService() { 102 return businessObjectService; 103 } 104 105 public void setBusinessObjectService(BusinessObjectService businessObjectService) { 106 this.businessObjectService = businessObjectService; 107 } 108 109 }