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 java.util.HashMap;
019 import java.util.Map;
020
021 import org.apache.commons.lang.StringUtils;
022 import org.apache.log4j.Logger;
023 import org.kuali.kfs.module.ar.ArConstants;
024 import org.kuali.kfs.module.ar.ArKeyConstants;
025 import org.kuali.kfs.module.ar.ArPropertyConstants;
026 import org.kuali.kfs.module.ar.businessobject.OrganizationOptions;
027 import org.kuali.kfs.module.ar.businessobject.SystemInformation;
028 import org.kuali.kfs.sys.KFSConstants;
029 import org.kuali.kfs.sys.KFSKeyConstants;
030 import org.kuali.kfs.sys.context.SpringContext;
031 import org.kuali.kfs.sys.service.UniversityDateService;
032 import org.kuali.kfs.sys.service.impl.KfsParameterConstants;
033 import org.kuali.rice.kns.document.MaintenanceDocument;
034 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
035 import org.kuali.rice.kns.service.BusinessObjectService;
036 import org.kuali.rice.kns.service.ParameterService;
037 import org.kuali.rice.kns.util.ErrorMap;
038 import org.kuali.rice.kns.util.ErrorMessage;
039 import org.kuali.rice.kns.util.ObjectUtils;
040 import org.kuali.rice.kns.util.TypedArrayList;
041
042 public class OrganizationOptionsRule extends MaintenanceDocumentRuleBase {
043 protected static Logger LOG = org.apache.log4j.Logger.getLogger(OrganizationOptionsRule.class);
044
045 protected OrganizationOptions newOrganizationOptions;
046 protected OrganizationOptions oldOrganizationOptions;
047
048 @Override
049 public void setupConvenienceObjects() {
050 newOrganizationOptions = (OrganizationOptions) super.getNewBo();
051 oldOrganizationOptions = (OrganizationOptions) super.getOldBo();
052 }
053
054 @Override
055 protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
056
057 boolean success = true;
058 success &= doesSystemInformationExistForProcessingChartAngOrg(newOrganizationOptions);
059 success &= isOrganizationOptionsPostalZipCodeNotEmpty(newOrganizationOptions);
060 return success;
061 }
062
063 @Override
064 protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
065 // always return true even if there are business rule failures.
066 processCustomRouteDocumentBusinessRules(document);
067 return true;
068
069 }
070
071 // TODO method is gone - fix for kim / session work
072 // /**
073 // *
074 // * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#checkAuthorizationRestrictions(org.kuali.rice.kns.document.MaintenanceDocument)
075 // */
076 // @Override
077 // protected boolean checkAuthorizationRestrictions(MaintenanceDocument document) {
078 // boolean success = super.checkAuthorizationRestrictions(document);
079 //
080 // ErrorMap map = GlobalVariables.getMessageMap();
081 //
082 // if(map.containsMessageKey(KFSKeyConstants.ERROR_DOCUMENT_AUTHORIZATION_RESTRICTED_FIELD_CHANGED)) {
083 // removeRestrictedFieldChangedErrors(map, KFSConstants.MAINTENANCE_NEW_MAINTAINABLE + ArPropertyConstants.OrganizationOptionsFields.PROCESSING_CHART_OF_ACCOUNTS_CODE);
084 // removeRestrictedFieldChangedErrors(map, KFSConstants.MAINTENANCE_NEW_MAINTAINABLE + ArPropertyConstants.OrganizationOptionsFields.PROCESSING_ORGANIZATION_CODE);
085 // removeRestrictedFieldChangedErrors(map, KFSConstants.MAINTENANCE_NEW_MAINTAINABLE + ArPropertyConstants.OrganizationOptionsFields.ORGANIZATION_REMIT_TO_ADDRESS_NAME);
086 // removeRestrictedFieldChangedErrors(map, KFSConstants.MAINTENANCE_NEW_MAINTAINABLE + ArPropertyConstants.OrganizationOptionsFields.ORGANIZATION_REMIT_TO_LINE1_STREET_ADDRESS);
087 // removeRestrictedFieldChangedErrors(map, KFSConstants.MAINTENANCE_NEW_MAINTAINABLE + ArPropertyConstants.OrganizationOptionsFields.ORGANIZATION_REMIT_TO_LINE2_STREET_ADDRESS);
088 // removeRestrictedFieldChangedErrors(map, KFSConstants.MAINTENANCE_NEW_MAINTAINABLE + ArPropertyConstants.OrganizationOptionsFields.ORGANIZATION_REMIT_TO_CITY_NAME);
089 // removeRestrictedFieldChangedErrors(map, KFSConstants.MAINTENANCE_NEW_MAINTAINABLE + ArPropertyConstants.OrganizationOptionsFields.ORGANIZATION_REMIT_TO_STATE_CODE);
090 // removeRestrictedFieldChangedErrors(map, KFSConstants.MAINTENANCE_NEW_MAINTAINABLE + ArPropertyConstants.OrganizationOptionsFields.ORGANIZATION_REMIT_TO_ZIP_CODE);
091 // }
092 //
093 // return success;
094 // }
095
096 /**
097 * This method...
098 * @param map
099 */
100 protected void removeRestrictedFieldChangedErrors(ErrorMap map, String propertyKey) {
101 TypedArrayList errorMessages = (TypedArrayList)map.get(propertyKey);
102 if(errorMessages!=null) {
103 for(int i=0; i<errorMessages.size(); i++) {
104 ErrorMessage eMessage = (ErrorMessage)errorMessages.get(i);
105 String errorKey = eMessage.getErrorKey();
106 if(errorKey.equals(KFSKeyConstants.ERROR_DOCUMENT_AUTHORIZATION_RESTRICTED_FIELD_CHANGED)) {
107 errorMessages.remove(i);
108 }
109 }
110 }
111 }
112
113 /**
114 * This method this returns true if system information row exists for processing chart and org
115 *
116 * @param organizationOptions
117 * @return
118 */
119 public boolean doesSystemInformationExistForProcessingChartAngOrg(OrganizationOptions organizationOptions) {
120 boolean success = true;
121
122 String processingChartOfAccountCode = organizationOptions.getProcessingChartOfAccountCode();
123 String processingOrganizationCode = organizationOptions.getProcessingOrganizationCode();
124
125 Map criteria = new HashMap();
126 criteria.put(KFSConstants.UNIVERSITY_FISCAL_YEAR_PROPERTY_NAME, SpringContext.getBean(UniversityDateService.class).getCurrentFiscalYear());
127 criteria.put(ArPropertyConstants.OrganizationOptionsFields.PROCESSING_CHART_OF_ACCOUNTS_CODE, processingChartOfAccountCode);
128 criteria.put(ArPropertyConstants.OrganizationOptionsFields.PROCESSING_ORGANIZATION_CODE, processingOrganizationCode);
129
130 SystemInformation systemInformation = (SystemInformation) SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(SystemInformation.class, criteria);
131
132 if (ObjectUtils.isNull(systemInformation)) {
133 putFieldError(ArPropertyConstants.OrganizationOptionsFields.PROCESSING_CHART_OF_ACCOUNTS_CODE, ArKeyConstants.OrganizationOptionsErrors.SYS_INFO_DOES_NOT_EXIST_FOR_PROCESSING_CHART_AND_ORG, new String[] { processingChartOfAccountCode, processingOrganizationCode });
134 success = false;
135 }
136 return success;
137 }
138
139 /**
140 * This method returns false if org option postal code is empty and sales tax indicator is set to Y. Else, returns true.
141 * @param organizationOptions
142 * @return
143 */
144 public boolean isOrganizationOptionsPostalZipCodeNotEmpty(OrganizationOptions organizationOptions) {
145 boolean success = true;
146
147 // check if sales tax is enabled && if org postal code is empty
148 if (SpringContext.getBean(ParameterService.class).getIndicatorParameter(KfsParameterConstants.ACCOUNTS_RECEIVABLE_DOCUMENT.class, ArConstants.ENABLE_SALES_TAX_IND) && StringUtils.isBlank(organizationOptions.getOrganizationPostalZipCode())) {
149 putFieldError(ArPropertyConstants.OrganizationOptionsFields.ORGANIZATION_POSTAL_ZIP_CODE, ArKeyConstants.OrganizationOptionsErrors.ERROR_ORG_OPTIONS_ZIP_CODE_REQUIRED);
150 success = false;
151 }
152
153 return success;
154 }
155 }