001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.document;
017:
018: import java.sql.Date;
019:
020: import org.apache.commons.lang.StringUtils;
021: import org.kuali.core.document.TransactionalDocumentBase;
022: import org.kuali.core.service.DateTimeService;
023: import org.kuali.core.util.NumberUtils;
024: import org.kuali.core.util.ObjectUtils;
025: import org.kuali.kfs.context.SpringContext;
026: import org.kuali.module.chart.bo.AccountingPeriod;
027: import org.kuali.module.chart.service.AccountingPeriodService;
028: import org.kuali.module.financial.service.UniversityDateService;
029:
030: /**
031: * Base implementation for a ledger posting document.
032: */
033: public class LedgerPostingDocumentBase extends
034: TransactionalDocumentBase implements LedgerPostingDocument {
035: private Integer tmpPostingYear;
036: private String tmpPostingPeriodCode;
037:
038: protected AccountingPeriod accountingPeriod;
039: protected Integer postingYear;
040: protected String postingPeriodCode;
041: protected boolean checkPostingYearForCopy;
042:
043: /**
044: * Constructs a LedgerPostingDocumentBase.java.
045: */
046: public LedgerPostingDocumentBase() {
047: super ();
048: createInitialAccountingPeriod();
049: }
050:
051: /**
052: * Used during initialization to provide a base <code>{@link AccountingPeriod}</code>.<br/>
053: * <p>
054: * This is a hack right now because its intended to be set by the
055: * <code>{@link org.kuali.module.chart.service.AccountingPeriodService}</code>
056: *
057: * @return AccountingPeriod
058: */
059: private void createInitialAccountingPeriod() {
060: Date date = SpringContext.getBean(DateTimeService.class)
061: .getCurrentSqlDate();
062: AccountingPeriod accountingPeriod = SpringContext.getBean(
063: AccountingPeriodService.class).getByDate(date);
064:
065: setAccountingPeriod(accountingPeriod);
066: }
067:
068: /**
069: * @see org.kuali.kfs.document.LedgerPostingDocument#getPostingYear()
070: */
071: public Integer getPostingYear() {
072: return postingYear;
073: }
074:
075: /**
076: * @see org.kuali.kfs.document.LedgerPostingDocument#setPostingYear(java.lang.Integer)
077: */
078: public void setPostingYear(Integer postingYear) {
079: this .tmpPostingYear = postingYear;
080: handleAccountingPeriodChange();
081: }
082:
083: /**
084: * @see org.kuali.kfs.document.LedgerPostingDocument#getPostingPeriodCode()
085: */
086: public String getPostingPeriodCode() {
087: return postingPeriodCode;
088: }
089:
090: /**
091: * @see org.kuali.kfs.document.LedgerPostingDocument#setPostingPeriodCode(java.lang.String)
092: */
093: public void setPostingPeriodCode(String postingPeriodCode) {
094: this .tmpPostingPeriodCode = postingPeriodCode;
095: handleAccountingPeriodChange();
096: }
097:
098: /**
099: * @see org.kuali.kfs.document.LedgerPostingDocument#getAccountingPeriod()
100: */
101: public AccountingPeriod getAccountingPeriod() {
102: return accountingPeriod;
103: }
104:
105: /**
106: * @see org.kuali.kfs.document.LedgerPostingDocument#setAccountingPeriod(AccountingPeriod)
107: */
108: public void setAccountingPeriod(AccountingPeriod accountingPeriod) {
109: Integer postingYear = null;
110: String postingPeriodCode = null;
111: if (accountingPeriod != null) {
112: postingYear = accountingPeriod.getUniversityFiscalYear();
113: postingPeriodCode = accountingPeriod
114: .getUniversityFiscalPeriodCode();
115: }
116: setPostingYear(postingYear);
117: setPostingPeriodCode(postingPeriodCode);
118: }
119:
120: /**
121: * Uses <code>{@link AccountingPeriod}</code> key to set new key values at once. <br/>
122: * <p>
123: * This is called whenever <code>postingYear</code> or <code>postingPeriodCode</code> is changed. If neither value in the
124: * key is null, then a change has occured and the values are handled at once.
125: * </p>
126: */
127: private void handleAccountingPeriodChange() {
128: Integer year = this .tmpPostingYear;
129: String code = this .tmpPostingPeriodCode;
130:
131: if (year != null && StringUtils.isNotBlank(code)) {
132: AccountingPeriod accountingPeriod = SpringContext.getBean(
133: AccountingPeriodService.class).getByPeriod(code,
134: year);
135: if (ObjectUtils.isNotNull(accountingPeriod)) {
136: accountingPeriod.refresh();
137: this .accountingPeriod = accountingPeriod;
138: this .postingPeriodCode = code;
139: this .postingYear = year;
140: this .tmpPostingPeriodCode = null;
141: this .tmpPostingYear = null;
142: }
143: }
144: }
145:
146: /**
147: * @see org.kuali.core.document.TransactionalDocumentBase#getAllowsErrorCorrection() Checks the condition the posting year of
148: * the original document is current fiscal year.
149: */
150: @Override
151: public boolean getAllowsErrorCorrection() {
152: boolean allowsCorrection = super .getAllowsErrorCorrection();
153:
154: Integer fiscalYear = SpringContext.getBean(
155: UniversityDateService.class).getCurrentFiscalYear();
156: if (!NumberUtils.equals(fiscalYear, getPostingYear())) {
157: allowsCorrection = false;
158: }
159:
160: return allowsCorrection;
161: }
162: }
|