View Javadoc
1   /*
2    * Copyright 2006 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.coa.document.validation.impl;
17  
18  import java.util.Map;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.kuali.ole.coa.businessobject.ObjectCode;
22  import org.kuali.ole.coa.businessobject.ObjectLevel;
23  import org.kuali.ole.coa.service.ChartService;
24  import org.kuali.ole.coa.service.ObjectLevelService;
25  import org.kuali.ole.sys.OLEConstants;
26  import org.kuali.ole.sys.OLEKeyConstants;
27  import org.kuali.ole.sys.context.SpringContext;
28  import org.kuali.rice.core.api.config.property.ConfigurationService;
29  import org.kuali.rice.kns.document.MaintenanceDocument;
30  import org.kuali.rice.kns.rules.PromptBeforeValidationBase;
31  import org.kuali.rice.krad.document.Document;
32  
33  /**
34   * PreRules checks for the {@link ObjectCode} that needs to occur while still in the Struts processing. This includes defaults, confirmations,
35   * etc.
36   */
37  public class ObjectCodePreRules extends PromptBeforeValidationBase {
38      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ObjectCodePreRules.class);
39  
40      protected static ChartService chartService;
41      protected static ObjectLevelService objectLevelService;
42      protected Map reportsTo;
43  
44      /**
45       *
46       * Constructs a ObjectCodePreRules
47       * Pseudo-injects services and populates the reportsTo hierarchy
48       */
49      public ObjectCodePreRules() {
50          if (objectLevelService == null) {
51              objectLevelService = SpringContext.getBean(ObjectLevelService.class);
52              chartService = SpringContext.getBean(ChartService.class);
53          }
54  
55          reportsTo = chartService.getReportsToHierarchy();
56      }
57  
58      /**
59       * This method forces the reports to chart on the object code to be the correct one based on the
60       * reports to hierarchy
61       * <p>
62       * Additionally if the object level is null or inactive it confirms with the user that this
63       * is actually the object level code they wish to use
64       * @see org.kuali.rice.kns.rules.PromptBeforeValidationBase#doRules(org.kuali.rice.krad.document.Document)
65       */
66      @Override
67      public boolean doPrompts(Document document) {
68          MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
69  
70          LOG.debug("doRules");
71  
72          if (LOG.isDebugEnabled()) {
73              LOG.debug("new maintainable is: " + maintenanceDocument.getNewMaintainableObject().getClass());
74          }
75          ObjectCode newObjectCode = (ObjectCode) maintenanceDocument.getNewMaintainableObject().getBusinessObject();
76  
77          String chart = newObjectCode.getChartOfAccountsCode();
78          String reportsToChart = (String) reportsTo.get(chart);
79          if (LOG.isDebugEnabled()) {
80              LOG.debug("Chart: " + chart);
81              LOG.debug("reportsTo: " + reportsToChart);
82              LOG.debug("User supplied reportsToChart: " + newObjectCode.getReportsToChartOfAccountsCode());
83          }
84  
85          // force reportsTo to the right value regardless of user input
86          newObjectCode.setReportsToChartOfAccountsCode(reportsToChart);
87  
88          // If Object Level is inactive, ask user confirmation question
89          ObjectLevel financialObjectLevel = objectLevelService.getByPrimaryId(chart, newObjectCode.getFinancialObjectLevelCode());
90          if (!(financialObjectLevel == null)) {
91              if (!financialObjectLevel.isActive()) {
92                  String objectLevelChartOfAccountCode = financialObjectLevel.getChartOfAccountsCode();
93                  String objectLevelFinancialObjectLevelCode = financialObjectLevel.getFinancialObjectLevelCode();
94                  String objectLevelFinancialObjectLevelName = financialObjectLevel.getFinancialObjectLevelName();
95                  String questionText = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEKeyConstants.ObjectCode.QUESTION_INACTIVE_OBJECT_LEVEL_CONFIRMATION);
96                  questionText = StringUtils.replace(questionText, "{0}", objectLevelChartOfAccountCode);
97                  questionText = StringUtils.replace(questionText, "{1}", objectLevelFinancialObjectLevelCode);
98                  questionText = StringUtils.replace(questionText, "{2}", objectLevelFinancialObjectLevelName);
99                  boolean useInactiveObjectLevel = super.askOrAnalyzeYesNoQuestion(OLEConstants.ObjectCodeConstants.INACTIVE_OBJECT_LEVEL_QUESTION_ID, questionText);
100                 if (!useInactiveObjectLevel) {
101                     event.setActionForwardName(OLEConstants.MAPPING_BASIC);
102                     return false;
103                 }
104             }
105         }
106 
107         return true;
108 
109     }
110 }