001 /*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.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/ecl2.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.kfs.sys.document;
017
018 import java.sql.Date;
019
020 import org.kuali.kfs.coa.businessobject.AccountingPeriod;
021 import org.kuali.kfs.coa.service.AccountingPeriodService;
022 import org.kuali.kfs.sys.context.SpringContext;
023 import org.kuali.rice.kew.exception.WorkflowException;
024 import org.kuali.rice.kns.service.DataDictionaryService;
025 import org.kuali.rice.kns.service.DateTimeService;
026 import org.kuali.rice.kns.util.ObjectUtils;
027
028 /**
029 * Base implementation for a ledger posting document.
030 */
031 public class LedgerPostingDocumentBase extends FinancialSystemTransactionalDocumentBase implements LedgerPostingDocument {
032 static protected transient DateTimeService dateTimeService;
033 static protected transient AccountingPeriodService accountingPeriodService;
034 static protected transient DataDictionaryService dataDictionaryService;
035
036 protected AccountingPeriod accountingPeriod;
037 protected Integer postingYear;
038 protected String postingPeriodCode;
039 protected boolean checkPostingYearForCopy;
040
041 /**
042 * Constructs a LedgerPostingDocumentBase.java.
043 */
044 public LedgerPostingDocumentBase() {
045 super();
046 createInitialAccountingPeriod();
047 }
048
049 /**
050 * Used during initialization to provide a base <code>{@link AccountingPeriod}</code>.<br/>
051 * <p>
052 * This is a hack right now because its intended to be set by the
053 * <code>{@link org.kuali.kfs.coa.service.AccountingPeriodService}</code>
054 *
055 * @return AccountingPeriod
056 */
057 public void createInitialAccountingPeriod() {
058 AccountingPeriod accountingPeriod = retrieveCurrentAccountingPeriod();
059 setAccountingPeriod(accountingPeriod);
060 }
061
062 /**
063 * Finds the accounting period for the current date
064 * @return the current accounting period
065 */
066 public AccountingPeriod retrieveCurrentAccountingPeriod() {
067 try {
068 Date date = getDateTimeService().getCurrentSqlDate();
069 return getAccountingPeriodService().getByDate(date);
070 } catch ( RuntimeException ex ) {
071 // catch and ignore - prevent blowup when called before services initialized
072 return null;
073 }
074 }
075
076 /**
077 * @see org.kuali.kfs.sys.document.LedgerPostingDocument#getPostingYear()
078 */
079 public Integer getPostingYear() {
080 return postingYear;
081 }
082
083 /**
084 * @see org.kuali.kfs.sys.document.LedgerPostingDocument#setPostingYear(java.lang.Integer)
085 */
086 public void setPostingYear(Integer postingYear) {
087 this.postingYear = postingYear;
088 }
089
090 /**
091 * @see org.kuali.kfs.sys.document.LedgerPostingDocument#getPostingPeriodCode()
092 */
093 public String getPostingPeriodCode() {
094 return postingPeriodCode;
095 }
096
097 /**
098 * @see org.kuali.kfs.sys.document.LedgerPostingDocument#setPostingPeriodCode(java.lang.String)
099 */
100 public void setPostingPeriodCode(String postingPeriodCode) {
101 this.postingPeriodCode = postingPeriodCode;
102 }
103
104 /**
105 * @see org.kuali.kfs.sys.document.LedgerPostingDocument#getAccountingPeriod()
106 */
107 public AccountingPeriod getAccountingPeriod() {
108 accountingPeriod = getAccountingPeriodService().getByPeriod(postingPeriodCode, postingYear);
109
110 return accountingPeriod;
111 }
112
113 /**
114 * @see org.kuali.kfs.sys.document.LedgerPostingDocument#setAccountingPeriod(AccountingPeriod)
115 */
116 public void setAccountingPeriod(AccountingPeriod accountingPeriod) {
117 this.accountingPeriod = accountingPeriod;
118
119 if(ObjectUtils.isNotNull(accountingPeriod)) {
120 this.setPostingYear(accountingPeriod.getUniversityFiscalYear());
121 this.setPostingPeriodCode(accountingPeriod.getUniversityFiscalPeriodCode());
122 }
123 }
124
125 /**
126 * If we've copied, we need to update the posting period and year
127 * @see org.kuali.rice.kns.document.DocumentBase#toCopy()
128 */
129 @Override
130 public void toCopy() throws WorkflowException, IllegalStateException {
131 super.toCopy();
132 setAccountingPeriod(retrieveCurrentAccountingPeriod());
133 }
134
135 /**
136 * Returns the financial document type code for the given document, using the DataDictionaryService
137 * @return the financial document type code for the given document
138 */
139 public String getFinancialDocumentTypeCode() {
140 return getDataDictionaryService().getDocumentTypeNameByClass(this.getClass());
141 }
142
143
144 public static DataDictionaryService getDataDictionaryService() {
145 if ( dataDictionaryService == null ) {
146 dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
147 }
148 return dataDictionaryService;
149 }
150
151 public static DateTimeService getDateTimeService() {
152 if ( dateTimeService == null ) {
153 dateTimeService = SpringContext.getBean(DateTimeService.class);
154 }
155 return dateTimeService;
156 }
157
158 public static AccountingPeriodService getAccountingPeriodService() {
159 if ( accountingPeriodService == null ) {
160 accountingPeriodService = SpringContext.getBean(AccountingPeriodService.class);
161 }
162 return accountingPeriodService;
163 }
164 }