1 package com.leonarduk.clearcheckbook.dto;
2
3 import java.util.Map;
4
5 import org.apache.log4j.Logger;
6
7 import com.leonarduk.clearcheckbook.ClearcheckbookException;
8 import com.leonarduk.utils.NumberUtils;
9
10 /***
11 *
12 *
13 *
14 * @author Stephen Leonard
15 * @since 28 Jan 2014
16 *
17 * @version $Author:: $: Author of last commit
18 * @version $Rev:: $: Revision of last commit
19 * @version $Date:: $: Date of last commit
20 *
21 */
22 public class AccountDataType extends AbstractDataType<AccountDataType> {
23
24 private static final Logger _logger = Logger
25 .getLogger(AccountDataType.class);
26
27 public enum Fields {
28 ID, NAME, TYPE_ID, DEPOSIT, JIVE_DEPOSIT, WITHDRAWAL, JIVE_WITHDRAWAL, INITIAL_BALANCE
29 }
30
31 public enum Type {
32 UNKNOWN, CASH, CHECKING, SAVINGS, CREDIT_CARD, INVESTMENT;
33
34 public String toString() {
35 return String.valueOf(ordinal());
36 };
37
38 public static Type fromString(String ordinal)
39 throws ClearcheckbookException {
40 try {
41 int i = Integer.valueOf(ordinal);
42 return Type.values()[i];
43 } catch (Exception e) {
44 throw new ClearcheckbookException(ordinal
45 + " is not a valid Type", e);
46 }
47 }
48
49 public static Type fromOrdinal(int ordinal)
50 throws ClearcheckbookException {
51 try {
52 return Type.values()[ordinal];
53 } catch (Exception e) {
54 throw new ClearcheckbookException(ordinal
55 + " is not a valid Type", e);
56 }
57 }
58 }
59
60 @Override
61 protected Enum<?>[] getEditFields() {
62 Fields[] insertFields = new Fields[] { Fields.ID, Fields.NAME,
63 Fields.TYPE_ID };
64 return insertFields;
65 }
66
67 @Override
68 protected Enum<?>[] getInsertFields() {
69 Fields[] insertFields = new Fields[] { Fields.NAME, Fields.TYPE_ID,
70 Fields.INITIAL_BALANCE };
71 return insertFields;
72 }
73
74 public AccountDataType(Map<String, String> map) {
75 super(map);
76 }
77
78 private AccountDataType() {
79 super();
80 }
81
82 /***
83 *
84 * @param name
85 * @param type_id
86 * @param initial_balance
87 * @return
88 */
89 public static AccountDataType create(String name, Type type_id,
90 double initial_balance) {
91 AccountDataType accountDataType = new AccountDataType();
92 accountDataType.setName(name);
93 accountDataType.setTypeId(type_id);
94 accountDataType.setInitialBalance(initial_balance);
95
96 _logger.debug("createUserDataType: " + name + " " + type_id + " "
97 + initial_balance + " -> " + accountDataType);
98
99 return accountDataType;
100 }
101
102 private void setInitialBalance(double initial_balance) {
103 setValue(Fields.INITIAL_BALANCE, initial_balance);
104 }
105
106 public String getName() {
107 return getValue(Fields.NAME);
108 }
109
110 public void setName(String name) {
111 setValue(Fields.NAME, name);
112 }
113
114 public Type getTypeId() throws ClearcheckbookException {
115 return Type.fromString(getValue(Fields.TYPE_ID));
116 }
117
118 public void setTypeId(Type type_id) {
119 setValue(Fields.TYPE_ID, type_id);
120 }
121
122 public Double getDeposit() {
123 return getDoubleValue(Fields.DEPOSIT);
124 }
125
126 public void setDeposit(double value) {
127 setValue(Fields.DEPOSIT, value);
128 }
129
130 public Double getWithdrawal() {
131 return getDoubleValue(Fields.WITHDRAWAL);
132 }
133
134 /***
135 * Extension of the API to take the deposit - withdrawal values to give
136 * balance.
137 *
138 * @return
139 */
140 public Double getCurrentBalance() {
141 return NumberUtils.formatMoney(getDeposit().doubleValue()
142 - getWithdrawal().doubleValue());
143 }
144
145 public void setWithdrawal(Double value) {
146 setValue(Fields.WITHDRAWAL, value);
147 }
148
149 public Double getJiveDeposit() {
150 return getDoubleValue(Fields.JIVE_DEPOSIT);
151 }
152
153 public Double getJiveWithdrawal() {
154 return getDoubleValue(Fields.JIVE_WITHDRAWAL);
155 }
156
157 public void setJiveDeposit(Double value) {
158 setValue(Fields.JIVE_DEPOSIT, value);
159 }
160
161 public void setJiveWithdrawal(Double value) {
162 setValue(Fields.JIVE_WITHDRAWAL, value);
163 }
164
165 @Override
166 protected Enum<?>[] getFields() {
167 return Fields.values();
168 }
169
170 }