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.reconciliation;
025:
026: import java.util.regex.Pattern;
027: import java.util.regex.PatternSyntaxException;
028:
029: import net.sf.jmoney.model2.Account;
030: import net.sf.jmoney.model2.Currency;
031: import net.sf.jmoney.model2.ExtendableObject;
032: import net.sf.jmoney.model2.IObjectKey;
033: import net.sf.jmoney.model2.IValues;
034: import net.sf.jmoney.model2.ListKey;
035:
036: /**
037: * The data model for an entry.
038: */
039: public final class MemoPattern extends ExtendableObject {
040:
041: protected int orderingIndex = 0;
042:
043: protected String pattern = null;
044:
045: protected String check = null;
046:
047: protected String description = null;
048:
049: /**
050: * Element: Account
051: */
052: IObjectKey accountKey = null;
053:
054: protected String memo = null;
055:
056: /*
057: * The compiled pattern. This is not a property but
058: * it is compiled each time an object of this class is constructed.
059: */
060: Pattern compiledPattern = null;
061:
062: /**
063: * Applicable only if the account is an IncomeExpenseAccount
064: * and the multi-currency property in the account is set.
065: * <P>
066: * Element: Currency
067: */
068: // TODO: same comment as for account above.
069: public IObjectKey incomeExpenseCurrencyKey = null;
070:
071: /**
072: * Constructor used by datastore plug-ins to create
073: * a pattern object.
074: *
075: * @param parent The key to a Transaction object.
076: * This parameter must be non-null.
077: * The getObject method must not be called on this
078: * key from within this constructor because the
079: * key may not yet be in a state in which it is
080: * capable of materializing an object.
081: */
082: public MemoPattern(IObjectKey objectKey, ListKey parentKey,
083: int orderingIndex, String pattern, String check,
084: String description, IObjectKey accountKey, String memo,
085: IObjectKey incomeExpenseCurrencyKey, IValues extensionValues) {
086: super (objectKey, parentKey, extensionValues);
087:
088: this .orderingIndex = orderingIndex;
089: this .pattern = pattern;
090: this .check = check;
091: this .description = description;
092: this .accountKey = accountKey;
093: this .memo = memo;
094: this .incomeExpenseCurrencyKey = incomeExpenseCurrencyKey;
095:
096: if (pattern != null) {
097: try {
098: compiledPattern = Pattern.compile(pattern);
099: } catch (PatternSyntaxException e) {
100: compiledPattern = null;
101: }
102: }
103: }
104:
105: /**
106: * Constructor used by datastore plug-ins to create
107: * a pattern object.
108: *
109: * @param parent The key to a Transaction object.
110: * This parameter must be non-null.
111: * The getObject method must not be called on this
112: * key from within this constructor because the
113: * key may not yet be in a state in which it is
114: * capable of materializing an object.
115: */
116: public MemoPattern(IObjectKey objectKey, ListKey parentKey) {
117: super (objectKey, parentKey);
118:
119: this .orderingIndex = 0;
120: this .pattern = null;
121: this .check = null;
122: this .description = null;
123: this .accountKey = null;
124: this .memo = null;
125: this .incomeExpenseCurrencyKey = null;
126: }
127:
128: @Override
129: protected String getExtendablePropertySetId() {
130: return "net.sf.jmoney.reconciliation.pattern";
131: }
132:
133: /**
134: * Returns the index which specifies the order in which
135: * patterns should be tried.
136: */
137: public int getOrderingIndex() {
138: return orderingIndex;
139: }
140:
141: /**
142: * Returns the pattern.
143: */
144: public String getPattern() {
145: return pattern;
146: }
147:
148: /**
149: * Returns the check.
150: */
151: public String getCheck() {
152: return check;
153: }
154:
155: /**
156: * Returns the description.
157: */
158: public String getDescription() {
159: return description;
160: }
161:
162: /**
163: * Returns the account.
164: */
165: public Account getAccount() {
166: if (accountKey == null) {
167: return null;
168: } else {
169: return (Account) accountKey.getObject();
170: }
171: }
172:
173: /**
174: * Returns the currency in which the amount in this entry is denominated.
175: * This property is applicable if and only if the account for this entry
176: * is an IncomeExpenseAccount and the multi-currency property in the account
177: * is set.
178: */
179: public Currency getIncomeExpenseCurrency() {
180: if (incomeExpenseCurrencyKey == null) {
181: return null;
182: } else {
183: return (Currency) incomeExpenseCurrencyKey.getObject();
184: }
185: }
186:
187: /**
188: * Returns the memo.
189: */
190: public String getMemo() {
191: return memo;
192: }
193:
194: /**
195: * Sets the index which specifies the order in which
196: * patterns should be tried.
197: */
198: public void setOrderingIndex(int orderingIndex) {
199: int oldOrderingIndex = this .orderingIndex;
200: this .orderingIndex = orderingIndex;
201:
202: // Notify the change manager.
203: processPropertyChange(MemoPatternInfo
204: .getOrderingIndexAccessor(), oldOrderingIndex,
205: orderingIndex);
206: }
207:
208: /**
209: * Sets the pattern.
210: */
211: public void setPattern(String pattern) {
212: String oldPattern = this .pattern;
213: this .pattern = pattern;
214:
215: if (pattern != null) {
216: try {
217: compiledPattern = Pattern.compile(pattern);
218: } catch (PatternSyntaxException e) {
219: compiledPattern = null;
220: }
221: } else {
222: compiledPattern = null;
223: }
224:
225: // Notify the change manager.
226: processPropertyChange(MemoPatternInfo.getPatternAccessor(),
227: oldPattern, pattern);
228: }
229:
230: /**
231: * Sets the check.
232: */
233: public void setCheck(String aCheck) {
234: String oldCheck = this .check;
235: check = (aCheck != null && aCheck.length() == 0) ? null
236: : aCheck;
237:
238: // Notify the change manager.
239: processPropertyChange(MemoPatternInfo.getCheckAccessor(),
240: oldCheck, check);
241: }
242:
243: /**
244: * Sets the description.
245: */
246: public void setDescription(String aDescription) {
247: String oldDescription = this .description;
248: description = (aDescription != null && aDescription.length() == 0) ? null
249: : aDescription;
250:
251: // Notify the change manager.
252: processPropertyChange(MemoPatternInfo.getDescriptionAccessor(),
253: oldDescription, description);
254: }
255:
256: /**
257: * Sets the account.
258: */
259: public void setAccount(Account newAccount) {
260: Account oldAccount = accountKey == null ? null
261: : (Account) accountKey.getObject();
262:
263: // TODO: This is not efficient. Better would be to pass
264: // an object key as the old value to the property change
265: // method. Then the object is materialized only if
266: // necessary.
267: // NOTE: Even though a null account is not valid, we support
268: // the setting of it because code may potentially need to do this
269: // in order to, say, delete the account before the new account
270: // of the entry is known.
271: accountKey = newAccount == null ? null : newAccount
272: .getObjectKey();
273:
274: // Notify the change manager.
275: processPropertyChange(MemoPatternInfo.getAccountAccessor(),
276: oldAccount, newAccount);
277: }
278:
279: /**
280: * Sets the memo.
281: */
282: public void setMemo(String aMemo) {
283: String oldMemo = this .memo;
284: this .memo = (aMemo != null && aMemo.length() == 0) ? null
285: : aMemo;
286:
287: // Notify the change manager.
288: processPropertyChange(MemoPatternInfo.getMemoAccessor(),
289: oldMemo, memo);
290: }
291:
292: /**
293: * Sets the currency in which the amount in this entry is denominated.
294: * This property is applicable if and only if the account for this entry
295: * is an IncomeExpenseAccount and the multi-currency property in the account
296: * is set.
297: */
298: public void setIncomeExpenseCurrency(Currency incomeExpenseCurrency) {
299: Currency oldIncomeExpenseCurrency = incomeExpenseCurrencyKey == null ? null
300: : (Currency) incomeExpenseCurrencyKey.getObject();
301:
302: // TODO: This is not efficient. Better would be to pass
303: // an object key as the old value to the property change
304: // method. Then the object is materialized only if
305: // necessary.
306: incomeExpenseCurrencyKey = incomeExpenseCurrency == null ? null
307: : incomeExpenseCurrency.getObjectKey();
308:
309: // Notify the change manager.
310: processPropertyChange(MemoPatternInfo
311: .getIncomeExpenseCurrencyAccessor(),
312: oldIncomeExpenseCurrency, incomeExpenseCurrency);
313: }
314:
315: public Pattern getCompiledPattern() {
316: return compiledPattern;
317: }
318: }
|