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;
017
018 import org.apache.commons.lang.StringUtils;
019 import org.kuali.kfs.module.ar.businessobject.InvoiceRecurrence;
020 import org.kuali.kfs.sys.KFSConstants;
021 import org.kuali.kfs.sys.context.SpringContext;
022 import org.kuali.kfs.sys.document.FinancialSystemMaintainable;
023 import org.kuali.kfs.sys.document.FinancialSystemMaintenanceDocument;
024 import org.kuali.rice.kew.exception.WorkflowException;
025 import org.kuali.rice.kim.bo.Person;
026 import org.kuali.rice.kim.bo.entity.KimPrincipal;
027 import org.kuali.rice.kim.service.IdentityManagementService;
028 import org.kuali.rice.kns.service.DocumentService;
029 import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;
030
031 public class InvoiceRecurrenceMaintainable extends FinancialSystemMaintainable {
032 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(InvoiceRecurrenceMaintainable.class);
033
034 private static final String INACTIVATING_NODE_NAME = "InvoiceRecurrenceIsInactivating";
035 private static final String INITIATED_BY_SYSTEM_USER = "InitiatedBySystemUser";
036
037 @Override
038 protected boolean answerSplitNodeQuestion(String nodeName) throws UnsupportedOperationException {
039 // return true if the doc is flipping form Active to Inactive, false otherwise
040 if ( StringUtils.equalsIgnoreCase( INACTIVATING_NODE_NAME, nodeName) ) {
041 // go through some contortions to get the oldMaintainable to compare against
042 FinancialSystemMaintenanceDocument maintDoc = getParentMaintDoc();
043 // make sure all the needed objects are there
044 if ( maintDoc == null
045 || maintDoc.getOldMaintainableObject() == null
046 || maintDoc.getOldMaintainableObject().getBusinessObject() == null ) {
047 return false;
048 }
049 boolean oldIsActive = ((InvoiceRecurrence)maintDoc.getOldMaintainableObject().getBusinessObject()).isActive();
050 boolean newIsActive = ((InvoiceRecurrence)getBusinessObject()).isActive();
051
052 // return true if the invoicerecurrence is being deactivated, otherwise return false
053 return oldIsActive && !newIsActive;
054 }
055
056 // return true if the document was initiated by the SYSTEM_USER, false otherwise
057 if ( StringUtils.equalsIgnoreCase( INITIATED_BY_SYSTEM_USER, nodeName) ) {
058 FinancialSystemMaintenanceDocument maintDoc = getParentMaintDoc();
059 if ( maintDoc == null
060 || maintDoc.getDocumentHeader() == null
061 || maintDoc.getDocumentHeader().getWorkflowDocument() == null
062 || StringUtils.isBlank( maintDoc.getDocumentHeader().getWorkflowDocument().getInitiatorPrincipalId() ) ) {
063 return false;
064 }
065
066 String initiatorPrincipalId = maintDoc.getDocumentHeader().getWorkflowDocument().getInitiatorPrincipalId();
067 KimPrincipal principal = SpringContext.getBean(IdentityManagementService.class).getPrincipal(initiatorPrincipalId);
068
069 return principal != null && StringUtils.equalsIgnoreCase(principal.getPrincipalName(), KFSConstants.SYSTEM_USER);
070 }
071
072 throw new UnsupportedOperationException("InvoiceRecurrenceMaintainable does not implement the answerSplitNodeQuestion method. Node name specified was: " + nodeName);
073
074 }
075
076 protected FinancialSystemMaintenanceDocument getParentMaintDoc() {
077 // how I wish for the ability to directly access the parent object
078 DocumentService documentService = SpringContext.getBean(DocumentService.class);
079 try {
080 return (FinancialSystemMaintenanceDocument) documentService.getByDocumentHeaderId(this.documentNumber);
081 } catch (WorkflowException e) {
082 LOG.error( "Unable to retrieve maintenance document for use in split node routing - returning null",e);
083 }
084 return null;
085 }
086
087 @SuppressWarnings("deprecation")
088 @Override
089 public void processAfterRetrieve() {
090 super.processAfterRetrieve();
091 // Need to make sure that the customer invoice object has been loaded on the new object
092 ((InvoiceRecurrence)getBusinessObject()).setCustomerInvoiceDocument( getBusinessObjectService().findBySinglePrimaryKey(CustomerInvoiceDocument.class, ((InvoiceRecurrence)getBusinessObject()).getInvoiceNumber() ));
093 }
094
095 }