1 package com.leonarduk.clearcheckbook.dto;
2
3 import java.util.Arrays;
4 import java.util.LinkedList;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.apache.log4j.Logger;
9
10 import com.leonarduk.clearcheckbook.ClearcheckbookException;
11
12 /***
13 *
14 *
15 *
16 * @author Stephen Leonard
17 * @since 28 Jan 2014
18 *
19 * @version $Author:: $: Author of last commit
20 * @version $Rev:: $: Revision of last commit
21 * @version $Date:: $: Date of last commit
22 *
23 */
24 public class TransactionDataType extends AbstractDataType<TransactionDataType> {
25
26 /***
27 *
28 * This holds the output fields
29 */
30 public enum Fields {
31 ID, DATE, AMOUNT, DESCRIPTION, CHECK_NUM, MEMO, PAYEE, ACCOUNT_ID, CATEGORY_ID, JIVE, TRANSACTION_TYPE, SPECIALSTATUS, PARENT, RELATED_TRANSFER, INITIAL_BALANCE;
32 }
33
34 @Override
35 protected Enum<?>[] getFields() {
36 return Fields.values();
37 }
38
39 public static Enum<?>[] getFileFields() {
40 Fields[] values = Fields.values();
41 List<Fields> asList = new LinkedList<>(Arrays.asList(values));
42 asList.remove(Fields.TRANSACTION_TYPE.ordinal());
43 return asList.toArray(new Fields[asList.size()]);
44 }
45
46 public enum NonoutputFields {
47 FROM_ACCOUNT_ID, TO_ACCOUNT_ID;
48 }
49
50 @Override
51 public String[] getValues() throws ClearcheckbookException {
52 Enum<?>[] fields = getFileFields();
53 String[] values = new String[fields.length];
54 for (int i = 0; i < values.length; i++) {
55 values[i] = getNonNullValue(fields[i]);
56 }
57 return values;
58 }
59
60 public enum Type {
61 WITHDRAWAL, DEPOSIT;
62
63 public String toString() {
64 return String.valueOf(ordinal());
65 };
66
67 public static boolean isMember(String key) {
68 Type[] values = values();
69 for (int i = 0; i < values.length; i++) {
70 if (values[i].name().equalsIgnoreCase(key)) {
71 return true;
72 }
73 }
74 return false;
75 }
76
77 public static Type fromString(String ordinal)
78 throws ClearcheckbookException {
79 try {
80 int i = Integer.valueOf(ordinal);
81 return Type.values()[i];
82 } catch (Exception e) {
83 throw new ClearcheckbookException(ordinal
84 + " is not a valid Type", e);
85 }
86 }
87
88 public static Type fromOrdinal(int ordinal)
89 throws ClearcheckbookException {
90 try {
91 return Type.values()[ordinal];
92 } catch (Exception e) {
93 throw new ClearcheckbookException(ordinal
94 + " is not a valid Type", e);
95 }
96 }
97 }
98
99 private static final Logger _logger = Logger
100 .getLogger(TransactionDataType.class);
101
102 /***
103 * Version with id field.
104 *
105 * @param id
106 * @param date
107 * @param amount
108 * @param transactionType
109 * @param accountId
110 * @param categoryId
111 * @param description
112 * @param jive
113 * @param fromAccountId
114 * @param toAccountId
115 * @param checkNum
116 * @param memo
117 * @param payee
118 * @return
119 */
120 public static TransactionDataType create(Long id, String date,
121 double amount, long accountId, long categoryId, String description,
122 boolean jive, long fromAccountId, long toAccountId,
123 String checkNum, String memo, String payee) {
124 TransactionDataType create = TransactionDataType.create(date, amount,
125 accountId, categoryId, description, jive, fromAccountId,
126 toAccountId, checkNum, memo, payee);
127 create.setValue(Fields.ID, id);
128 return create;
129 }
130
131 public static TransactionDataType create(String date, Double amount,
132 Long accountId, Long categoryId, String description, Boolean jive,
133 Long fromAccountId, Long toAccountId, String checkNum, String memo,
134 String payee) {
135 TransactionDataType transactionDataType = new TransactionDataType();
136 transactionDataType.setDate(date);
137 transactionDataType.setAmount(amount);
138 transactionDataType.setTransactionType(getTransactionType(amount));
139 transactionDataType.setAccountId(accountId);
140 transactionDataType.setCategoryId(categoryId);
141 transactionDataType.setDescription(description);
142 transactionDataType.setJive(jive);
143 transactionDataType.setFromAccountId(fromAccountId);
144 transactionDataType.setToAccountId(toAccountId);
145 transactionDataType.setCheckNum(checkNum);
146 transactionDataType.setMemo(memo);
147 transactionDataType.setPayee(payee);
148
149 _logger.debug("createCategoriesDataType: " + transactionDataType);
150 return transactionDataType;
151 }
152
153 /***
154 * if amount is negative is WITHDRAWAL else DEPOSIT
155 *
156 * @param amount
157 * @return
158 */
159 private static Type getTransactionType(double amount) {
160 if (amount < 0) {
161 return Type.WITHDRAWAL;
162 }
163 return Type.DEPOSIT;
164 }
165
166 private TransactionDataType() {
167 super();
168 }
169
170 public TransactionDataType(TransactionDataType original){
171 super(original);
172 }
173 public TransactionDataType(Map<String, String> map)
174 throws ClearcheckbookException {
175 super(map);
176 if (null == getJive()) {
177 setJive(false);
178 }
179 setTransactionType(getTransactionType(getAmount()));
180 }
181
182 public Long getAccountId() {
183 return getLongValue(Fields.ACCOUNT_ID);
184 }
185
186 public Double getAmount() {
187 return getDoubleValue(Fields.AMOUNT);
188 }
189
190 public Long getCategoryId() {
191 return getLongValue(Fields.CATEGORY_ID);
192 }
193
194 public String getCheckNum() {
195 return getValue(Fields.CHECK_NUM);
196 }
197
198 public String getDate() {
199 return getValue(Fields.DATE);
200 }
201
202 public String getDescription() {
203 return getValue(Fields.DESCRIPTION);
204 }
205
206 @Override
207 protected Enum<?>[] getEditFields() {
208 Fields[] insertFields = new Fields[] { Fields.ID, Fields.DATE,
209 Fields.AMOUNT, Fields.TRANSACTION_TYPE, Fields.ACCOUNT_ID,
210 Fields.CATEGORY_ID, Fields.DESCRIPTION, Fields.JIVE,
211 Fields.CHECK_NUM, Fields.MEMO };
212 return insertFields;
213 }
214
215 public Long getFromAccountId() {
216 return getLongValue(NonoutputFields.FROM_ACCOUNT_ID);
217 }
218
219 protected Enum<?>[] getInsertFields() {
220 Enum<?>[] insertFields = new Enum<?>[] { Fields.DATE, Fields.AMOUNT,
221 Fields.TRANSACTION_TYPE, Fields.ACCOUNT_ID, Fields.CATEGORY_ID,
222 Fields.DESCRIPTION, Fields.JIVE,
223 NonoutputFields.FROM_ACCOUNT_ID, NonoutputFields.TO_ACCOUNT_ID,
224 Fields.CHECK_NUM, Fields.MEMO, Fields.PAYEE };
225 return insertFields;
226 }
227
228 public Boolean getJive() {
229 return getBooleanValue(Fields.JIVE);
230 }
231
232 public String getMemo() {
233 return getValue(Fields.MEMO);
234 }
235
236 public String getPayee() {
237 return getValue(Fields.PAYEE);
238 }
239
240 public Long getToAccount() {
241 return getLongValue(NonoutputFields.TO_ACCOUNT_ID);
242 }
243
244 public Type getTransactionType() throws ClearcheckbookException {
245 return Type.fromString(getValue(Fields.TRANSACTION_TYPE));
246 }
247
248 public void setAccountId(Long string) {
249 setValue(Fields.ACCOUNT_ID, string);
250 }
251
252 public void setAmount(Double amount) {
253 setValue(Fields.AMOUNT, amount);
254 }
255
256 public void setCategoryId(Long string) {
257 setValue(Fields.CATEGORY_ID, string);
258 }
259
260 public void setCheckNum(String string) {
261 setValue(Fields.CHECK_NUM, string);
262 }
263
264 public void setDate(String string) {
265 setValue(Fields.DATE, string);
266 }
267
268 public void setDescription(String string) {
269 setValue(Fields.DESCRIPTION, string);
270 }
271
272 public void setFromAccountId(Long fromAccountId) {
273 setValue(NonoutputFields.FROM_ACCOUNT_ID, fromAccountId);
274 }
275
276 public void setJive(Boolean jive) {
277 setValue(Fields.JIVE, jive);
278 }
279
280 public void setMemo(String string) {
281 setValue(Fields.MEMO, string);
282 }
283
284 public void setPayee(String valString) {
285 setValue(Fields.PAYEE, valString);
286 }
287
288 public void setToAccountId(Long toAccountId) {
289 setValue(NonoutputFields.TO_ACCOUNT_ID, toAccountId);
290 }
291
292 public void setTransactionType(Type transactionType) {
293 setValue(Fields.TRANSACTION_TYPE, transactionType);
294 }
295
296 public void markToBeDeleted() {
297 setValue(Fields.ID, -1 * getId());
298 }
299
300 }