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.Date;
027:
028: /**
029: *
030: * @author Nigel
031: */
032: public class Transaction extends ExtendableObject {
033:
034: protected Date date = null;
035:
036: protected IListManager<Entry> entries;
037:
038: public Transaction(IObjectKey objectKey, ListKey parentKey,
039: IListManager<Entry> entries, Date date,
040: IValues extensionValues) {
041: super (objectKey, parentKey, extensionValues);
042:
043: this .entries = entries;
044: this .date = date;
045: }
046:
047: public Transaction(IObjectKey objectKey, ListKey parentKey) {
048: super (objectKey, parentKey);
049:
050: this .entries = objectKey.constructListManager(TransactionInfo
051: .getEntriesAccessor());
052:
053: // TODO: Check that this sets the date to the current date.
054: this .date = new Date();
055: }
056:
057: @Override
058: protected String getExtendablePropertySetId() {
059: return "net.sf.jmoney.transaction";
060: }
061:
062: /**
063: * Returns the date.
064: */
065: public Date getDate() {
066: return date;
067: }
068:
069: public void setDate(Date date) {
070: Date oldDate = this .date;
071: this .date = date;
072:
073: // Notify the change manager.
074: processPropertyChange(TransactionInfo.getDateAccessor(),
075: oldDate, date);
076: }
077:
078: public Entry createEntry() {
079: return new EntryCollection(entries, this , TransactionInfo
080: .getEntriesAccessor()).createEntry();
081: }
082:
083: public EntryCollection getEntryCollection() {
084: return new EntryCollection(entries, this , TransactionInfo
085: .getEntriesAccessor());
086: }
087:
088: public boolean deleteEntry(Entry entry) {
089: return new EntryCollection(entries, this , TransactionInfo
090: .getEntriesAccessor()).deleteEntry(entry);
091: }
092:
093: // Some helper methods:
094:
095: public boolean hasTwoEntries() {
096: return entries.size() == 2;
097: }
098:
099: public boolean hasMoreThanTwoEntries() {
100: return entries.size() > 2;
101: }
102:
103: /**
104: * Given an entry in the transaction, return the other entry.
105: * <P>
106: * If there are more than two entries in the transaction then null is returned.
107: * If there is only one entry in the transaction then an
108: * exception is throw. If the given entry is not in the transaction then
109: * an exception will be thrown.
110: *
111: * @param thisEntry an entry in the transaction
112: * @return the other entry in the transaction or null if more than one other entry
113: * is in the transaction
114: */
115: public Entry getOther(Entry this Entry) {
116: boolean this EntryFound = false;
117: Entry anotherEntry = null;
118: for (Entry entry : entries) {
119: if (!entry.equals(this Entry)) {
120: if (anotherEntry != null) {
121: // There is more than one entry other than the given entry
122: return null;
123: }
124: anotherEntry = entry;
125: } else {
126: this EntryFound = true;
127: }
128: }
129:
130: if (!this EntryFound) {
131: throw new RuntimeException("Double entry error");
132: }
133:
134: if (anotherEntry == null) {
135: throw new RuntimeException("Double entry error");
136: }
137:
138: return anotherEntry;
139: }
140:
141: /**
142: * This class adds a little tighter typing to ObjectCollection,
143: * but it is barely worth while having this class.
144: */
145: public class EntryCollection extends ObjectCollection<Entry> {
146: EntryCollection(IListManager<Entry> listManager,
147: ExtendableObject parent,
148: ListPropertyAccessor<Entry> listPropertyAccessor) {
149: super (listManager, parent, listPropertyAccessor);
150: }
151:
152: /**
153: * Identical to <code>remove</remove> but tighter typing
154: */
155: public boolean deleteEntry(Entry entry) {
156: return remove(entry);
157: }
158:
159: /**
160: * Identical to <code>createNewElement</remove> but tighter typing
161: */
162: public Entry createEntry() {
163: return createNewElement(EntryInfo.getPropertySet());
164: }
165: }
166: }
|