View Javadoc
1   /*
2    * Copyright 2008 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.sys.document.service.impl;
17  
18  import java.sql.Date;
19  import java.text.MessageFormat;
20  import java.util.List;
21  
22  import org.kuali.ole.coa.businessobject.AccountingPeriod;
23  import org.kuali.ole.coa.businessobject.BalanceType;
24  import org.kuali.ole.coa.businessobject.ObjectCode;
25  import org.kuali.ole.coa.service.ObjectCodeService;
26  import org.kuali.ole.coa.service.ObjectTypeService;
27  import org.kuali.ole.sys.OLEKeyConstants;
28  import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntrySourceDetail;
29  import org.kuali.ole.sys.context.SpringContext;
30  import org.kuali.ole.sys.document.service.AccountingDocumentRuleHelperService;
31  import org.kuali.rice.core.api.config.property.ConfigurationService;
32  import org.kuali.rice.core.api.datetime.DateTimeService;
33  import org.kuali.rice.kns.service.DataDictionaryService;
34  import org.kuali.rice.krad.datadictionary.AttributeDefinition;
35  import org.kuali.rice.krad.datadictionary.DataDictionaryEntry;
36  import org.kuali.rice.krad.util.GlobalVariables;
37  import org.kuali.rice.krad.util.ObjectUtils;
38  
39  /**
40   * The default implementation of the AccountingDocumentRuleHelperService
41   */
42  public class AccountingDocumentRuleHelperServiceImpl implements AccountingDocumentRuleHelperService {
43      private DataDictionaryService ddService;
44      private ObjectTypeService objectTypeService;
45  
46      /**
47       * @see org.kuali.ole.sys.document.service.AccountingDocumentRuleHelperService#isExpense(org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntrySourceDetail)
48       */
49      public boolean isExpense(GeneralLedgerPendingEntrySourceDetail postable) {
50          // return SpringContext.getBean(ConfigurationService.class).succeedsRule(OLEConstants.FINANCIAL_NAMESPACE,
51          // KUALI_TRANSACTION_PROCESSING_GLOBAL_RULES_SECURITY_GROUPING, APPLICATION_PARAMETER.EXPENSE_OBJECT_TYPE_CODES,
52          // getObjectCodeTypeCodeWithoutSideEffects(accountingLine) );
53          List<String> expenseObjectTypes = objectTypeService.getCurrentYearBasicExpenseObjectTypes();
54          return expenseObjectTypes.contains(getObjectCodeTypeCodeWithoutSideEffects(postable));
55      }
56  
57      /**
58       * @see org.kuali.ole.sys.document.service.AccountingDocumentRuleHelperService#isIncome(org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntrySourceDetail)
59       */
60      public boolean isIncome(GeneralLedgerPendingEntrySourceDetail postable) {
61          List<String> incomeObjectTypes = objectTypeService.getCurrentYearBasicIncomeObjectTypes();
62          return incomeObjectTypes.contains(getObjectCodeTypeCodeWithoutSideEffects(postable));
63      }
64  
65      /**
66       * Makes sure that the objectCode attribute is fully populated b/c we are using proxying in our persistence layer.
67       * 
68       * @param accountingLine
69       * @return the object type code of the object code of the given accounting line
70       */
71      public String getObjectCodeTypeCodeWithoutSideEffects(GeneralLedgerPendingEntrySourceDetail postable) {
72          Integer fiscalYear = postable.getPostingYear();
73          String chartOfAccountsCode = postable.getChartOfAccountsCode();
74          String financialObjectCode = postable.getFinancialObjectCode();
75  
76          ObjectCodeService objectCodeService = SpringContext.getBean(ObjectCodeService.class);
77          ObjectCode objectCode = objectCodeService.getByPrimaryIdWithCaching(fiscalYear, chartOfAccountsCode, financialObjectCode);
78  
79          return ObjectUtils.isNotNull(objectCode) ? objectCode.getFinancialObjectTypeCode() : null;
80      }
81  
82      /**
83       * @see org.kuali.ole.sys.document.service.AccountingDocumentRuleHelperService#isValidBalanceType(org.kuali.ole.coa.businessobject.BalanceTyp,
84       *      java.lang.String)
85       */
86      public boolean isValidBalanceType(BalanceType balanceType, String errorPropertyName) {
87          return isValidBalanceType(balanceType, BalanceType.class, errorPropertyName, errorPropertyName);
88      }
89  
90      /**
91       * Looks up a label from the data dictionary
92       * 
93       * @param entryClass the class of the attribute to lookup the label for
94       * @param attributeName the attribute to look up the label for
95       * @return the label
96       */
97      protected String getLabelFromDataDictionary(Class entryClass, String attributeName) {
98          DataDictionaryEntry entry = ddService.getDataDictionary().getDictionaryObjectEntry(entryClass.getName());
99          if (entry == null) {
100             throw new IllegalArgumentException("Cannot find DataDictionary entry for " + entryClass);
101         }
102         AttributeDefinition attributeDefinition = entry.getAttributeDefinition(attributeName);
103         if (attributeDefinition == null) {
104             throw new IllegalArgumentException("Cannot find " + entryClass + " attribute with name " + attributeName);
105         }
106         return attributeDefinition.getLabel();
107     }
108 
109     /**
110      * @see org.kuali.ole.sys.document.service.AccountingDocumentRuleHelperService#isValidBalanceType(org.kuali.ole.coa.businessobject.BalanceTyp,
111      *      java.lang.Class, java.lang.String, java.lang.String)
112      */
113     public boolean isValidBalanceType(BalanceType balanceType, Class entryClass, String attributeName, String errorPropertyName) {
114         String label = getLabelFromDataDictionary(entryClass, attributeName);
115         if (ObjectUtils.isNull(balanceType)) {
116             GlobalVariables.getMessageMap().putError(errorPropertyName, OLEKeyConstants.ERROR_EXISTENCE, label);
117             return false;
118         }
119         // make sure it's active for usage
120         if (!balanceType.isActive()) {
121             GlobalVariables.getMessageMap().putError(errorPropertyName, OLEKeyConstants.ERROR_INACTIVE, label);
122             return false;
123         }
124         return true;
125     }
126 
127     /**
128      * @see org.kuali.ole.sys.document.service.AccountingDocumentRuleHelperService#isValidOpenAccountingPeriod(org.kuali.ole.coa.businessobject.AccountingPeriod,
129      *      java.lang.Class, java.lang.String, java.lang.String)
130      */
131     public boolean isValidOpenAccountingPeriod(AccountingPeriod accountingPeriod, Class entryClass, String attribueName, String errorPropertyName) {
132         // retrieve from system to make sure it exists
133         String label = getLabelFromDataDictionary(entryClass, attribueName);
134         if (ObjectUtils.isNull(accountingPeriod)) {
135             GlobalVariables.getMessageMap().putError(errorPropertyName, OLEKeyConstants.ERROR_EXISTENCE, label);
136             return false;
137         }
138 
139         // make sure it's open for use
140         if (!accountingPeriod.isActive()) {
141             GlobalVariables.getMessageMap().putError(errorPropertyName, OLEKeyConstants.ERROR_DOCUMENT_ACCOUNTING_PERIOD_CLOSED);
142             return false;
143         }
144 
145         return true;
146     }
147 
148     /**
149      * @see org.kuali.ole.sys.document.service.AccountingDocumentRuleHelperService#isValidReversalDate(java.sql.Date,
150      *      java.lang.String)
151      */
152     public boolean isValidReversalDate(Date reversalDate, String errorPropertyName) {
153         java.sql.Date today = SpringContext.getBean(DateTimeService.class).getCurrentSqlDateMidnight();
154         if (null != reversalDate && reversalDate.before(today)) {
155             GlobalVariables.getMessageMap().putError(errorPropertyName, OLEKeyConstants.ERROR_DOCUMENT_INCORRECT_REVERSAL_DATE);
156             return false;
157         }
158         else {
159             return true;
160         }
161     }
162 
163     /**
164      * Gets the named property from ConfigurationService (i.e., from ApplicationResources.properties) and formats it with the
165      * given arguments (if any).
166      * 
167      * @param propertyName
168      * @param arguments
169      * @return the formatted property (i.e., message), with any {@code {0}} replaced with the first argument, {@code {1}} with the
170      *         second argument, etc.
171      */
172     public String formatProperty(String propertyName, Object... arguments) {
173         return MessageFormat.format(SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(propertyName), arguments);
174     }
175 
176     /**
177      * Sets the dataDictionaryService attribute value.
178      * 
179      * @param ddService The ddService to set.
180      */
181     public void setDataDictionaryService(DataDictionaryService ddService) {
182         this.ddService = ddService;
183     }
184 
185     /**
186      * Sets the objectTypeService attribute value.
187      * 
188      * @param objectTypeService The objectTypeService to set.
189      */
190     public void setObjectTypeService(ObjectTypeService objectTypeService) {
191         this.objectTypeService = objectTypeService;
192     }
193 
194 }