From c25c88446039ae9c35eba131bec9079eb69b04b4 Mon Sep 17 00:00:00 2001 From: hrosa Date: Thu, 7 Jun 2018 16:19:01 +0100 Subject: [PATCH] BS-232 Added builder for SmsMessageFilter --- .../dao/entities/SmsMessageFilter.java | 123 +++++++++++++++--- 1 file changed, 103 insertions(+), 20 deletions(-) diff --git a/restcomm/restcomm.dao/src/main/java/org/restcomm/connect/dao/entities/SmsMessageFilter.java b/restcomm/restcomm.dao/src/main/java/org/restcomm/connect/dao/entities/SmsMessageFilter.java index 0df114c345..9f468afdd7 100644 --- a/restcomm/restcomm.dao/src/main/java/org/restcomm/connect/dao/entities/SmsMessageFilter.java +++ b/restcomm/restcomm.dao/src/main/java/org/restcomm/connect/dao/entities/SmsMessageFilter.java @@ -19,38 +19,45 @@ */ package org.restcomm.connect.dao.entities; +import org.restcomm.connect.commons.annotations.concurrency.Immutable; + +import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; -import org.restcomm.connect.commons.annotations.concurrency.Immutable; - /** * @author vunguyen + * @author Henrique Rosa (henrique.rosa@telestax.com) */ @Immutable public class SmsMessageFilter { - private final String accountSid; - private final List accountSidSet; // if not-null we need the cdrs that belong to several accounts - private final String recipient; - private final String sender; - private final Date startTime; // to initialize it pass string arguments with yyyy-MM-dd format - private final Date endTime; - private final String body; - private final Integer limit; - private final Integer offset; - private final String instanceid; + private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); + + private String accountSid; + private List accountSidSet; // if not-null we need the cdrs that belong to several accounts + private String recipient; + private String sender; + private Date startTime; // to initialize it pass string arguments with yyyy-MM-dd format + private Date endTime; + private String body; + private Integer limit; + private Integer offset; + private String instanceid; + + public SmsMessageFilter() { + } public SmsMessageFilter(String accountSid, List accountSidSet, String recipient, String sender, String startTime, String endTime, - String body, Integer limit, Integer offset) throws ParseException { - this(accountSid, accountSidSet, recipient,sender,startTime,endTime, body, limit,offset,null); + String body, Integer limit, Integer offset) throws ParseException { + this(accountSid, accountSidSet, recipient, sender, startTime, endTime, body, limit, offset, null); } public SmsMessageFilter(String accountSid, List accountSidSet, String recipient, String sender, String startTime, String endTime, - String body, Integer limit, Integer offset, String instanceId) throws ParseException { + String body, Integer limit, Integer offset, String instanceId) throws ParseException { this.accountSid = accountSid; this.accountSidSet = accountSidSet; @@ -67,15 +74,13 @@ public SmsMessageFilter(String accountSid, List accountSidSet, String re this.limit = limit; this.offset = offset; if (startTime != null) { - SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd"); - Date date = parser.parse(startTime); + Date date = DATE_FORMAT.parse(startTime); this.startTime = date; } else this.startTime = null; if (endTime != null) { - SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd"); - Date date = parser.parse(endTime); + Date date = DATE_FORMAT.parse(endTime); this.endTime = date; } else { this.endTime = null; @@ -123,5 +128,83 @@ public int getOffset() { return offset; } - public String getInstanceid() { return instanceid; } + public String getInstanceid() { + return instanceid; + } + + public Builder builer() { + return new Builder(); + } + + public class Builder { + + private final SmsMessageFilter filter; + + private Builder() { + this.filter = new SmsMessageFilter(); + } + + public Builder accountSid(String accountSid) { + this.filter.accountSid = accountSid; + return this; + } + + public Builder accountSidSet(List accountSidSet) { + this.filter.accountSidSet = accountSidSet; + return this; + } + + public Builder recipient(String recipient) { + if (recipient != null && !recipient.isEmpty()) { + this.filter.recipient = (recipient.startsWith("%") ? "" : "%") + this.filter.recipient; + } + return this; + } + + public Builder sender(String sender) { + if (sender != null && !sender.isEmpty()) { + this.filter.sender = (sender.startsWith("%") ? "" : "%") + this.filter.sender; + } + return this; + } + + public Builder startTime(String startTime) throws ParseException { + if (startTime != null && !startTime.isEmpty()) { + this.filter.startTime = DATE_FORMAT.parse(startTime); + } + return this; + } + + public Builder endTime(String endTime) throws ParseException { + if (endTime != null && !endTime.isEmpty()) { + this.filter.endTime = DATE_FORMAT.parse(endTime); + } + return this; + } + + public Builder body(String body) { + this.filter.body = body; + return this; + } + + public Builder limit(int limit) { + this.filter.limit = limit; + return this; + } + + public Builder offset(int offset) { + this.filter.offset = offset; + return this; + } + + public Builder instanceId(String instanceId) { + this.filter.instanceid = instanceid; + return this; + } + + public SmsMessageFilter build() { + return this.filter; + } + + } }