View Javadoc
1   /*
2    * Copyright 2007 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;
17  
18  import java.sql.Date;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.kuali.ole.coa.businessobject.AccountingPeriod;
22  import org.kuali.ole.coa.service.AccountingPeriodService;
23  import org.kuali.ole.sys.context.SpringContext;
24  import org.kuali.rice.core.api.datetime.DateTimeService;
25  import org.kuali.rice.kew.api.exception.WorkflowException;
26  import org.kuali.rice.kns.service.DataDictionaryService;
27  import org.kuali.rice.krad.util.ObjectUtils;
28  
29  /**
30   * Base implementation for a ledger posting document.
31   */
32  public class LedgerPostingDocumentBase extends FinancialSystemTransactionalDocumentBase implements LedgerPostingDocument {
33      static protected transient DateTimeService dateTimeService;
34      static protected transient AccountingPeriodService accountingPeriodService;
35      static protected transient DataDictionaryService dataDictionaryService;
36         
37      protected AccountingPeriod accountingPeriod;
38      protected Integer postingYear;
39      protected String postingPeriodCode;
40      protected boolean checkPostingYearForCopy;
41  
42      /**
43       * Constructs a LedgerPostingDocumentBase.java.
44       */
45      public LedgerPostingDocumentBase() {
46          super();
47          if ( SpringContext.isInitialized() ) {
48              createInitialAccountingPeriod();
49          }
50      }
51  
52      /**
53       * Used during initialization to provide a base <code>{@link AccountingPeriod}</code>.<br/>
54       * <p>
55       * This is a hack right now because its intended to be set by the
56       * <code>{@link org.kuali.ole.coa.service.AccountingPeriodService}</code>
57       * 
58       * @return AccountingPeriod
59       */
60      public void createInitialAccountingPeriod() { 
61          AccountingPeriod accountingPeriod = retrieveCurrentAccountingPeriod();
62          setAccountingPeriod(accountingPeriod);
63      }
64      
65      /**
66       * Finds the accounting period for the current date
67       * @return the current accounting period
68       */
69      public AccountingPeriod retrieveCurrentAccountingPeriod() {
70      	// CSU 6702 BEGIN
71          try {
72          // CSU 6702 END    	
73          Date date = getDateTimeService().getCurrentSqlDate();
74          return getAccountingPeriodService().getByDate(date);
75         	// CSU 6702 BEGIN
76          } catch ( RuntimeException ex ) {
77              // catch and ignore - prevent blowup when called before services initialized
78              return null;
79          }
80          // CSU 6702 END
81      }
82  
83      /**
84       * @see org.kuali.ole.sys.document.LedgerPostingDocument#getPostingYear()
85       */
86      public Integer getPostingYear() {
87          return postingYear;
88      }
89  
90      /**
91       * @see org.kuali.ole.sys.document.LedgerPostingDocument#setPostingYear(java.lang.Integer)
92       */
93      public void setPostingYear(Integer postingYear) {
94          this.postingYear = postingYear;
95      }
96  
97      /**
98       * @see org.kuali.ole.sys.document.LedgerPostingDocument#getPostingPeriodCode()
99       */
100     public String getPostingPeriodCode() {
101         return postingPeriodCode;
102     }
103 
104     /**
105      * @see org.kuali.ole.sys.document.LedgerPostingDocument#setPostingPeriodCode(java.lang.String)
106      */
107     public void setPostingPeriodCode(String postingPeriodCode) {
108         this.postingPeriodCode = postingPeriodCode;
109     }
110 
111     /**
112      * @see org.kuali.ole.sys.document.LedgerPostingDocument#getAccountingPeriod()
113      */
114     public AccountingPeriod getAccountingPeriod() {
115         accountingPeriod = getAccountingPeriodService().getByPeriod(postingPeriodCode, postingYear);
116 
117         return accountingPeriod;
118     }
119 
120     /**
121      * @see org.kuali.ole.sys.document.LedgerPostingDocument#setAccountingPeriod(AccountingPeriod)
122      */
123     public void setAccountingPeriod(AccountingPeriod accountingPeriod) {
124         this.accountingPeriod = accountingPeriod;
125         
126         if(ObjectUtils.isNotNull(accountingPeriod)) {
127             this.setPostingYear(accountingPeriod.getUniversityFiscalYear());
128             this.setPostingPeriodCode(accountingPeriod.getUniversityFiscalPeriodCode());
129         }
130     }
131     
132     /**
133      * If we've copied, we need to update the posting period and year
134      * @see org.kuali.rice.krad.document.DocumentBase#toCopy()
135      */
136     @Override
137     public void toCopy() throws WorkflowException, IllegalStateException {
138         super.toCopy();
139         setAccountingPeriod(retrieveCurrentAccountingPeriod());
140     }
141     
142     /**
143      * Returns the financial document type code for the given document, using the DataDictionaryService
144      * @return the financial document type code for the given document
145      */
146     public String getFinancialDocumentTypeCode() {
147         return getDataDictionaryService().getDocumentTypeNameByClass(this.getClass());
148     }
149     
150 
151     public static DataDictionaryService getDataDictionaryService() {
152         if ( dataDictionaryService == null ) {
153             dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
154         }
155         return dataDictionaryService;
156     }
157 
158     public static DateTimeService getDateTimeService() {
159         if ( dateTimeService == null ) {
160             dateTimeService = SpringContext.getBean(DateTimeService.class);
161         }
162         return dateTimeService;
163     }
164 
165     public static AccountingPeriodService getAccountingPeriodService() {
166         if ( accountingPeriodService == null ) {
167             accountingPeriodService = SpringContext.getBean(AccountingPeriodService.class);
168         }
169         return accountingPeriodService;
170     }
171     
172     // CSU 6702 BEGIN
173     // rSmart-jkneal-KFSCSU-199-begin mod for selected accounting period
174     /**
175      * Composite of postingPeriodCode and postingYear
176      * @return Return a composite of postingPeriodCode and postingYear
177      */
178     public String getAccountingPeriodCompositeString() {
179         return postingPeriodCode + postingYear;
180     }
181 
182     /**
183      * Set accountingPeriod based on incoming paramater.
184      * @param accountingPeriodString in the form of [period][year]
185      */
186     public void setAccountingPeriodCompositeString(String accountingPeriodString) {
187         if (StringUtils.isNotBlank(accountingPeriodString)) {
188             String period = StringUtils.left(accountingPeriodString, 2);
189             Integer year = new Integer(StringUtils.right(accountingPeriodString, 4));
190             AccountingPeriod accountingPeriod = getAccountingPeriodService().getByPeriod(period, year);
191             setAccountingPeriod(accountingPeriod);
192         }
193     }
194     // rSmart-jkneal-KFSCSU-199-end mod for selected accounting period
195     // CSU 6702 END    
196 }