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.List;
020    
021    import org.kuali.kfs.coa.document.validation.impl.MaintenancePreRulesBase;
022    import org.kuali.kfs.module.cam.CamsConstants;
023    import org.kuali.kfs.module.cam.CamsKeyConstants;
024    import org.kuali.kfs.module.cam.CamsPropertyConstants;
025    import org.kuali.kfs.module.cam.businessobject.AssetGlobal;
026    import org.kuali.kfs.module.cam.businessobject.AssetPaymentDetail;
027    import org.kuali.kfs.module.cam.document.service.AssetService;
028    import org.kuali.kfs.sys.KFSConstants;
029    import org.kuali.kfs.sys.context.SpringContext;
030    import org.kuali.rice.kns.document.MaintenanceDocument;
031    import org.kuali.rice.kns.service.KualiConfigurationService;
032    import org.kuali.rice.kns.service.ParameterService;
033    import org.kuali.rice.kns.util.ObjectUtils;
034    
035    public class AssetGlobalPreRules extends MaintenancePreRulesBase {
036        protected static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AssetGlobalPreRules.class);
037    
038        /**
039         * Sets up a convenience object and few other Asset attributes
040         * 
041         * @see org.kuali.kfs.coa.document.validation.impl.MaintenancePreRulesBase#doCustomPreRules(org.kuali.rice.kns.document.MaintenanceDocument)
042         */
043        @Override
044        protected boolean doCustomPreRules(MaintenanceDocument document) {
045            if (hasDifferentObjectSubTypes(document)) {
046                if (!isOkHavingDifferentObjectSubTypes()) {
047                    event.setActionForwardName(KFSConstants.MAPPING_BASIC);
048                    return false;
049                }
050            }
051            return true;
052        }
053    
054    
055        /**
056         * Validate all object sub type codes are from the same group.
057         * 
058         * @param assetGlobal
059         * @param newLine
060         * @return
061         */
062        public boolean hasDifferentObjectSubTypes(MaintenanceDocument document) {
063            AssetGlobal assetGlobal = (AssetGlobal) document.getNewMaintainableObject().getBusinessObject();
064    
065            boolean invalid = false;
066            List<String> objectSubTypeList = new ArrayList<String>();
067    
068            for (AssetPaymentDetail assetPaymentDetail : assetGlobal.getAssetPaymentDetails()) {
069                assetPaymentDetail.refreshReferenceObject(CamsPropertyConstants.AssetPaymentDetail.OBJECT_CODE);
070                if (ObjectUtils.isNotNull(assetPaymentDetail.getObjectCode())) {
071                    objectSubTypeList.add(assetPaymentDetail.getObjectCode().getFinancialObjectSubTypeCode());
072                }
073            }
074            if (!getAssetService().isObjectSubTypeCompatible(objectSubTypeList)) {
075                invalid = true;
076            }
077            return invalid;
078        }
079    
080    
081        protected boolean isOkHavingDifferentObjectSubTypes() {
082            String parameterDetail = "(module:" + getParameterService().getNamespace(AssetGlobal.class) + "/component:" + getParameterService().getDetailType(AssetGlobal.class) + ")";
083            KualiConfigurationService kualiConfiguration = SpringContext.getBean(KualiConfigurationService.class);
084    
085            String continueQuestion = kualiConfiguration.getPropertyString(CamsKeyConstants.CONTINUE_QUESTION);
086            String warningMessage = kualiConfiguration.getPropertyString(CamsKeyConstants.Payment.WARNING_NOT_SAME_OBJECT_SUB_TYPES) + " " + CamsConstants.Parameters.OBJECT_SUB_TYPE_GROUPS + " " + parameterDetail + ". " + continueQuestion;
087            return super.askOrAnalyzeYesNoQuestion(CamsConstants.AssetPayment.ASSET_PAYMENT_DIFFERENT_OBJECT_SUB_TYPE_CONFIRMATION_QUESTION, warningMessage);
088        }
089    
090        protected AssetService getAssetService() {
091            return SpringContext.getBean(AssetService.class);
092        }
093    
094        protected ParameterService getParameterService() {
095            return SpringContext.getBean(ParameterService.class);
096        }
097    }