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.cam.document.validation.impl; 017 018 import java.util.ArrayList; 019 import java.util.Arrays; 020 import java.util.List; 021 022 import org.apache.commons.lang.StringUtils; 023 import org.kuali.kfs.module.cam.CamsConstants; 024 import org.kuali.kfs.module.cam.CamsKeyConstants; 025 import org.kuali.kfs.module.cam.CamsPropertyConstants; 026 import org.kuali.kfs.module.cam.businessobject.Asset; 027 import org.kuali.kfs.module.cam.businessobject.AssetGlobal; 028 import org.kuali.kfs.module.cam.businessobject.AssetPayment; 029 import org.kuali.kfs.module.cam.businessobject.AssetPaymentAssetDetail; 030 import org.kuali.kfs.module.cam.businessobject.AssetPaymentDetail; 031 import org.kuali.kfs.module.cam.document.AssetPaymentDocument; 032 import org.kuali.kfs.module.cam.document.service.AssetService; 033 import org.kuali.kfs.sys.KFSConstants; 034 import org.kuali.kfs.sys.context.SpringContext; 035 import org.kuali.rice.kns.document.Document; 036 import org.kuali.rice.kns.rules.PromptBeforeValidationBase; 037 import org.kuali.rice.kns.service.KualiConfigurationService; 038 import org.kuali.rice.kns.service.ParameterService; 039 import org.kuali.rice.kns.util.ObjectUtils; 040 041 /** 042 * This prerule is ex.. 043 */ 044 public class AssetPaymentDocumentPreRules extends PromptBeforeValidationBase { 045 protected static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AssetPaymentDocumentPreRules.class); 046 047 @Override 048 public boolean doPrompts(Document document) { 049 if (hasDifferentObjectSubTypes((AssetPaymentDocument) document)) { 050 if (!isOkHavingDifferentObjectSubTypes()) { 051 event.setActionForwardName(KFSConstants.MAPPING_BASIC); 052 return false; 053 } 054 } 055 return true; 056 } 057 058 /** 059 * This method determines whether or not an asset has different object sub type codes in its documents. 060 * 061 * @return true when the asset has payments with object codes that point to different object sub type codes 062 */ 063 public boolean hasDifferentObjectSubTypes(AssetPaymentDocument document) { 064 //This method will only execute if the document is being submitted. 065 if (!(document.getDocumentHeader().getWorkflowDocument().stateIsSaved() || document.getDocumentHeader().getWorkflowDocument().stateIsInitiated())) { 066 return false; 067 } 068 069 List<String> subTypes = new ArrayList<String>(); 070 subTypes = SpringContext.getBean(ParameterService.class).getParameterValues(Asset.class, CamsConstants.Parameters.OBJECT_SUB_TYPE_GROUPS); 071 072 List<AssetPaymentDetail> assetPaymentDetails = document.getSourceAccountingLines(); 073 List<String> validObjectSubTypes = new ArrayList<String>(); 074 075 String objectSubTypeCode = null; 076 077 /* 078 * Expected system parameter elements (object sub types). [BD,BF] [CM,CF,CO] [UC,UF,UO] [LI,LF] 079 */ 080 081 // Getting the list of object sub type codes from the asset payments on the jsp. 082 List<String> objectSubTypeList = new ArrayList<String>(); 083 for (AssetPaymentDetail assetPaymentDetail : assetPaymentDetails) { 084 assetPaymentDetail.refreshReferenceObject(CamsPropertyConstants.AssetPaymentDetail.OBJECT_CODE); 085 if (ObjectUtils.isNull(assetPaymentDetail.getObjectCode())) { 086 return false; 087 } 088 objectSubTypeList.add(assetPaymentDetail.getObjectCode().getFinancialObjectSubTypeCode()); 089 } 090 091 if (!getAssetService().isObjectSubTypeCompatible(objectSubTypeList)) { 092 return true; 093 } 094 095 List<AssetPaymentAssetDetail> assetPaymentAssetDetails = document.getAssetPaymentAssetDetail(); 096 for (AssetPaymentAssetDetail assetPaymentAssetDetail : assetPaymentAssetDetails) { 097 assetPaymentAssetDetail.getAsset().refreshReferenceObject(CamsPropertyConstants.Asset.ASSET_PAYMENTS); 098 List<AssetPayment> assetPayments = assetPaymentAssetDetail.getAsset().getAssetPayments(); 099 100 // Comparing against the already approved asset payments 101 if (!assetPayments.isEmpty()) { 102 for (AssetPayment assetPayment : assetPayments) { 103 String paymentSubObjectType = assetPayment.getFinancialObject().getFinancialObjectSubTypeCode(); 104 105 validObjectSubTypes = new ArrayList<String>(); 106 for (String subType : subTypes) { 107 validObjectSubTypes = Arrays.asList(StringUtils.split(subType, ",")); 108 if (validObjectSubTypes.contains(paymentSubObjectType)) { 109 break; 110 } 111 validObjectSubTypes = new ArrayList<String>(); 112 } 113 if (validObjectSubTypes.isEmpty()) 114 validObjectSubTypes.add(paymentSubObjectType); 115 116 // Comparing the same asset payment document 117 for (AssetPaymentDetail assetPaymentDetail : assetPaymentDetails) { 118 if (!validObjectSubTypes.contains(assetPaymentDetail.getObjectCode().getFinancialObjectSubTypeCode())) { 119 // Differences where found. 120 return true; 121 } 122 } 123 } 124 } 125 } 126 // If none object sub types are different... 127 return false; 128 } 129 130 protected boolean isOkHavingDifferentObjectSubTypes() { 131 String parameterDetail = "(module:" + getParameterService().getNamespace(AssetGlobal.class) + "/component:" + getParameterService().getDetailType(AssetGlobal.class) + ")"; 132 KualiConfigurationService kualiConfiguration = SpringContext.getBean(KualiConfigurationService.class); 133 134 String continueQuestion = kualiConfiguration.getPropertyString(CamsKeyConstants.CONTINUE_QUESTION); 135 String warningMessage = kualiConfiguration.getPropertyString(CamsKeyConstants.Payment.WARNING_NOT_SAME_OBJECT_SUB_TYPES) + " " + CamsConstants.Parameters.OBJECT_SUB_TYPE_GROUPS + " " + parameterDetail + ". " + continueQuestion; 136 return super.askOrAnalyzeYesNoQuestion(CamsConstants.AssetPayment.ASSET_PAYMENT_DIFFERENT_OBJECT_SUB_TYPE_CONFIRMATION_QUESTION, warningMessage); 137 } 138 139 protected AssetService getAssetService() { 140 return SpringContext.getBean(AssetService.class); 141 } 142 143 protected ParameterService getParameterService() { 144 return SpringContext.getBean(ParameterService.class); 145 } 146 }