|
| 1 | +package org.motechproject.nms.kilkari.dto; |
| 2 | + |
| 3 | +import org.joda.time.DateTime; |
| 4 | +import org.joda.time.Days; |
| 5 | +import org.motechproject.nms.kilkari.domain.SubscriptionPackMessage; |
| 6 | + |
| 7 | +import java.util.Date; |
| 8 | + |
| 9 | +public class SubscriptionDto { |
| 10 | + private final long id; |
| 11 | + private final Date activationDate; |
| 12 | + private final String deactivationReason; |
| 13 | + private final Date endDate; |
| 14 | + private final String firstMessageDayOfWeek; |
| 15 | + private final boolean needsWelcomeMessageViaObd; |
| 16 | + private final String origin; |
| 17 | + private final String secondMessageDayOfWeek; |
| 18 | + private final Date startDate; |
| 19 | + private final String status; |
| 20 | + private final long subscriberIdOid; |
| 21 | + private final String subscriptionId; |
| 22 | + private final long subscriptionPackIdOid; |
| 23 | + private final Date creationDate; |
| 24 | + private final String creator; |
| 25 | + private final Date modificationDate; |
| 26 | + private final String modifiedBy; |
| 27 | + private final String owner; |
| 28 | + private final boolean needsWelcomeOptInForWp; |
| 29 | + private final String callingNumber; |
| 30 | + private final String languageCode; |
| 31 | + private final String circleName; |
| 32 | + private final long stateId; |
| 33 | + private final int weeks; |
| 34 | + private final String subscriptionPackName; |
| 35 | + private final int messagesPerWeek; |
| 36 | + |
| 37 | + private SubscriptionDto(Builder builder) { |
| 38 | + this.id = builder.id; |
| 39 | + this.activationDate = builder.activationDate; |
| 40 | + this.deactivationReason = builder.deactivationReason; |
| 41 | + this.endDate = builder.endDate; |
| 42 | + this.firstMessageDayOfWeek = builder.firstMessageDayOfWeek; |
| 43 | + this.needsWelcomeMessageViaObd = builder.needsWelcomeMessageViaObd; |
| 44 | + this.origin = builder.origin; |
| 45 | + this.secondMessageDayOfWeek = builder.secondMessageDayOfWeek; |
| 46 | + this.startDate = builder.startDate; |
| 47 | + this.status = builder.status; |
| 48 | + this.subscriberIdOid = builder.subscriberIdOid; |
| 49 | + this.subscriptionId = builder.subscriptionId; |
| 50 | + this.subscriptionPackIdOid = builder.subscriptionPackIdOid; |
| 51 | + this.creationDate = builder.creationDate; |
| 52 | + this.creator = builder.creator; |
| 53 | + this.modificationDate = builder.modificationDate; |
| 54 | + this.modifiedBy = builder.modifiedBy; |
| 55 | + this.owner = builder.owner; |
| 56 | + this.needsWelcomeOptInForWp = builder.needsWelcomeOptInForWp; |
| 57 | + this.callingNumber = builder.callingNumber; |
| 58 | + this.languageCode = builder.languageCode; |
| 59 | + this.circleName = builder.circleName; |
| 60 | + this.stateId = builder.stateId; |
| 61 | + this.weeks = builder.weeks; |
| 62 | + this.subscriptionPackName = builder.subscriptionPackName; |
| 63 | + this.messagesPerWeek = builder.messagesPerWeek; |
| 64 | + } |
| 65 | + |
| 66 | + public long getId() { |
| 67 | + return id; |
| 68 | + } |
| 69 | + |
| 70 | + public Date getActivationDate() { |
| 71 | + return activationDate; |
| 72 | + } |
| 73 | + |
| 74 | + public String getDeactivationReason() { |
| 75 | + return deactivationReason; |
| 76 | + } |
| 77 | + |
| 78 | + public Date getEndDate() { |
| 79 | + return endDate; |
| 80 | + } |
| 81 | + |
| 82 | + public String getFirstMessageDayOfWeek() { |
| 83 | + return firstMessageDayOfWeek; |
| 84 | + } |
| 85 | + |
| 86 | + public boolean isNeedsWelcomeMessageViaObd() { |
| 87 | + return needsWelcomeMessageViaObd; |
| 88 | + } |
| 89 | + |
| 90 | + public String getOrigin() { |
| 91 | + return origin; |
| 92 | + } |
| 93 | + |
| 94 | + public String getSecondMessageDayOfWeek() { |
| 95 | + return secondMessageDayOfWeek; |
| 96 | + } |
| 97 | + |
| 98 | + public Date getStartDate() { |
| 99 | + return startDate; |
| 100 | + } |
| 101 | + |
| 102 | + public String getStatus() { |
| 103 | + return status; |
| 104 | + } |
| 105 | + |
| 106 | + public long getSubscriberIdOid() { |
| 107 | + return subscriberIdOid; |
| 108 | + } |
| 109 | + |
| 110 | + public String getSubscriptionId() { |
| 111 | + return subscriptionId; |
| 112 | + } |
| 113 | + |
| 114 | + public long getSubscriptionPackIdOid() { |
| 115 | + return subscriptionPackIdOid; |
| 116 | + } |
| 117 | + |
| 118 | + public Date getCreationDate() { |
| 119 | + return creationDate; |
| 120 | + } |
| 121 | + |
| 122 | + public String getCreator() { |
| 123 | + return creator; |
| 124 | + } |
| 125 | + |
| 126 | + public Date getModificationDate() { |
| 127 | + return modificationDate; |
| 128 | + } |
| 129 | + |
| 130 | + public String getModifiedBy() { |
| 131 | + return modifiedBy; |
| 132 | + } |
| 133 | + |
| 134 | + public String getOwner() { |
| 135 | + return owner; |
| 136 | + } |
| 137 | + |
| 138 | + public boolean isNeedsWelcomeOptInForWp() { |
| 139 | + return needsWelcomeOptInForWp; |
| 140 | + } |
| 141 | + |
| 142 | + public String getCallingNumber() { |
| 143 | + return callingNumber; |
| 144 | + } |
| 145 | + |
| 146 | + public String getLanguageCode() { |
| 147 | + return languageCode; |
| 148 | + } |
| 149 | + |
| 150 | + public String getCircleName() { |
| 151 | + return circleName; |
| 152 | + } |
| 153 | + |
| 154 | + public long getStateId() { |
| 155 | + return stateId; |
| 156 | + } |
| 157 | + |
| 158 | + public int getWeeks() { |
| 159 | + return weeks; |
| 160 | + } |
| 161 | + |
| 162 | + public String getSubscriptionPackName() { |
| 163 | + return subscriptionPackName; |
| 164 | + } |
| 165 | + |
| 166 | + public int getMessagesPerWeek() { |
| 167 | + return messagesPerWeek; |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | + public SubscriptionPackMessage nextScheduledMessage(DateTime date) { |
| 172 | + |
| 173 | + int daysIntoPack = Days.daysBetween(new DateTime(startDate), date).getDays(); |
| 174 | + if (daysIntoPack < 0) { |
| 175 | + throw new IllegalStateException( |
| 176 | + String.format("Subscription with ID %s is not due for any scheduled message. Start date in the future", subscriptionId)); |
| 177 | + } |
| 178 | + |
| 179 | + if (needsWelcomeMessageViaObd) { |
| 180 | + return getWelcomeMessage(); |
| 181 | + } |
| 182 | + |
| 183 | + int currentWeek = daysIntoPack / 7 + 1; |
| 184 | + |
| 185 | + return getMessageByWeekAndMessageId(currentWeek, 1); |
| 186 | + |
| 187 | + } |
| 188 | + |
| 189 | + public SubscriptionPackMessage getMessageByWeekAndMessageId(int week, int day) { |
| 190 | + String weekId = String.format("w%d_%d", week, day); |
| 191 | + String messageFileName = String.format("w%d_%d.wav", week, day); |
| 192 | + |
| 193 | + return new SubscriptionPackMessage(weekId, messageFileName, 120); // Using default 120 seconds duration |
| 194 | + } |
| 195 | + |
| 196 | + private SubscriptionPackMessage getWelcomeMessage() { |
| 197 | + return new SubscriptionPackMessage("w1_1", "w1_1.wav", 120); |
| 198 | + } |
| 199 | + |
| 200 | + public static class Builder { |
| 201 | + private long id; |
| 202 | + private Date activationDate; |
| 203 | + private String deactivationReason; |
| 204 | + private Date endDate; |
| 205 | + private String firstMessageDayOfWeek; |
| 206 | + private boolean needsWelcomeMessageViaObd; |
| 207 | + private String origin; |
| 208 | + private String secondMessageDayOfWeek; |
| 209 | + private Date startDate; |
| 210 | + private String status; |
| 211 | + private long subscriberIdOid; |
| 212 | + private String subscriptionId; |
| 213 | + private long subscriptionPackIdOid; |
| 214 | + private Date creationDate; |
| 215 | + private String creator; |
| 216 | + private Date modificationDate; |
| 217 | + private String modifiedBy; |
| 218 | + private String owner; |
| 219 | + private boolean needsWelcomeOptInForWp; |
| 220 | + private String callingNumber; |
| 221 | + private String languageCode; |
| 222 | + private String circleName; |
| 223 | + private long stateId; |
| 224 | + private int weeks; |
| 225 | + private String subscriptionPackName; |
| 226 | + private int messagesPerWeek; |
| 227 | + |
| 228 | + public Builder setId(long id) { this.id = id; return this; } |
| 229 | + public Builder setActivationDate(Date activationDate) { this.activationDate = activationDate; return this; } |
| 230 | + public Builder setDeactivationReason(String deactivationReason) { this.deactivationReason = deactivationReason; return this; } |
| 231 | + public Builder setEndDate(Date endDate) { this.endDate = endDate; return this; } |
| 232 | + public Builder setFirstMessageDayOfWeek(String firstMessageDayOfWeek) { this.firstMessageDayOfWeek = firstMessageDayOfWeek; return this; } |
| 233 | + public Builder setNeedsWelcomeMessageViaObd(boolean needsWelcomeMessageViaObd) { this.needsWelcomeMessageViaObd = needsWelcomeMessageViaObd; return this; } |
| 234 | + public Builder setOrigin(String origin) { this.origin = origin; return this; } |
| 235 | + public Builder setSecondMessageDayOfWeek(String secondMessageDayOfWeek) { this.secondMessageDayOfWeek = secondMessageDayOfWeek; return this; } |
| 236 | + public Builder setStartDate(Date startDate) { this.startDate = startDate; return this; } |
| 237 | + public Builder setStatus(String status) { this.status = status; return this; } |
| 238 | + public Builder setSubscriberIdOid(long subscriberIdOid) { this.subscriberIdOid = subscriberIdOid; return this; } |
| 239 | + public Builder setSubscriptionId(String subscriptionId) { this.subscriptionId = subscriptionId; return this; } |
| 240 | + public Builder setSubscriptionPackIdOid(long subscriptionPackIdOid) { this.subscriptionPackIdOid = subscriptionPackIdOid; return this; } |
| 241 | + public Builder setCreationDate(Date creationDate) { this.creationDate = creationDate; return this; } |
| 242 | + public Builder setCreator(String creator) { this.creator = creator; return this; } |
| 243 | + public Builder setModificationDate(Date modificationDate) { this.modificationDate = modificationDate; return this; } |
| 244 | + public Builder setModifiedBy(String modifiedBy) { this.modifiedBy = modifiedBy; return this; } |
| 245 | + public Builder setOwner(String owner) { this.owner = owner; return this; } |
| 246 | + public Builder setNeedsWelcomeOptInForWp(boolean needsWelcomeOptInForWp) { this.needsWelcomeOptInForWp = needsWelcomeOptInForWp; return this; } |
| 247 | + public Builder setCallingNumber(String callingNumber) { this.callingNumber = callingNumber; return this; } |
| 248 | + public Builder setLanguageCode(String languageCode) { this.languageCode = languageCode; return this; } |
| 249 | + public Builder setCircleName(String circleName) { this.circleName = circleName; return this; } |
| 250 | + public Builder setStateId(long stateId) { this.stateId = stateId; return this; } |
| 251 | + public Builder setWeeks(int weeks) { this.weeks = weeks; return this; } |
| 252 | + public Builder setSubscriptionPackName(String subscriptionPackName) { this.subscriptionPackName = subscriptionPackName; return this; } |
| 253 | + public Builder setMessagesPerWeek(int messagesPerWeek) { this.messagesPerWeek = messagesPerWeek; return this; } |
| 254 | + |
| 255 | + |
| 256 | + |
| 257 | + public SubscriptionDto build() { |
| 258 | + return new SubscriptionDto(this); |
| 259 | + } |
| 260 | + } |
| 261 | +} |
0 commit comments