1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
41  
42  public class AccountingDocumentRuleHelperServiceImpl implements AccountingDocumentRuleHelperService {
43      private DataDictionaryService ddService;
44      private ObjectTypeService objectTypeService;
45  
46      
47  
48  
49      public boolean isExpense(GeneralLedgerPendingEntrySourceDetail postable) {
50          
51          
52          
53          List<String> expenseObjectTypes = objectTypeService.getCurrentYearBasicExpenseObjectTypes();
54          return expenseObjectTypes.contains(getObjectCodeTypeCodeWithoutSideEffects(postable));
55      }
56  
57      
58  
59  
60      public boolean isIncome(GeneralLedgerPendingEntrySourceDetail postable) {
61          List<String> incomeObjectTypes = objectTypeService.getCurrentYearBasicIncomeObjectTypes();
62          return incomeObjectTypes.contains(getObjectCodeTypeCodeWithoutSideEffects(postable));
63      }
64  
65      
66  
67  
68  
69  
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  
84  
85  
86      public boolean isValidBalanceType(BalanceType balanceType, String errorPropertyName) {
87          return isValidBalanceType(balanceType, BalanceType.class, errorPropertyName, errorPropertyName);
88      }
89  
90      
91  
92  
93  
94  
95  
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 
111 
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         
120         if (!balanceType.isActive()) {
121             GlobalVariables.getMessageMap().putError(errorPropertyName, OLEKeyConstants.ERROR_INACTIVE, label);
122             return false;
123         }
124         return true;
125     }
126 
127     
128 
129 
130 
131     public boolean isValidOpenAccountingPeriod(AccountingPeriod accountingPeriod, Class entryClass, String attribueName, String errorPropertyName) {
132         
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         
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 
150 
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 
165 
166 
167 
168 
169 
170 
171 
172     public String formatProperty(String propertyName, Object... arguments) {
173         return MessageFormat.format(SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(propertyName), arguments);
174     }
175 
176     
177 
178 
179 
180 
181     public void setDataDictionaryService(DataDictionaryService ddService) {
182         this.ddService = ddService;
183     }
184 
185     
186 
187 
188 
189 
190     public void setObjectTypeService(ObjectTypeService objectTypeService) {
191         this.objectTypeService = objectTypeService;
192     }
193 
194 }