View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.cam.document.validation.impl;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.kuali.kfs.coa.document.validation.impl.MaintenancePreRulesBase;
25  import org.kuali.kfs.module.cam.CamsConstants;
26  import org.kuali.kfs.module.cam.CamsKeyConstants;
27  import org.kuali.kfs.module.cam.CamsPropertyConstants;
28  import org.kuali.kfs.module.cam.businessobject.AssetGlobal;
29  import org.kuali.kfs.module.cam.businessobject.AssetPaymentDetail;
30  import org.kuali.kfs.module.cam.document.service.AssetService;
31  import org.kuali.kfs.sys.KFSConstants;
32  import org.kuali.kfs.sys.context.SpringContext;
33  import org.kuali.rice.core.api.config.property.ConfigurationService;
34  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
35  import org.kuali.rice.kns.document.MaintenanceDocument;
36  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
37  import org.kuali.rice.krad.util.ObjectUtils;
38  
39  public class AssetGlobalPreRules extends MaintenancePreRulesBase {
40      protected static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AssetGlobalPreRules.class);
41  
42      /**
43       * Sets up a convenience object and few other Asset attributes
44       * 
45       * @see org.kuali.kfs.coa.document.validation.impl.MaintenancePreRulesBase#doCustomPreRules(org.kuali.rice.kns.document.MaintenanceDocument)
46       */
47      @Override
48      protected boolean doCustomPreRules(MaintenanceDocument document) {
49          if (hasDifferentObjectSubTypes(document)) {
50              if (!isOkHavingDifferentObjectSubTypes()) {
51                  event.setActionForwardName(KFSConstants.MAPPING_BASIC);
52                  return false;
53              }
54          }
55          return true;
56      }
57  
58  
59      /**
60       * Validate all object sub type codes are from the same group.
61       * 
62       * @param assetGlobal
63       * @param newLine
64       * @return
65       */
66      public boolean hasDifferentObjectSubTypes(MaintenanceDocument document) {
67          AssetGlobal assetGlobal = (AssetGlobal) document.getNewMaintainableObject().getBusinessObject();
68  
69          boolean invalid = false;
70          List<String> objectSubTypeList = new ArrayList<String>();
71  
72          for (AssetPaymentDetail assetPaymentDetail : assetGlobal.getAssetPaymentDetails()) {
73              assetPaymentDetail.refreshReferenceObject(CamsPropertyConstants.AssetPaymentDetail.OBJECT_CODE);
74              if (ObjectUtils.isNotNull(assetPaymentDetail.getObjectCode())) {
75                  objectSubTypeList.add(assetPaymentDetail.getObjectCode().getFinancialObjectSubTypeCode());
76              }
77          }
78          if (!getAssetService().isObjectSubTypeCompatible(objectSubTypeList)) {
79              invalid = true;
80          }
81          return invalid;
82      }
83  
84  
85      protected boolean isOkHavingDifferentObjectSubTypes() {
86          String parameterDetail = "(module:" + KRADServiceLocatorWeb.getKualiModuleService().getNamespaceCode(AssetGlobal.class) + "/component:" + AssetGlobal.class.getSimpleName() + ")";
87          ConfigurationService kualiConfiguration = SpringContext.getBean(ConfigurationService.class);
88  
89          String continueQuestion = kualiConfiguration.getPropertyValueAsString(CamsKeyConstants.CONTINUE_QUESTION);
90          String warningMessage = kualiConfiguration.getPropertyValueAsString(CamsKeyConstants.Payment.WARNING_NOT_SAME_OBJECT_SUB_TYPES) + " " + CamsConstants.Parameters.OBJECT_SUB_TYPE_GROUPS + " " + parameterDetail + ". " + continueQuestion;
91          return super.askOrAnalyzeYesNoQuestion(CamsConstants.AssetPayment.ASSET_PAYMENT_DIFFERENT_OBJECT_SUB_TYPE_CONFIRMATION_QUESTION, warningMessage);
92      }
93  
94      protected AssetService getAssetService() {
95          return SpringContext.getBean(AssetService.class);
96      }
97  
98      protected ParameterService getParameterService() {
99          return SpringContext.getBean(ParameterService.class);
100     }
101 }