View Javadoc

1   /*
2    * Copyright  2004-2005 Stefan Reuter
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package net.sf.asterisk.fastagi.command;
18  
19  /***
20   * Say a given time, returning early if any of the given DTMF digits are
21   * pressed.<br>
22   * Returns 0 if playback completes without a digit being pressed, or the ASCII
23   * numerical value of the digit if one was pressed or -1 on error/hangup.<br>
24   * Available since Asterisk 1.2.
25   * 
26   * @since 0.2
27   * @author srt
28   * @version $Id: SayDateTimeCommand.java,v 1.1 2005/11/27 15:22:40 srt Exp $
29   */
30  public class SayDateTimeCommand extends AGICommand
31  {
32      /***
33       * Serial version identifier
34       */
35      private static final long serialVersionUID = -976344744239948036L;
36  
37      private static final String DEFAULT_FORMAT = "ABdY 'digits/at' IMp";
38  
39      private long time;
40      private String escapeDigits;
41      private String format;
42      private String timezone;
43  
44      /***
45       * Creates a new SayDateTimeCommand that says the given time.
46       * 
47       * @param time the time to say in seconds elapsed since 00:00:00 on January
48       *            1, 1970, Coordinated Universal Time (UTC)
49       */
50      public SayDateTimeCommand(long time)
51      {
52          this.time = time;
53      }
54  
55      /***
56       * Creates a new SayDateTimeCommand that says the given time and allows
57       * interruption by one of the given escape digits.
58       * 
59       * @param time the time to say in seconds elapsed since 00:00:00 on January
60       *            1, 1970, Coordinated Universal Time (UTC)
61       * @param escapeDigits the digits that allow the user to interrupt this
62       *            command or <code>null</code> for none.
63       */
64      public SayDateTimeCommand(long time, String escapeDigits)
65      {
66          this.time = time;
67          this.escapeDigits = escapeDigits;
68      }
69  
70      /***
71       * Creates a new SayDateTimeCommand that says the given time in the given
72       * format and allows interruption by one of the given escape digits.
73       * 
74       * @param time the time to say in seconds elapsed since 00:00:00 on January
75       *            1, 1970, Coordinated Universal Time (UTC)
76       * @param escapeDigits the digits that allow the user to interrupt this
77       *            command or <code>null</code> for none.
78       * @param format the format the time should be said in
79       */
80      public SayDateTimeCommand(long time, String escapeDigits, String format)
81      {
82          this.time = time;
83          this.escapeDigits = escapeDigits;
84          this.format = format;
85      }
86  
87      /***
88       * Creates a new SayDateTimeCommand that says the given time in the given
89       * format and timezone and allows interruption by one of the given escape
90       * digits.
91       * 
92       * @param time the time to say in seconds elapsed since 00:00:00 on January
93       *            1, 1970, Coordinated Universal Time (UTC)
94       * @param escapeDigits the digits that allow the user to interrupt this
95       *            command or <code>null</code> for none.
96       * @param format the format the time should be said in
97       * @param timezone the timezone to use when saying the time, for example
98       *            "UTC" or "Europe/Berlin".
99       */
100     public SayDateTimeCommand(long time, String escapeDigits, String format,
101             String timezone)
102     {
103         this.time = time;
104         this.escapeDigits = escapeDigits;
105         this.format = format;
106         this.timezone = timezone;
107     }
108 
109     /***
110      * Returns the time to say in seconds elapsed since 00:00:00 on January 1,
111      * 1970, Coordinated Universal Time (UTC).
112      * 
113      * @return the time to say in seconds elapsed since 00:00:00 on January 1,
114      *         1970, Coordinated Universal Time (UTC)
115      */
116     public long getTime()
117     {
118         return time;
119     }
120 
121     /***
122      * Returns the time to say in seconds elapsed since 00:00:00 on January 1,
123      * 1970, Coordinated Universal Time (UTC).<br>
124      * This property is mandatory.
125      * 
126      * @param time the time to say in seconds elapsed since 00:00:00 on January
127      *            1, 1970, Coordinated Universal Time (UTC)
128      */
129     public void setTime(long time)
130     {
131         this.time = time;
132     }
133 
134     /***
135      * Returns the digits that allow the user to interrupt this command.
136      * 
137      * @return the digits that allow the user to interrupt this command.
138      */
139     public String getEscapeDigits()
140     {
141         return escapeDigits;
142     }
143 
144     /***
145      * Sets the digits that allow the user to interrupt this command.
146      * 
147      * @param escapeDigits the digits that allow the user to interrupt this
148      *            command or <code>null</code> for none.
149      */
150     public void setEscapeDigits(String escapeDigits)
151     {
152         this.escapeDigits = escapeDigits;
153     }
154 
155     /***
156      * Returns the format the time should be said in.
157      * 
158      * @return the format the time should be said in
159      */
160     public String getFormat()
161     {
162         return format;
163     }
164 
165     /***
166      * Sets the format the time should be said in.<br>
167      * See <code>voicemail.conf</code>.<br>
168      * Defaults to "ABdY 'digits/at' IMp".
169      * 
170      * @param format the format the time should be said in
171      */
172     public void setFormat(String format)
173     {
174         this.format = format;
175     }
176 
177     /***
178      * Returns the timezone to use when saying the time.
179      * 
180      * @return the timezone to use when saying the time.
181      */
182     public String getTimezone()
183     {
184         return timezone;
185     }
186 
187     /***
188      * Sets the timezone to use when saying the time.<br>
189      * A list of available timezones is available in
190      * <code>/usr/share/zoneinfo</code> on your Asterisk server.<br>
191      * Defaults to machine default.
192      * 
193      * @param timezone the timezone to use when saying the time, for example
194      *            "UTC" or "Europe/Berlin".
195      */
196     public void setTimezone(String timezone)
197     {
198         this.timezone = timezone;
199     }
200 
201     public String buildCommand()
202     {
203         StringBuffer sb;
204 
205         sb = new StringBuffer("SAY DATETIME ");
206         sb.append(time);
207         sb.append(" ");
208         sb.append(escapeAndQuote(escapeDigits));
209 
210         if (format == null && timezone != null)
211         {
212             sb.append(" ");
213             sb.append(escapeAndQuote(DEFAULT_FORMAT));
214         }
215         if (format != null)
216         {
217             sb.append(" ");
218             sb.append(escapeAndQuote(format));
219         }
220 
221         if (timezone != null)
222         {
223             sb.append(" ");
224             sb.append(escapeAndQuote(timezone));
225         }
226 
227         return sb.toString();
228     }
229 }