001: /*
002: *
003: * JMoney - A Personal Finance Manager
004: * Copyright (c) 2002 Johann Gyger <johann.gyger@switzerland.org>
005: * Copyright (c) 2004 Nigel Westbury <westbury@users.sourceforge.net>
006: *
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License as published by
010: * the Free Software Foundation; either version 2 of the License, or
011: * (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package net.sf.jmoney.model2;
025:
026: import java.util.Calendar;
027: import java.util.Date;
028:
029: import net.sf.jmoney.JMoneyPlugin;
030:
031: /**
032: * The data model for an entry.
033: */
034: public final class Entry extends ExtendableObject {
035:
036: protected long creation = Calendar.getInstance().getTime()
037: .getTime();
038:
039: protected String check = null;
040:
041: protected Date valuta = null;
042:
043: /**
044: * Element: Account
045: */
046: protected IObjectKey accountKey = null;
047:
048: protected long amount = 0;
049:
050: protected String memo = null;
051:
052: /**
053: * Applicable only if the account is an IncomeExpenseAccount
054: * and the multi-currency property in the account is set.
055: * <P>
056: * Element: Currency
057: */
058: protected IObjectKey incomeExpenseCurrencyKey = null;
059:
060: /**
061: * Constructor used by datastore plug-ins to create
062: * an entry object.
063: *
064: * Note that the entry constructed by this constructor
065: * may be invalid. For example, it is possible that a
066: * null account is set. It is the callers responsibility
067: * to ensure that an account is set before it relinquishes
068: * control to other plug-ins.
069: *
070: * @param parent The key to a Transaction object.
071: * This parameter must be non-null.
072: * The getObject method must not be called on this
073: * key from within this constructor because the
074: * key may not yet be in a state in which it is
075: * capable of materializing an object.
076: */
077: public Entry(IObjectKey objectKey, ListKey parentKey, String check,
078: IObjectKey accountKey, Date valuta, String memo,
079: long amount, long creation,
080: IObjectKey incomeExpenseCurrencyKey, IValues extensionValues) {
081: super (objectKey, parentKey, extensionValues);
082:
083: if (creation == 0) {
084: this .creation = Calendar.getInstance().getTime().getTime();
085: } else {
086: this .creation = creation;
087: }
088: this .check = check;
089: this .valuta = valuta;
090: this .accountKey = accountKey;
091: this .amount = amount;
092: this .memo = memo;
093: this .incomeExpenseCurrencyKey = incomeExpenseCurrencyKey;
094: }
095:
096: /**
097: * Constructor used by datastore plug-ins to create
098: * an entry object.
099: *
100: * Note that the entry constructed by this constructor
101: * may be invalid. For example, it is possible that a
102: * null account is set. It is the callers responsibility
103: * to ensure that an account is set before it relinquishes
104: * control to other plug-ins.
105: *
106: * @param parent The key to a Transaction object.
107: * This parameter must be non-null.
108: * The getObject method must not be called on this
109: * key from within this constructor because the
110: * key may not yet be in a state in which it is
111: * capable of materializing an object.
112: */
113: public Entry(IObjectKey objectKey, ListKey parentKey) {
114: super (objectKey, parentKey);
115:
116: this .creation = Calendar.getInstance().getTime().getTime();
117: this .check = null;
118: this .valuta = null;
119: this .accountKey = null;
120: this .amount = 0;
121: this .memo = null;
122: this .incomeExpenseCurrencyKey = null;
123: }
124:
125: @Override
126: protected String getExtendablePropertySetId() {
127: return "net.sf.jmoney.entry";
128: }
129:
130: /**
131: * Returns the transaction.
132: */
133: public Transaction getTransaction() {
134: return (Transaction) parentKey.getParentKey().getObject();
135: }
136:
137: /**
138: * Returns the creation.
139: */
140: public long getCreation() {
141: return creation;
142: }
143:
144: /**
145: * Returns the check.
146: */
147: public String getCheck() {
148: return check;
149: }
150:
151: /**
152: * Returns the valuta.
153: */
154: public Date getValuta() {
155: return valuta;
156: }
157:
158: /**
159: * Returns the account.
160: */
161: public Account getAccount() {
162: if (accountKey == null) {
163: return null;
164: } else {
165: return (Account) accountKey.getObject();
166: }
167: }
168:
169: /**
170: * Returns the currency in which the amount in this entry is denominated.
171: * This property is applicable if and only if the account for this entry
172: * is an IncomeExpenseAccount and the multi-currency property in the account
173: * is set.
174: */
175: public Currency getIncomeExpenseCurrency() {
176: if (incomeExpenseCurrencyKey == null) {
177: return null;
178: } else {
179: return (Currency) incomeExpenseCurrencyKey.getObject();
180: }
181: }
182:
183: /**
184: * returns the other account of the transaction associated with this
185: * entry. If the transaction is a splitted one, there are several "other
186: * accounts", and the returned value is "null".
187: */
188: public Account getOtherAccount() {
189: if (getTransaction().hasTwoEntries()) {
190: return getTransaction().getOther(this ).getAccount();
191: } else {
192: return null;
193: }
194: }
195:
196: public String getFullAccountName() {
197: if (getTransaction().hasTwoEntries()) {
198: Account category = getTransaction().getOther(this )
199: .getAccount();
200: if (category == null) {
201: return null;
202: } else {
203: return category.getFullAccountName();
204: }
205: } else if (getTransaction().hasMoreThanTwoEntries()) {
206: // TODO: get rid of this message from here,
207: // and move text from jmoney to jmoney.accountentriespanel
208: return JMoneyPlugin.getResourceString("SplitCategory.name");
209: } else {
210: return null;
211: }
212: }
213:
214: /**
215: * Returns the amount.
216: */
217: public long getAmount() {
218: return amount;
219: }
220:
221: /**
222: * Returns the memo.
223: */
224: public String getMemo() {
225: return memo;
226: }
227:
228: /**
229: * @return The commodity for this entry, or null if not enough
230: * information has been set to determine the commodity.
231: */
232: public Commodity getCommodity() {
233: if (getAccount() == null) {
234: return null;
235: } else {
236: return getAccount().getCommodity(this );
237: }
238: }
239:
240: /**
241: * Sets the creation.
242: */
243: public void setCreation(long aCreation) {
244: long oldCreation = this .creation;
245: creation = aCreation;
246:
247: // Notify the change manager.
248: processPropertyChange(EntryInfo.getCreationAccessor(),
249: new Long(oldCreation), new Long(creation));
250: }
251:
252: /**
253: * Sets the check.
254: */
255: public void setCheck(String aCheck) {
256: String oldCheck = this .check;
257: check = (aCheck != null && aCheck.length() == 0) ? null
258: : aCheck;
259:
260: // Notify the change manager.
261: processPropertyChange(EntryInfo.getCheckAccessor(), oldCheck,
262: check);
263: }
264:
265: /**
266: * Sets the valuta.
267: */
268: public void setValuta(Date aValuta) {
269: Date oldValuta = this .valuta;
270: valuta = aValuta;
271:
272: // Notify the change manager.
273: processPropertyChange(EntryInfo.getValutaAccessor(), oldValuta,
274: valuta);
275: }
276:
277: /**
278: * Sets the account.
279: */
280: public void setAccount(Account newAccount) {
281: Account oldAccount = accountKey == null ? null
282: : (Account) accountKey.getObject();
283:
284: // TODO: This is not efficient. Better would be to pass
285: // an object key as the old value to the property change
286: // method. Then the object is materialized only if
287: // necessary.
288: // NOTE: Even though a null account is not valid, we support
289: // the setting of it because code may potentially need to do this
290: // in order to, say, delete the account before the new account
291: // of the entry is known.
292: accountKey = newAccount == null ? null : newAccount
293: .getObjectKey();
294:
295: // Notify the change manager.
296: processPropertyChange(EntryInfo.getAccountAccessor(),
297: oldAccount, newAccount);
298: }
299:
300: /**
301: * Sets the amount.
302: */
303: public void setAmount(long anAmount) {
304: long oldAmount = this .amount;
305: amount = anAmount;
306:
307: // Notify the change manager.
308: processPropertyChange(EntryInfo.getAmountAccessor(), oldAmount,
309: amount);
310: }
311:
312: /**
313: * Sets the memo.
314: */
315: public void setMemo(String aMemo) {
316: String oldMemo = this .memo;
317: this .memo = (aMemo != null && aMemo.length() == 0) ? null
318: : aMemo;
319:
320: // Notify the change manager.
321: processPropertyChange(EntryInfo.getMemoAccessor(), oldMemo,
322: memo);
323: }
324:
325: /**
326: * Sets the currency in which the amount in this entry is denominated.
327: * This property is applicable if and only if the account for this entry
328: * is an IncomeExpenseAccount and the multi-currency property in the account
329: * is set.
330: */
331: public void setIncomeExpenseCurrency(Currency incomeExpenseCurrency) {
332: Currency oldIncomeExpenseCurrency = incomeExpenseCurrencyKey == null ? null
333: : (Currency) incomeExpenseCurrencyKey.getObject();
334:
335: // TODO: This is not efficient. Better would be to pass
336: // an object key as the old value to the property change
337: // method. Then the object is materialized only if
338: // necessary.
339: incomeExpenseCurrencyKey = incomeExpenseCurrency == null ? null
340: : incomeExpenseCurrency.getObjectKey();
341:
342: // Notify the change manager.
343: processPropertyChange(EntryInfo
344: .getIncomeExpenseCurrencyAccessor(),
345: oldIncomeExpenseCurrency, incomeExpenseCurrency);
346: }
347: }
|