001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.chart.rules;
017:
018: import java.util.Map;
019:
020: import org.apache.commons.lang.StringUtils;
021: import org.kuali.core.document.Document;
022: import org.kuali.core.document.MaintenanceDocument;
023: import org.kuali.core.rules.PreRulesContinuationBase;
024: import org.kuali.core.service.KualiConfigurationService;
025: import org.kuali.kfs.KFSConstants;
026: import org.kuali.kfs.KFSKeyConstants;
027: import org.kuali.kfs.context.SpringContext;
028: import org.kuali.module.chart.bo.ObjLevel;
029: import org.kuali.module.chart.bo.ObjectCode;
030: import org.kuali.module.chart.service.ChartService;
031: import org.kuali.module.chart.service.ObjectLevelService;
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 PreRulesContinuationBase {
038: protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
039: .getLogger(ObjectCodePreRules.class);
040:
041: private static ChartService chartService;
042: private static ObjectLevelService objectLevelService;
043: private Map reportsTo;
044:
045: /**
046: *
047: * Constructs a ObjectCodePreRules
048: * Pseudo-injects services and populates the reportsTo hierarchy
049: */
050: public ObjectCodePreRules() {
051: if (objectLevelService == null) {
052: objectLevelService = SpringContext
053: .getBean(ObjectLevelService.class);
054: chartService = SpringContext.getBean(ChartService.class);
055: }
056:
057: reportsTo = chartService.getReportsToHierarchy();
058: }
059:
060: /**
061: * This method forces the reports to chart on the object code to be the correct one based on the
062: * reports to hierarchy
063: * <p>
064: * Additionally if the object level is null or inactive it confirms with the user that this
065: * is actually the object level code they wish to use
066: * @see org.kuali.core.rules.PreRulesContinuationBase#doRules(org.kuali.core.document.Document)
067: */
068: public boolean doRules(Document document) {
069: MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
070:
071: LOG.debug("doRules");
072:
073: if (LOG.isDebugEnabled()) {
074: LOG.debug("new maintainable is: "
075: + maintenanceDocument.getNewMaintainableObject()
076: .getClass());
077: }
078: ObjectCode newObjectCode = (ObjectCode) maintenanceDocument
079: .getNewMaintainableObject().getBusinessObject();
080:
081: String chart = newObjectCode.getChartOfAccountsCode();
082: String reportsToChart = (String) reportsTo.get(chart);
083: if (LOG.isDebugEnabled()) {
084: LOG.debug("Chart: " + chart);
085: LOG.debug("reportsTo: " + reportsToChart);
086: LOG.debug("User supplied reportsToChart: "
087: + newObjectCode.getReportsToChartOfAccountsCode());
088: }
089:
090: // force reportsTo to the right value regardless of user input
091: newObjectCode.setReportsToChartOfAccountsCode(reportsToChart);
092:
093: // If Object Level is inactive, ask user confirmation question
094: ObjLevel financialObjectLevel = objectLevelService
095: .getByPrimaryId(chart, newObjectCode
096: .getFinancialObjectLevelCode());
097: if (!(financialObjectLevel == null)) {
098: if (!financialObjectLevel
099: .isFinancialObjectLevelActiveIndicator()) {
100: String objectLevelChartOfAccountCode = financialObjectLevel
101: .getChartOfAccountsCode();
102: String objectLevelFinancialObjectLevelCode = financialObjectLevel
103: .getFinancialObjectLevelCode();
104: String objectLevelFinancialObjectLevelName = financialObjectLevel
105: .getFinancialObjectLevelName();
106: String questionText = SpringContext
107: .getBean(KualiConfigurationService.class)
108: .getPropertyString(
109: KFSKeyConstants.ObjectCode.QUESTION_INACTIVE_OBJECT_LEVEL_CONFIRMATION);
110: questionText = StringUtils.replace(questionText, "{0}",
111: objectLevelChartOfAccountCode);
112: questionText = StringUtils.replace(questionText, "{1}",
113: objectLevelFinancialObjectLevelCode);
114: questionText = StringUtils.replace(questionText, "{2}",
115: objectLevelFinancialObjectLevelName);
116: boolean useInactiveObjectLevel = super
117: .askOrAnalyzeYesNoQuestion(
118: KFSConstants.ObjectCodeConstants.INACTIVE_OBJECT_LEVEL_QUESTION_ID,
119: questionText);
120: if (!useInactiveObjectLevel) {
121: event
122: .setActionForwardName(KFSConstants.MAPPING_BASIC);
123: return false;
124: }
125: }
126: }
127:
128: return true;
129:
130: }
131: }
|