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.Map;
019
020 import org.apache.commons.lang.StringUtils;
021 import org.kuali.kfs.coa.businessobject.ObjectLevel;
022 import org.kuali.kfs.coa.businessobject.ObjectCode;
023 import org.kuali.kfs.coa.service.ChartService;
024 import org.kuali.kfs.coa.service.ObjectLevelService;
025 import org.kuali.kfs.sys.KFSConstants;
026 import org.kuali.kfs.sys.KFSKeyConstants;
027 import org.kuali.kfs.sys.context.SpringContext;
028 import org.kuali.rice.kns.document.Document;
029 import org.kuali.rice.kns.document.MaintenanceDocument;
030 import org.kuali.rice.kns.rules.PromptBeforeValidationBase;
031 import org.kuali.rice.kns.service.KualiConfigurationService;
032
033 /**
034 * PreRules checks for the {@link ObjectCode} that needs to occur while still in the Struts processing. This includes defaults, confirmations,
035 * etc.
036 */
037 public class ObjectCodePreRules extends PromptBeforeValidationBase {
038 protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ObjectCodePreRules.class);
039
040 protected static ChartService chartService;
041 protected static ObjectLevelService objectLevelService;
042 protected Map reportsTo;
043
044 /**
045 *
046 * Constructs a ObjectCodePreRules
047 * Pseudo-injects services and populates the reportsTo hierarchy
048 */
049 public ObjectCodePreRules() {
050 if (objectLevelService == null) {
051 objectLevelService = SpringContext.getBean(ObjectLevelService.class);
052 chartService = SpringContext.getBean(ChartService.class);
053 }
054
055 reportsTo = chartService.getReportsToHierarchy();
056 }
057
058 /**
059 * This method forces the reports to chart on the object code to be the correct one based on the
060 * reports to hierarchy
061 * <p>
062 * Additionally if the object level is null or inactive it confirms with the user that this
063 * is actually the object level code they wish to use
064 * @see org.kuali.rice.kns.rules.PromptBeforeValidationBase#doRules(org.kuali.rice.kns.document.Document)
065 */
066 @Override
067 public boolean doPrompts(Document document) {
068 MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
069
070 LOG.debug("doRules");
071
072 if (LOG.isDebugEnabled()) {
073 LOG.debug("new maintainable is: " + maintenanceDocument.getNewMaintainableObject().getClass());
074 }
075 ObjectCode newObjectCode = (ObjectCode) maintenanceDocument.getNewMaintainableObject().getBusinessObject();
076
077 String chart = newObjectCode.getChartOfAccountsCode();
078 String reportsToChart = (String) reportsTo.get(chart);
079 if (LOG.isDebugEnabled()) {
080 LOG.debug("Chart: " + chart);
081 LOG.debug("reportsTo: " + reportsToChart);
082 LOG.debug("User supplied reportsToChart: " + newObjectCode.getReportsToChartOfAccountsCode());
083 }
084
085 // force reportsTo to the right value regardless of user input
086 newObjectCode.setReportsToChartOfAccountsCode(reportsToChart);
087
088 // If Object Level is inactive, ask user confirmation question
089 ObjectLevel financialObjectLevel = objectLevelService.getByPrimaryId(chart, newObjectCode.getFinancialObjectLevelCode());
090 if (!(financialObjectLevel == null)) {
091 if (!financialObjectLevel.isActive()) {
092 String objectLevelChartOfAccountCode = financialObjectLevel.getChartOfAccountsCode();
093 String objectLevelFinancialObjectLevelCode = financialObjectLevel.getFinancialObjectLevelCode();
094 String objectLevelFinancialObjectLevelName = financialObjectLevel.getFinancialObjectLevelName();
095 String questionText = SpringContext.getBean(KualiConfigurationService.class).getPropertyString(KFSKeyConstants.ObjectCode.QUESTION_INACTIVE_OBJECT_LEVEL_CONFIRMATION);
096 questionText = StringUtils.replace(questionText, "{0}", objectLevelChartOfAccountCode);
097 questionText = StringUtils.replace(questionText, "{1}", objectLevelFinancialObjectLevelCode);
098 questionText = StringUtils.replace(questionText, "{2}", objectLevelFinancialObjectLevelName);
099 boolean useInactiveObjectLevel = super.askOrAnalyzeYesNoQuestion(KFSConstants.ObjectCodeConstants.INACTIVE_OBJECT_LEVEL_QUESTION_ID, questionText);
100 if (!useInactiveObjectLevel) {
101 event.setActionForwardName(KFSConstants.MAPPING_BASIC);
102 return false;
103 }
104 }
105 }
106
107 return true;
108
109 }
110 }