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.coa.document.validation.impl;
017
018 import java.util.Iterator;
019 import java.util.List;
020 import org.kuali.kfs.coa.businessobject.AccountingPeriod;
021 import org.kuali.kfs.sys.KFSKeyConstants;
022 import org.kuali.kfs.sys.businessobject.SystemOptions;
023 import org.kuali.kfs.sys.context.SpringContext;
024 import org.kuali.rice.kns.document.MaintenanceDocument;
025 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
026 import org.kuali.rice.kns.service.KeyValuesService;
027
028 /**
029 * Business rule(s) applicable to AccountingPeriodMaintence documents.
030 */
031 public class AccountingPeriodRule extends MaintenanceDocumentRuleBase {
032
033 protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AccountingPeriodRule.class);
034
035 protected static final String GENERAL_FUND_CD = "GF";
036 protected static final String RESTRICTED_FUND_CD = "RF";
037 protected static final String ENDOWMENT_FUND_CD = "EN";
038 protected static final String PLANT_FUND_CD = "PF";
039
040 protected static final String RESTRICTED_CD_RESTRICTED = "R";
041 protected static final String RESTRICTED_CD_UNRESTRICTED = "U";
042 protected static final String RESTRICTED_CD_TEMPORARILY_RESTRICTED = "T";
043 protected static final String SUB_FUND_GROUP_MEDICAL_PRACTICE_FUNDS = "MPRACT";
044 protected static final String BUDGET_RECORDING_LEVEL_MIXED = "M";
045
046 protected AccountingPeriod oldAccountingPeriod;
047 protected AccountingPeriod newAccountingPeriod;
048
049 public AccountingPeriodRule() {
050 }
051
052 /**
053 * This method sets the convenience objects like newAccount and oldAccount, so you have short and easy handles to the new and
054 * old objects contained in the maintenance document. It also calls the BusinessObjectBase.refresh(), which will attempt to load
055 * all sub-objects from the DB by their primary keys, if available.
056 *
057 * @param document - the maintenanceDocument being evaluated
058 */
059 public void setupConvenienceObjects() {
060
061 // setup oldAccountingPeriod convenience objects, make sure all possible sub-objects are populated
062 oldAccountingPeriod = (AccountingPeriod) super.getOldBo();
063
064 // setup newAccountingPeriod convenience objects, make sure all possible sub-objects are populated
065 newAccountingPeriod = (AccountingPeriod) super.getNewBo();
066 }
067
068 /**
069 * This method checks the following rules: calls processCustomRouteDocumentBusinessRules but does not fail if any of them fail
070 * (this only happens on routing)
071 *
072 * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
073 */
074 protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
075
076 LOG.info("processCustomSaveDocumentBusinessRules called");
077 // call the route rules to report all of the messages, but ignore the result
078 processCustomRouteDocumentBusinessRules(document);
079
080 // Save always succeeds, even if there are business rule failures
081 return true;
082 }
083
084 /**
085 * This method checks to see if the fiscal year for any of {@link Options} is the same as the {@link AccountingPeriod}'s fiscal
086 * year
087 *
088 * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
089 */
090 protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
091
092 LOG.info("processCustomRouteDocumentBusinessRules called");
093 setupConvenienceObjects();
094
095 Boolean foundYear = false;
096
097 KeyValuesService boService = SpringContext.getBean(KeyValuesService.class);
098 List optionList = (List) boService.findAll(SystemOptions.class);
099 if ( newAccountingPeriod.getUniversityFiscalYear() != null) {
100 for (Iterator iter = optionList.iterator(); iter.hasNext();) {
101 SystemOptions options = (SystemOptions) iter.next();
102 if (options.getUniversityFiscalYear().compareTo(newAccountingPeriod.getUniversityFiscalYear()) == 0) {
103 foundYear = true;
104 break;
105 }
106 }
107 }
108 if (!foundYear) {
109 // display an error
110 putFieldError("universityFiscalYear", KFSKeyConstants.ERROR_DOCUMENT_FISCAL_PERIOD_YEAR_DOESNT_EXIST);
111 }
112
113 return foundYear;
114 }
115
116 }