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.clearcheckbook.dto.AccountDataType.Fields; 9 import com.leonarduk.clearcheckbook.dto.AccountDataType.Type; 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 LimitDataType extends AbstractDataType<LimitDataType> { 24 25 private static final Logger _logger = Logger.getLogger(LimitDataType.class); 26 27 public enum Fields { 28 ID, NAME, AMOUNT, ACCOUNT_ID, CATEGORY_ID, SPENT, ROLLOVER, RESET_DAY, TRANSFER, DEPOSIT, DURATION, START_DATE, THIS_START_DATE, THIS_END_DATE, ORIGINAL_LIMIT, LIMIT_AMOUNT 29 } 30 31 @Override 32 protected Enum<?>[] getFields() { 33 return Fields.values(); 34 } 35 36 public enum Duration { 37 WEEKLY, BI_WEEKLY, MONTHLY, QUARTERLY, SEMI_ANNUALLY, ANNUALLY; 38 39 public String toString() { 40 return String.valueOf(ordinal()); 41 }; 42 43 public static Duration fromString(String ordinal) 44 throws ClearcheckbookException { 45 try { 46 int i = Integer.valueOf(ordinal); 47 return Duration.values()[i]; 48 } catch (Exception e) { 49 throw new ClearcheckbookException(ordinal 50 + " is not a valid Type", e); 51 } 52 } 53 54 public static Type fromOrdinal(int ordinal) 55 throws ClearcheckbookException { 56 try { 57 return Type.values()[ordinal]; 58 } catch (Exception e) { 59 throw new ClearcheckbookException(ordinal 60 + " is not a valid Type", e); 61 } 62 } 63 } 64 65 public LimitDataType(Map<String, String> map) { 66 super(map); 67 setStart_date(getStart_date()); 68 } 69 70 @Override 71 protected Enum<?>[] getEditFields() { 72 return new Fields[] { Fields.ID, Fields.AMOUNT, Fields.DURATION, 73 Fields.RESET_DAY, Fields.START_DATE, Fields.ROLLOVER, 74 Fields.TRANSFER, Fields.DEPOSIT }; 75 } 76 77 @Override 78 protected Enum<?>[] getInsertFields() { 79 return new Fields[] { Fields.ACCOUNT_ID, Fields.CATEGORY_ID, 80 Fields.AMOUNT, Fields.DURATION, Fields.RESET_DAY, 81 Fields.START_DATE, Fields.ROLLOVER, Fields.TRANSFER, 82 Fields.DEPOSIT }; 83 } 84 85 private LimitDataType() { 86 super(); 87 } 88 89 public static LimitDataType create(long accountId, long categoryId, 90 int amount, Duration duration, int reset_day, String start_date, 91 boolean rollover, boolean transfer, boolean deposit) { 92 LimitDataType limitDataType = new LimitDataType(); 93 limitDataType.setAccountId(accountId); 94 limitDataType.setCategoryId(categoryId); 95 limitDataType.setAmount(amount); 96 limitDataType.setDuration(duration); 97 limitDataType.setReset_day(reset_day); 98 limitDataType.setStart_date(start_date); 99 limitDataType.setRollover(rollover); 100 limitDataType.setTransfer(transfer); 101 limitDataType.setDeposit(deposit); 102 103 _logger.debug("create: " + limitDataType); 104 return limitDataType; 105 } 106 107 public long getAccountId() { 108 return getLongValue(Fields.ACCOUNT_ID); 109 } 110 111 public void setAccountId(long accountId) { 112 setValue(Fields.ACCOUNT_ID, accountId); 113 } 114 115 public long getCategoryId() { 116 return getLongValue(Fields.CATEGORY_ID); 117 } 118 119 public void setCategoryId(long categoryId) { 120 setValue(Fields.CATEGORY_ID, categoryId); 121 } 122 123 public Integer getAmount() { 124 Integer limit = getIntegerValue(Fields.AMOUNT); 125 if (null == limit) { 126 limit = getIntegerValue(Fields.LIMIT_AMOUNT); 127 } 128 return limit; 129 } 130 131 public void setAmount(int amount) { 132 setValue(Fields.AMOUNT, amount); 133 } 134 135 public Duration getDuration() throws ClearcheckbookException { 136 return Duration.fromString(getValue(Fields.DURATION)); 137 } 138 139 public String getDurationName() { 140 return Duration.valueOf(getValue(Fields.DURATION)).name(); 141 } 142 143 public void setDuration(Duration duration) { 144 setValue(Fields.DURATION, String.valueOf(duration.ordinal())); 145 } 146 147 public int getReset_day() { 148 return getIntegerValue(Fields.RESET_DAY); 149 } 150 151 public void setReset_day(int reset_day) { 152 setValue(Fields.RESET_DAY, reset_day); 153 } 154 155 public String getStart_date() { 156 String startDate = getValue(Fields.START_DATE); 157 if (null == startDate) { 158 startDate = getValue(Fields.THIS_START_DATE); 159 } 160 if (null != startDate) { 161 return startDate.substring(0, 10); 162 } 163 return null; 164 } 165 166 public String getEnd_date() { 167 String startDate = getValue(Fields.THIS_END_DATE); 168 return startDate; 169 } 170 171 public void setStart_date(String start_date) { 172 setValue(Fields.START_DATE, start_date); 173 } 174 175 public Boolean getRollover() { 176 return getBooleanValue(Fields.ROLLOVER); 177 } 178 179 public void setRollover(boolean rollover) { 180 setValue(Fields.ROLLOVER, rollover); 181 } 182 183 public Boolean getTransfer() { 184 return getBooleanValue(Fields.TRANSFER); 185 } 186 187 public void setTransfer(boolean transfer) { 188 setValue(Fields.TRANSFER, transfer); 189 } 190 191 public Boolean getDeposit() { 192 return getBooleanValue(Fields.DEPOSIT); 193 } 194 195 public void setDeposit(boolean deposit) { 196 setValue(Fields.DEPOSIT, deposit); 197 } 198 }