001package com.plivo.api.models.message;
002
003import com.fasterxml.jackson.annotation.JsonProperty;
004import com.fasterxml.jackson.databind.annotation.JsonSerialize;
005import com.plivo.api.models.base.Creator;
006import com.plivo.api.serializers.DelimitedListSerializer;
007import com.plivo.api.util.Utils;
008import java.net.URL;
009import java.util.List;
010import retrofit2.Call;
011
012/**
013 * Represents an instance of a message created on PlivoClient.
014 */
015public class MessageCreator extends Creator<MessageCreateResponse> {
016
017  @JsonProperty("src")
018  private String source;
019  @JsonSerialize(using = DelimitedListSerializer.class)
020  @JsonProperty("dst")
021  private final List<String> destination;
022  private final String text;
023  @JsonProperty("powerpack_uuid")
024  private String powerpackUUID;
025  private MessageType type = MessageType.SMS;
026  private URL url = null;
027  private String method = "POST";
028  private Boolean log = null;
029  private Boolean trackable = null;
030
031  /**
032   * @param source The phone number that will be shown as the sender ID.
033   * @param destination The numbers to which the message will be sent.
034   * @param text The text message that will be sent.
035   */
036  MessageCreator(String source, List<String> destination, String text) {
037    if (!Utils.allNotNull(source, destination, text)) {
038      throw new IllegalArgumentException("source, destination and text must not be null");
039    }
040
041    if (destination.contains(source)) {
042      throw new IllegalArgumentException("destination cannot include source");
043    }
044
045    this.source = source;
046    this.destination = destination;
047    this.text = text;
048  }
049
050  /**
051   * @param destination The numbers to which the message will be sent.
052   * @param text The text message that will be sent.
053   * @param powerpackUUID The powerpack UUID to be used.
054   */
055  MessageCreator(List<String> destination, String text, String powerpackUUID) {
056    if (!Utils.allNotNull(powerpackUUID, destination, text)) {
057      throw new IllegalArgumentException("powerpack uuid, destination and text must not be null");
058    }
059    this.destination = destination;
060    this.text = text;
061    this.powerpackUUID = powerpackUUID;
062  }
063
064  public String source() {
065    return this.source;
066  }
067
068  public List<String> destination() {
069    return this.destination;
070  }
071
072  public String text() {
073    return this.text;
074  }
075
076  public MessageType type() {
077    return this.type;
078  }
079
080  public URL url() {
081    return this.url;
082  }
083
084  public String method() {
085    return this.method;
086  }
087
088  public Boolean log() {
089    return this.log;
090  }
091
092  /**
093   * @param type Must be {@link MessageType#SMS}
094   */
095  public MessageCreator type(final MessageType type) {
096    this.type = type;
097    return this;
098  }
099
100  /**
101   * @param url The URL to which with the status of the message is sent.
102   */
103  public MessageCreator url(final URL url) {
104    this.url = url;
105    return this;
106  }
107
108  /**
109   * @param method The method used to call the url. Defaults to POST.
110   */
111  public MessageCreator method(final String method) {
112    this.method = method;
113    return this;
114  }
115
116  /**
117   * @param log If set to false, the content of this message will not be logged on the Plivo
118   * infrastructure and the dst value will be masked
119   */
120  public MessageCreator log(final Boolean log) {
121    this.log = log;
122    return this;
123  }
124
125  /**
126   * @param trackable 
127   */
128  public MessageCreator trackable(final Boolean trackable) {
129    this.trackable = trackable;
130    return this;
131  }
132
133
134  @Override
135  protected Call<MessageCreateResponse> obtainCall() {
136    return client().getApiService().messageSend(client().getAuthId(), this);
137  }
138}