Source code: com/pjsofts/eurobudget/format/qif/QIFAccountItem.java
1 /*
2 * QIFAccountItem.java
3 *
4 */
5
6 package com.pjsofts.eurobudget.format.qif;
7
8 import com.pjsofts.eurobudget.DataModel;
9 import com.pjsofts.eurobudget.EBConstants;
10 import com.pjsofts.eurobudget.beans.Account;
11 import java.io.IOException;
12 import java.io.LineNumberReader;
13 import java.text.ParseException;
14 import java.util.Date;
15 import java.util.ResourceBundle;
16
17 /** Items for Account Information
18 * The account header !Account is used in two places-at the start of an account list and the start of a list of transactions to specify to which account they belong.
19 <PRE>
20 Items for Account Information
21 The account header !Account is used in two places-at the start of an account list and the start of a list of transactions to specify to which account they belong.
22
23 Field Indicator Explanation
24 N Name
25 T Type of account
26 D Description
27 L Credit limit (only for credit card accounts)
28 / Statement balance date
29 $ Statement balance amount
30 ^ End of entry
31 * </PRE>
32 */
33 public class QIFAccountItem {
34
35 private static final ResourceBundle i18n = EBConstants.i18n;
36
37 /** Creates a new instance of QIFAccountItem */
38 public QIFAccountItem() {
39 }
40 /** Name */
41 public String n = null;
42 /** type of account */
43 public String t = null;
44 /** Description */
45 public String d = null;
46 /** Credit limit (only for credit card accounts) */
47 public Double l = null;
48 /** Statement balance date */
49 public Date slash = null;
50 /** Statement balance amount */
51 public Double dollard = null;
52
53 public boolean isNull() {
54 return n == null && t == null && d == null
55 && l == null && slash == null && dollard == null;
56 }
57
58 /** convert a qif account item to an account
59 * @param dm data model (used to read current data not adding new data)
60 */
61 public Account toEBAccount(DataModel dm) {
62 Account account = new Account();//dm.getAccountFromString(first.tl);
63 account.setName(this.n == null ? i18n.getString("Account#")+dm.getAccounts().size() : this.n);
64 account.setInitialAmount(this.dollard == null ? 0.0d : this.dollard.doubleValue());
65 account.setCreationDate(this.slash == null ? null : this.slash);
66 //account.setCreditLimit(qifAccount.l == null ? null : qifAccount.l);
67 return account;
68 }
69
70 /** @return qif string for this account */
71 public static String toQIF(Account account) {
72 StringBuffer sb = new StringBuffer(50);
73 sb.append("!Account:\n");
74 sb.append("N"+account.getName()+"\n");//name
75 //sb.append("T"+account.get()+"\n");//type
76 if ( account.getComment() != null ) {
77 sb.append("D"+account.getComment()+"\n");//description
78 }
79 sb.append("L"+account.getMinAmount()+"\n");//credit limit
80 //Statement balance date
81 sb.append("/");
82 if ( account.getCreationDate() != null ) {
83 sb.append(QIF.DF.format(account.getCreationDate()));
84 } else {
85 sb.append("01/01/70");
86 }
87 sb.append("\n");
88 //Statement balance amount
89 sb.append("$");
90 sb.append(QIF.NF.format(account.getInitialAmount()));
91 sb.append("\n");
92 //^ End of entry
93 sb.append("^\n");
94 return sb.toString();
95 }
96
97 /**
98 * @param checkRuleDT true means check rules:D,T Mandatory
99 *
100 * @return qif lines in their respective variables or null
101 */
102 public static QIFAccountItem readItem(LineNumberReader br, boolean checkRuleDT) throws IOException, ParseException {
103 String line;
104 QIFAccountItem item = new QIFAccountItem();
105 line = br.readLine();
106 while ( line != null && !line.startsWith("^") ) {
107 if ( line.startsWith("N") ) {
108 line = line.trim().substring(1);
109 if ( item.n != null ) {
110 throw new ParseException(i18n.getString("QIF_error_N_duplicate"),br.getLineNumber());
111 } else {
112 item.n = line; //name
113 }
114 } else if ( line.startsWith("T") ) {
115 line = line.trim().substring(1);
116 if ( item.t != null ) {
117 throw new ParseException(i18n.getString("QIF_error_T_duplicate"),br.getLineNumber());
118 } else {
119 item.t = line; //type
120 }
121 } else if ( line.startsWith("D") ) {
122 line = line.trim().substring(1);
123 if ( item.d != null ) {
124 throw new ParseException(i18n.getString("QIF_error_D_duplicate"),br.getLineNumber());
125 } else {
126 item.d = line;
127 }
128 } else if ( line.startsWith("L") ) {
129 line = line.trim().substring(1);
130 if ( item.l != null ) {
131 throw new ParseException("QIF_error_L_duplicate",br.getLineNumber());
132 } else {
133 try {
134 item.l = new Double(QIF.NF.parse(line).doubleValue());
135 } catch (ParseException pe){
136 throw pe;
137 //throw new java.lang.Exception("Error at line "+br.getLineNumber(),pe);
138 }
139 }
140 } else if ( line.startsWith("/") ) {
141 line = line.trim().substring(1);
142 if ( item.slash != null ) {
143 throw new ParseException("QIF_error_statement_balance_date_duplicate",br.getLineNumber());
144 } else {
145 try {
146 line = line.replace('\'','/');//20yy, 19yy
147 item.slash = QIF.DF.parse(line);
148 } catch (ParseException pe){
149 throw pe;
150 // throw new java.lang.Exception("Error at line "+br.getLineNumber(),pe);
151 }
152 }
153 } else if ( line.startsWith("$") ) {
154 line = line.trim().substring(1);
155 if ( item.dollard != null ) {
156 throw new ParseException("QIF_error_statement_balance_amount_duplicate",br.getLineNumber());
157 } else {
158 try {
159 item.dollard = new Double(QIF.NF.parse(line).doubleValue());
160 } catch (ParseException pe){
161 throw pe;
162 //throw new java.lang.Exception("Error at line "+br.getLineNumber(),pe);
163 }
164 }
165 } else if ( line.length() == 0 ) {
166 // do nothing
167 } else throw new ParseException(i18n.getString("QIF_wrong_prefix")+line,br.getLineNumber());
168 line = br.readLine();
169 }
170 // Mandatory fields ?
171 // if ( checkRuleDT && !datas.isNull() && (datas.d == null || datas.t == null ) ) {
172 // throw new java.text.ParseException(i18n.getString("QIF_DT_mandatory"),br.getLineNumber());
173 // }
174 if ( item.isNull() )
175 return null;
176 return item;
177 }
178
179 }
180