001: /*
002: *
003: * JMoney - A Personal Finance Manager
004: * Copyright (c) 2007 Nigel Westbury <westbury@users.sf.net>
005: *
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: *
021: */
022:
023: package net.sf.jmoney.entrytable;
024:
025: import java.util.ArrayList;
026: import java.util.Collection;
027:
028: import net.sf.jmoney.model2.DataManager;
029: import net.sf.jmoney.model2.Entry;
030:
031: import org.eclipse.core.runtime.Assert;
032:
033: /**
034: * Class representing a top level entry in the list.
035: * <P>
036: * Note that it is entries and not transactions that are listed. For example, if
037: * a transaction has two entries in the account then that transaction will
038: * appear twice in the list.
039: */
040: public class EntryData {
041: /**
042: * The entry represented by this row. This entry will be null if the row
043: * represents a new entry that has never been committed to the datastore.
044: * Row controls will generally create a datastore transaction in which to
045: * edit this entry. However, this entry will be the committed version of
046: * the entry.
047: */
048: private Entry entry;
049:
050: private DataManager dataManager;
051:
052: /**
053: * The balance before this entry is added in, so the balance shown to the
054: * user for this line is calculated by adding this balance property to the
055: * amount of this entry.
056: */
057: private long balance;
058:
059: private int index;
060:
061: /**
062: * @param entry
063: * the entry to be edited, or null if a new entry is to be
064: * created
065: * @param dataManager
066: * the datastore manager into which the entry will be committed,
067: * which must be the same as the datastore manager for the entry
068: * parameter if the entry parameter is non-null
069: */
070: public EntryData(Entry entry, DataManager dataManager) {
071: this .entry = entry;
072: this .dataManager = dataManager;
073: }
074:
075: public void setIndex(int index) {
076: this .index = index;
077: }
078:
079: public int getIndex() {
080: return index;
081: }
082:
083: /**
084: * A transaction with split entries is a transaction that
085: * has entries in three or more accounts (where each account
086: * may be either a capital account or an income and
087: * expense category).
088: */
089: public boolean hasSplitEntries() {
090: return entry.getTransaction().getEntryCollection().size() >= 3;
091: }
092:
093: /**
094: * @return
095: */
096: public Entry getOtherEntry() {
097: Assert.isTrue(!hasSplitEntries());
098: return buildOtherEntriesList().get(0);
099: }
100:
101: /**
102: * @return
103: */
104: public Collection<Entry> getSplitEntries() {
105: return buildOtherEntriesList();
106: }
107:
108: /**
109: * @return the balance before this entry is added in, so
110: * the balance show to the user for this line is calculated
111: * by adding this balance property to the amount of this
112: * entry
113: */
114: public long getBalance() {
115: return balance;
116: }
117:
118: /**
119: * @param balance the balance before this entry is added in, so
120: * the balance show to the user for this line is calculated
121: * by adding this balance property to the amount of this
122: * entry
123: */
124: public void setBalance(long balance) {
125: this .balance = balance;
126: }
127:
128: public Entry getEntry() {
129: return entry;
130: }
131:
132: public DataManager getBaseSessionManager() {
133: return dataManager;
134: }
135:
136: /**
137: * Database reads may be necessary when getting the other entries.
138: * Furthermore, these are not needed unless an entry becomes visible. These
139: * are therefore fetched only when needed (not in the constructor of this
140: * object).
141: *
142: * We must be careful with this cached list because it is not kept up to date.
143: */
144: private ArrayList<Entry> buildOtherEntriesList() {
145: ArrayList<Entry> otherEntries = new ArrayList<Entry>();
146: for (Entry entry2 : entry.getTransaction().getEntryCollection()) {
147: if (!entry2.equals(entry)) {
148: otherEntries.add(entry2);
149: }
150: }
151: return otherEntries;
152: }
153: }
|