001: package net.sf.jmoney.charts;
002:
003: import java.util.Collections;
004: import java.util.Comparator;
005: import java.util.Date;
006: import java.util.Iterator;
007: import java.util.LinkedList;
008: import java.util.List;
009: import java.util.Vector;
010:
011: import net.sf.jmoney.JMoneyPlugin;
012: import net.sf.jmoney.model2.Account;
013: import net.sf.jmoney.model2.CapitalAccount;
014: import net.sf.jmoney.model2.Entry;
015: import net.sf.jmoney.model2.IEntryQueries;
016: import net.sf.jmoney.model2.Session;
017:
018: import org.eclipse.core.runtime.Assert;
019: import org.jfree.data.time.RegularTimePeriod;
020:
021: /**
022: * Toolbox-Class for the access to the dataes.
023: * This class won't be instanced.
024: *
025: * TODO - Faucheux This class should be defined as a plug-in
026: * @author Faucheux
027: */
028: public class Util {
029:
030: /**
031: * returns as Vector the list (not ordered) the accounts till the wished level (0 are the 'root' accounts)
032: * @author Faucheux
033: */
034: public static Vector getAccountsUntilLevel(Session session,
035: int level) {
036: Vector v = new Vector();
037: Iterator it = session.getAccountCollection().iterator();
038: while (it.hasNext()) {
039: Account a = (Account) it.next();
040: v.add(a);
041: addSubAccountsUntilLevelToVector(a, level, v);
042: }
043:
044: return v;
045: }
046:
047: private static void addSubAccountsUntilLevelToVector(Account a,
048: int level, Vector v) {
049: if (level == 0)
050: return;
051: Iterator it = a.getSubAccountCollection().iterator();
052: while (it.hasNext()) {
053: Account sa = (Account) it.next();
054: v.add(sa);
055: addSubAccountsUntilLevelToVector(sa, level - 1, v);
056: }
057: }
058:
059: /**
060: * @author Faucheux
061: */
062: public static Account getAccountByFullName(Session session,
063: String name) throws Error {
064:
065: Account a = null;
066: String begin = null;
067: String rest = null;
068: Account result = null;
069: if (name.indexOf('.') != -1) {
070: rest = name.substring(name.indexOf('.') + 1);
071: begin = name.substring(0, name.indexOf('.'));
072: } else {
073: begin = name;
074: }
075: Iterator it = session.getAccountCollection().iterator();
076: while (it.hasNext()) {
077: a = (Account) it.next();
078: if (ChartsPlugin.DEBUG)
079: System.out.println("Compare " + begin + " to "
080: + a.getName());
081: if (a.getName().equals(begin)) {
082: result = getSubAccountByFullName(a, rest);
083: break;
084: }
085: }
086: if (result == null)
087: throw new Error("The account >" + name + "< can't be found");
088: return result;
089: }
090:
091: private static Account getSubAccountByFullName(Account a,
092: String name) {
093: Account sa = null;
094: String begin = null;
095: String rest = null;
096: Account result = null;
097:
098: if (name == null) {
099: result = a;
100: } else {
101: if (name.indexOf('.') != -1) {
102: begin = name.substring(0, name.indexOf('.'));
103: rest = name.substring(name.indexOf('.') + 1);
104: } else {
105: begin = name;
106: }
107: Iterator it = a.getSubAccountCollection().iterator();
108: while (it.hasNext()) {
109: sa = (Account) it.next();
110: if (ChartsPlugin.DEBUG)
111: System.out.println("Compare " + begin + " to "
112: + sa.getName());
113: if (sa.getName().equals(begin))
114: result = getSubAccountByFullName(sa, rest);
115: }
116: }
117:
118: return result;
119: }
120:
121: public static List getEntriesFromAccount(Session session,
122: CapitalAccount a) {
123:
124: List entries = new LinkedList();
125:
126: Iterator it = a.getEntries().iterator();
127: while (it.hasNext()) {
128: entries.add(it.next());
129: }
130:
131: return entries;
132: }
133:
134: public static List getEntriesFromAccountBetweenDates(
135: Session session, CapitalAccount a, Date fromDate,
136: Date toDate) {
137:
138: List entries = new LinkedList();
139: Iterator it = a.getEntries().iterator();
140: while (it.hasNext()) {
141: Entry e = (Entry) it.next();
142: if (e.getTransaction().getDate().after(fromDate)
143: && e.getTransaction().getDate().before(toDate))
144: entries.add(e);
145: }
146:
147: return entries;
148: }
149:
150: /**
151: * return the list of the entries of an account, chronlogicaly ordered.
152: * @param session
153: * @param a
154: * @return
155: */
156: public static List sortChronogicalyEntries(List entries) {
157:
158: Collections.sort(entries, new Comparator() {
159: public int compare(Object a, Object b) {
160: return ((Entry) a).getTransaction().getDate()
161: .compareTo(
162: ((Entry) b).getTransaction().getDate());
163: }
164: });
165:
166: return entries;
167: }
168:
169: /**
170: * return a list of all entries of the given account and its subaccounts.
171: * @param session
172: * @param a
173: * @return
174: */
175: public static List getEntriesFromAccountAndSubaccounts(
176: Session session, CapitalAccount a) {
177: if (ChartsPlugin.DEBUG)
178: System.out
179: .println("getEntriesFromAccountAndSubaccounts for "
180: + a.getName());
181: List entries = getEntriesFromAccount(session, a);
182: Iterator it = a.getSubAccountCollection().iterator();
183: while (it.hasNext()) {
184: CapitalAccount subaccount = (CapitalAccount) it.next();
185: entries.addAll(getEntriesFromAccountAndSubaccounts(session,
186: subaccount));
187: }
188: return entries;
189: }
190:
191: /**
192: * return a list of all directs subaccounts.
193: * @param session
194: * @param a
195: * @return
196: */
197: public static List getSubAccounts(CapitalAccount a) {
198: if (ChartsPlugin.DEBUG)
199: System.out.println("getSubAccounts for " + a.getName());
200: return getSubAccountsUntilLevel(a, a.getLevel() + 1);
201: }
202:
203: /**
204: * return a list of all subaccounts until the given level.
205: * @param session
206: * @param a
207: * @return
208: */
209: public static List getSubAccountsUntilLevel(CapitalAccount a,
210: int level) {
211: if (ChartsPlugin.DEBUG)
212: System.out.println("getSubAccountsUntilLevel for "
213: + a.getName() + ", Level " + level);
214: List subaccounts = new LinkedList();
215: if (a.getLevel() < level) {
216: Iterator it = a.getSubAccountCollection().iterator();
217: while (it.hasNext()) {
218: CapitalAccount sa = (CapitalAccount) it.next();
219: subaccounts.add(sa);
220: subaccounts.addAll(getSubAccountsUntilLevel(sa, level));
221: }
222: }
223: return subaccounts;
224: }
225:
226: /**
227: * Return the sum of the mouvements between the two dates ; the list must be chronogically
228: * sorted !
229: *
230: * @param mvt
231: * @param fromDate
232: * @param toDate
233: * @return
234: */
235: public static long getSumMouvementsBetweenDates(List mvt,
236: Date fromDate, Date toDate) {
237: long sum = 0;
238:
239: Iterator it = mvt.iterator();
240: while (it.hasNext()) {
241: Entry e = (Entry) it.next();
242: if (e.getTransaction().getDate().after(toDate))
243: break;
244: if (!e.getTransaction().getDate().before(fromDate))
245: sum += e.getAmount();
246: }
247: return sum / 100;
248: }
249:
250: /**
251: * @param date
252: * @param date2
253: * @param includeSubAccounts
254: * @return
255: */
256: public static long[] getEntryTotalsByPeriod(CapitalAccount a,
257: RegularTimePeriod startPeriod, int numberOfPeriods,
258: boolean includeSubAccounts) {
259: IEntryQueries queries = (IEntryQueries) a.getSession()
260: .getAdapter(IEntryQueries.class);
261: if (queries != null) {
262: // TODO: correct it when the IEntryQueries are really implemented
263: // return queries.getEntryTotalsByMonth(this, startYear, startMonth, numberOfMonths, includeSubAccounts);
264: long[] totals = new long[1];
265: return totals;
266: } else {
267: // IEntryQueries has not been implemented in the datastore.
268: // We must therefore provide our own implementation.
269:
270: Vector entriesList = new Vector();
271: entriesList.addAll(a.getEntries());
272: if (includeSubAccounts) {
273: a.addEntriesFromSubAccounts(a, entriesList);
274: }
275:
276: Collections.sort(entriesList, new Comparator() {
277: public int compare(Object a, Object b) {
278: Assert.isTrue(a instanceof Entry);
279: Assert.isTrue(b instanceof Entry);
280: return ((Entry) a).getTransaction().getDate()
281: .compareTo(
282: ((Entry) b).getTransaction()
283: .getDate());
284: }
285: });
286:
287: long[] totals = new long[numberOfPeriods];
288:
289: // calculate the sum for each period
290: RegularTimePeriod period = startPeriod;
291: for (int i = 0; i < numberOfPeriods; i++) {
292: Date startOfPeriod = new Date(period
293: .getFirstMillisecond());
294: Date endOfPeriod = new Date(period.getLastMillisecond());
295: period = period.next();
296:
297: int total = 0;
298: for (Iterator iter = entriesList.iterator(); iter
299: .hasNext();) {
300: Entry entry = (Entry) iter.next();
301: if (ChartsPlugin.DEBUG)
302: System.out.println(entry.getTransaction()
303: .getDate());
304: if (entry.getTransaction().getDate().compareTo(
305: startOfPeriod) >= 0
306: && entry.getTransaction().getDate()
307: .compareTo(endOfPeriod) < 0) {
308: total += entry.getAmount();
309: }
310: }
311: totals[i] = total / 100;
312: }
313:
314: return totals;
315: }
316: }
317:
318: }
|