001/*
002 * Copyright 2007 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 */
016package org.kuali.ole.sys.document;
017
018import java.sql.Date;
019
020import org.apache.commons.lang.StringUtils;
021import org.kuali.ole.coa.businessobject.AccountingPeriod;
022import org.kuali.ole.coa.service.AccountingPeriodService;
023import org.kuali.ole.sys.context.SpringContext;
024import org.kuali.rice.core.api.datetime.DateTimeService;
025import org.kuali.rice.kew.api.exception.WorkflowException;
026import org.kuali.rice.kns.service.DataDictionaryService;
027import org.kuali.rice.krad.util.ObjectUtils;
028
029/**
030 * Base implementation for a ledger posting document.
031 */
032public class LedgerPostingDocumentBase extends FinancialSystemTransactionalDocumentBase implements LedgerPostingDocument {
033    static protected transient DateTimeService dateTimeService;
034    static protected transient AccountingPeriodService accountingPeriodService;
035    static protected transient DataDictionaryService dataDictionaryService;
036       
037    protected AccountingPeriod accountingPeriod;
038    protected Integer postingYear;
039    protected String postingPeriodCode;
040    protected boolean checkPostingYearForCopy;
041
042    /**
043     * Constructs a LedgerPostingDocumentBase.java.
044     */
045    public LedgerPostingDocumentBase() {
046        super();
047        if ( SpringContext.isInitialized() ) {
048            createInitialAccountingPeriod();
049        }
050    }
051
052    /**
053     * Used during initialization to provide a base <code>{@link AccountingPeriod}</code>.<br/>
054     * <p>
055     * This is a hack right now because its intended to be set by the
056     * <code>{@link org.kuali.ole.coa.service.AccountingPeriodService}</code>
057     * 
058     * @return AccountingPeriod
059     */
060    public void createInitialAccountingPeriod() { 
061        AccountingPeriod accountingPeriod = retrieveCurrentAccountingPeriod();
062        setAccountingPeriod(accountingPeriod);
063    }
064    
065    /**
066     * Finds the accounting period for the current date
067     * @return the current accounting period
068     */
069    public AccountingPeriod retrieveCurrentAccountingPeriod() {
070        // CSU 6702 BEGIN
071        try {
072        // CSU 6702 END         
073        Date date = getDateTimeService().getCurrentSqlDate();
074        return getAccountingPeriodService().getByDate(date);
075        // CSU 6702 BEGIN
076        } catch ( RuntimeException ex ) {
077            // catch and ignore - prevent blowup when called before services initialized
078            return null;
079        }
080        // CSU 6702 END
081    }
082
083    /**
084     * @see org.kuali.ole.sys.document.LedgerPostingDocument#getPostingYear()
085     */
086    public Integer getPostingYear() {
087        return postingYear;
088    }
089
090    /**
091     * @see org.kuali.ole.sys.document.LedgerPostingDocument#setPostingYear(java.lang.Integer)
092     */
093    public void setPostingYear(Integer postingYear) {
094        this.postingYear = postingYear;
095    }
096
097    /**
098     * @see org.kuali.ole.sys.document.LedgerPostingDocument#getPostingPeriodCode()
099     */
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}