1 package com.leonarduk.clearcheckbook.dto;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Map;
6
7 import org.apache.log4j.Logger;
8
9 import com.leonarduk.clearcheckbook.ClearcheckbookException;
10
11 /***
12 *
13 *
14 *
15 * @author Stephen Leonard
16 * @since 28 Jan 2014
17 *
18 * @version $Author:: $: Author of last commit
19 * @version $Rev:: $: Revision of last commit
20 * @version $Date:: $: Date of last commit
21 *
22 */
23 public class ReminderDataType extends AbstractDataType<ReminderDataType> {
24
25 public enum Fields {
26 ID, TITLE, START_DATE, END_DATE, NOTIFY, NOTIFY_TIME, REPEAT, REPEAT_EVERY_NUM, REPEAT_EVERY, FLOATS, FLOATS_EVERY_NUM, FLOATS_EVERY, TRANS_AMOUNT, TRANS_DESCRIPTION, TRANS_TRANSACTION_TYPE, TRANS_ACCOUNT_ID, TRANS_CATEGORY_ID, TRANS_TRANSFERTOACCOUNT, TRANS_CHECK_NUM, TRANS_MEMO, TRANS_PAYEE, START_YEAR, OCCUR_FLOATING, EMAIL, EMAILDAYS, START_MONTH, START_DAY, END_YEAR, END_MONTH, END_DAY, OCCUR_ONCE, OCCUR_REPEATING, TRANS_ACCOUNTFROM, TRANS_ACCOUNTTO, REMINDER_ID, TRANSACTION;
27 }
28
29 @Override
30 protected Enum<?>[] getFields() {
31 return Fields.values();
32 }
33
34 public ReminderDataType(Map<String, String> map) {
35 super(map);
36 setValue(Fields.ID, getId());
37 setEnd_Date(getEnd_date());
38 setStart_day(getStart_day());
39 setStart_month(getStart_month());
40 setStart_year(getStart_year());
41 }
42
43 private ReminderDataType() {
44 super();
45 }
46
47 private static final Logger _logger = Logger
48 .getLogger(ReminderDataType.class);
49
50 @Override
51 protected Enum<?>[] getInsertFields() {
52 return new Fields[] { Fields.ID, Fields.TITLE, Fields.START_DATE,
53 Fields.END_DATE, Fields.NOTIFY, Fields.NOTIFY_TIME,
54 Fields.REPEAT, Fields.REPEAT_EVERY_NUM, Fields.REPEAT_EVERY,
55 Fields.FLOATS, Fields.FLOATS_EVERY_NUM, Fields.FLOATS_EVERY,
56 Fields.TRANS_AMOUNT, Fields.TRANS_DESCRIPTION,
57 Fields.TRANS_TRANSACTION_TYPE, Fields.TRANS_ACCOUNT_ID,
58 Fields.TRANS_CATEGORY_ID, Fields.TRANS_TRANSFERTOACCOUNT,
59 Fields.TRANS_CHECK_NUM, Fields.TRANS_MEMO, Fields.TRANS_PAYEE };
60 }
61
62 public static ReminderDataType create(String title, Boolean email,
63 Integer emailDays, Integer start_year, String start_month,
64 String start_day, Integer end_year, Integer end_month,
65 Integer end_day, Boolean occur_once, Boolean occur_repeating,
66 Boolean occur_floating, Integer repeat_every,
67 Integer repeat_evey_num, Integer float_every_num,
68 Integer float_every, TransactionDataType transactionDataType)
69 throws ClearcheckbookException {
70 ReminderDataType reminderDataType = new ReminderDataType();
71 reminderDataType.setTitle(title);
72 reminderDataType.setEmail(email);
73 reminderDataType.setEmailDays(emailDays);
74 reminderDataType.setStart_date(start_year + "-" + start_month + "-"
75 + start_day);
76 reminderDataType
77 .setEnd_Date(end_year + "-" + end_month + "-" + end_day);
78 reminderDataType.setOccur_once(occur_once);
79 reminderDataType.setOccur_repeating(occur_repeating);
80 reminderDataType.setOccur_floating(occur_floating);
81 reminderDataType.setFloat_every(float_every);
82 reminderDataType.setFloat_every_num(float_every_num);
83 reminderDataType.setTransactionDataType(transactionDataType);
84 _logger.debug("createUserDataType: " + reminderDataType);
85
86 return reminderDataType;
87 }
88
89 private void setStart_date(String string) {
90 setValue(Fields.START_DATE, string);
91 }
92
93 /***
94 * since the getall method uses reminder_id not id, need to fix the data
95 */
96 @Override
97 public ParsedNameValuePair getIdParameter() {
98 String id = getValue(Fields.ID);
99 if (id == null) {
100 id = getValue(Fields.REMINDER_ID);
101 }
102 return new ParsedNameValuePair(Fields.ID.name().toLowerCase(), id);
103 }
104
105 public String getTitle() {
106 return getValue(Fields.TITLE);
107 }
108
109 public void setTitle(String title) {
110 setValue(Fields.TITLE, title);
111 }
112
113 public String getEnd_date() {
114 String nonNullValue = getNonNullValue(Fields.END_DATE);
115 if (nonNullValue.length() > 10) {
116 return nonNullValue.substring(0, 10);
117 }
118 return nonNullValue;
119 }
120
121 public void setEnd_Date(String date) {
122 setValue(Fields.END_DATE, date);
123 }
124
125 public Boolean getEmail() {
126 return getBooleanValue(Fields.EMAIL);
127 }
128
129 public void setEmail(Boolean email) {
130 setValue(Fields.EMAIL, email);
131 }
132
133 public Integer getEmailDays() {
134 return getIntegerValue(Fields.EMAILDAYS);
135
136 }
137
138 public void setEmailDays(Integer emailDays) {
139 setValue(Fields.EMAILDAYS, emailDays);
140 }
141
142 public String getStart_date() {
143 return getValue(Fields.START_DATE);
144 }
145
146 public Integer getStart_year() {
147 String date = getStart_date();
148 if (null == date) {
149 return null;
150 }
151 return Integer.valueOf(date.substring(0, 4));
152 }
153
154 public void setStart_year(Integer start_year) {
155 setValue(Fields.START_YEAR, start_year);
156 }
157
158 public String getStart_month() {
159 String date = getStart_date();
160 if (null == date) {
161 return null;
162 }
163 return date.substring(5, 7);
164 }
165
166 public void setStart_month(String string) {
167 setValue(Fields.START_MONTH, string);
168 }
169
170 public String getStart_day() {
171 String date = getStart_date();
172 if (null == date) {
173 return null;
174 }
175 return date.substring(8, 10);
176 }
177
178 public void setStart_day(String string) {
179 setValue(Fields.START_DAY, string);
180 }
181
182 public Integer getEnd_year() {
183 return getIntegerValue(Fields.END_YEAR);
184 }
185
186 public void setEnd_year(Integer end_year) {
187 setValue(Fields.END_YEAR, end_year);
188 }
189
190 public Integer getEnd_month() {
191 return getIntegerValue(Fields.END_MONTH);
192 }
193
194 public void setEnd_month(Integer end_month) {
195 setValue(Fields.END_MONTH, end_month);
196 }
197
198 public Integer getEnd_day() {
199 return getIntegerValue(Fields.END_DAY);
200 }
201
202 public void setEnd_day(Integer end_day) {
203 setValue(Fields.END_DAY, end_day);
204 }
205
206 public Boolean isOccur_once() {
207 return getBooleanValue(Fields.OCCUR_ONCE);
208 }
209
210 public void setOccur_once(Boolean occur_once) {
211 setValue(Fields.OCCUR_ONCE, occur_once);
212 }
213
214 public Boolean isOccur_repeating() {
215 return getBooleanValue(Fields.OCCUR_REPEATING);
216 }
217
218 public void setOccur_repeating(Boolean occur_repeating) {
219 setValue(Fields.OCCUR_REPEATING, occur_repeating);
220 }
221
222 public Boolean isOccur_floating() {
223 return getBooleanValue(Fields.OCCUR_FLOATING);
224 }
225
226 public void setOccur_floating(Boolean occur_floating) {
227 setValue(Fields.OCCUR_FLOATING, occur_floating);
228 }
229
230 public Integer getRepeat_every() {
231 return getIntegerValue(Fields.REPEAT_EVERY);
232 }
233
234 public void setRepeat_every(Integer repeat_every) {
235 setValue(Fields.REPEAT_EVERY, repeat_every);
236 }
237
238 public Integer getRepeat_evey_num() {
239 return getIntegerValue(Fields.REPEAT_EVERY_NUM);
240 }
241
242 public void setRepeat_evey_num(Integer repeat_evey_num) {
243 setValue(Fields.REPEAT_EVERY_NUM, repeat_evey_num);
244 }
245
246 public Integer getFloat_every_num() {
247 return getIntegerValue(Fields.FLOATS_EVERY_NUM);
248 }
249
250 public void setFloat_every_num(Integer float_every_num) {
251 setValue(Fields.FLOATS_EVERY_NUM, float_every_num);
252 }
253
254 public Integer getFloat_every() {
255 return getIntegerValue(Fields.FLOATS_EVERY);
256 }
257
258 public void setFloat_every(Integer float_every) {
259 setValue(Fields.FLOATS_EVERY, float_every);
260 }
261
262 /***
263 * Converts all the "trans_" fields to a TransactionDataType object
264 *
265 * @return
266 * @throws ClearcheckbookException
267 */
268 public TransactionDataType getTransactionDataType()
269 throws ClearcheckbookException {
270 Boolean jive = false;
271 String date = null;
272 TransactionDataType transactionDataType = TransactionDataType.create(
273 date, getDoubleValue(Fields.TRANS_AMOUNT),
274 getLongValue(Fields.TRANS_ACCOUNT_ID),
275 getLongValue(Fields.TRANS_CATEGORY_ID),
276 getValue(Fields.TRANS_DESCRIPTION), jive,
277 getLongValue(Fields.TRANS_ACCOUNTFROM),
278 getLongValue(Fields.TRANS_ACCOUNTTO),
279 getValue(Fields.TRANS_CHECK_NUM), getValue(Fields.TRANS_MEMO),
280 getValue(Fields.TRANS_PAYEE));
281
282 return transactionDataType;
283 }
284
285 /***
286 * Converts the {@link TransactionDataType} object into the fields expected,
287 * ie the ones starting "trans_".
288 *
289 * @param transactionDataType
290 * @throws ClearcheckbookException
291 */
292 public void setTransactionDataType(TransactionDataType transactionDataType)
293 throws ClearcheckbookException {
294 setValue(Fields.TRANS_AMOUNT, transactionDataType.getAmount());
295 setValue(Fields.TRANS_TRANSACTION_TYPE,
296 transactionDataType.getTransactionType());
297 setValue(Fields.TRANS_ACCOUNT_ID, transactionDataType.getAccountId());
298 setValue(Fields.TRANS_CATEGORY_ID, transactionDataType.getCategoryId());
299 setValue(Fields.TRANS_DESCRIPTION, transactionDataType.getDescription());
300 setValue(Fields.TRANS_ACCOUNTFROM,
301 transactionDataType.getFromAccountId());
302 setValue(Fields.TRANS_ACCOUNTTO, transactionDataType.getToAccount());
303 setValue(Fields.TRANS_CHECK_NUM, transactionDataType.getCheckNum());
304 setValue(Fields.TRANS_MEMO, transactionDataType.getMemo());
305 setValue(Fields.TRANS_PAYEE, transactionDataType.getPayee());
306 }
307
308 public Boolean getOccur_once() {
309 return getBooleanValue(Fields.OCCUR_ONCE);
310 }
311
312 public Boolean getOccur_repeating() {
313 return getBooleanValue(Fields.OCCUR_REPEATING);
314 }
315
316 public Boolean getOccur_floating() {
317 return getBooleanValue(Fields.OCCUR_FLOATING);
318 }
319
320 @Override
321 protected Enum<?>[] getEditFields() {
322 return new Enum[] { Fields.ID, Fields.TITLE, Fields.EMAIL,
323 Fields.EMAILDAYS, Fields.START_YEAR, Fields.START_MONTH,
324 Fields.START_DAY, Fields.END_YEAR, Fields.END_MONTH,
325 Fields.END_DATE, Fields.OCCUR_ONCE, Fields.OCCUR_REPEATING,
326 Fields.OCCUR_FLOATING, Fields.REPEAT_EVERY,
327 Fields.REPEAT_EVERY_NUM, Fields.FLOATS_EVERY,
328 Fields.FLOATS_EVERY_NUM, Fields.TRANSACTION,
329 Fields.TRANS_AMOUNT, Fields.TRANS_DESCRIPTION,
330 Fields.TRANS_PAYEE, Fields.TRANS_MEMO, Fields.TRANS_CHECK_NUM,
331 Fields.TRANS_ACCOUNT_ID, Fields.TRANS_CATEGORY_ID,
332 Fields.TRANS_TRANSACTION_TYPE, Fields.TRANS_ACCOUNTFROM,
333 Fields.TRANS_ACCOUNTTO };
334 }
335
336 /***
337 * There are some issues with Reminder data: reminder_id is used instead of
338 * id in get all. Email not given by get or getall
339 */
340 @SuppressWarnings("rawtypes")
341 @Override
342 protected ArrayList<Enum> getFieldsToIgnoreInEqualsMethod() {
343 return new ArrayList<Enum>(Arrays.asList(new Enum[] { Fields.EMAIL,
344 Fields.REMINDER_ID }));
345 }
346 }