View Javadoc
1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.mobility.push.entity;
17  
18  import java.io.Serializable;
19  import java.sql.Timestamp;
20  
21  import javax.persistence.Column;
22  import javax.persistence.Entity;
23  import javax.persistence.GeneratedValue;
24  import javax.persistence.GenerationType;
25  import javax.persistence.Id;
26  import javax.persistence.NamedQueries;
27  import javax.persistence.NamedQuery;
28  import javax.persistence.Table;
29  import javax.persistence.Version;
30  
31  /**
32   * A class which will serve as a tuple for messages that has to be sent.
33   * This class will be read by a Service querying that will send the unsent messages and keep track of retries
34   * 
35   * @author Kuali Mobility Team (mobility.dev@kuali.org)
36   * @since 2.1.0
37   */
38  @NamedQueries({
39  	/**
40  	 * Query to find all PushDeviceTuple instances that needs to be sent
41  	 */
42  	@NamedQuery(
43  		name = "PushDeviceTuple.findUnsent",
44  		query="SELECT t FROM PushDeviceTuple t WHERE t.status = 0 OR t.status = 2"
45  	),
46  	/**
47  	 * Query to find devices that was linked to a specific push
48  	 */
49  	@NamedQuery(
50  		name = "PushDeviceTuple.findPushDevices",
51  		query="SELECT d from PushDeviceTuple t, Device d  WHERE t.deviceId = d.id and t.pushId = :pushId"
52  	),
53  	@NamedQuery(
54  		name="PushDeviceTuple.countUnsent",
55  		query="select count(t) from PushDeviceTuple t where t.status = 0"
56  	),
57  	@NamedQuery(
58  			name="PushDeviceTuple.findTuplesForPush",
59  			query="select t from PushDeviceTuple t where t.pushId = :pushId"
60  	),
61      @NamedQuery(
62              name="PushDeviceTuple.deleteForStatus",
63              query="DELETE PushDeviceTuple t WHERE t.status = :status"
64      )
65  })
66  @Entity
67  @Table(name="KME_PSHDEV_T")
68  public class PushDeviceTuple implements Serializable {
69  
70  	/** Status indicating that the message is pending to be sent, i.e a message just added */
71  	public static final int STATUS_PENDING = 0;
72  
73  	/** Status indicatiing that the message has been sent */
74  	public static final int STATUS_SENT = 1;
75  
76  	/** Status indicating that sending the message has failed once, but waiting for a retry attempt */
77  	public static final int STATUS_WAITING_RETRY = 2;
78  
79  	/** 
80  	 * Status indicating the a maximum number of retries has failed, therefore the message is
81  	 *  seen as failed, and no further retries will be attempted */
82  	public static final int STATUS_FAILED = 3;
83  
84  	/** Serial version UID*/
85  	private static final long serialVersionUID = 9083352553678796701L;
86  
87  	/**
88  	 * Id for this PushDeviceTuple
89  	 */
90  	@Id
91  	@GeneratedValue(strategy = GenerationType.TABLE)
92  	@Column(name="ID")
93  	private Long tupleId;
94  
95  	/**
96  	 * ID of the <code>Push</code> messages this class relates to.
97  	 */
98  	@Column(name="PID")
99  	private Long pushId;
100 
101 	/**
102 	 * ID of the devices this message should be sent too.
103 	 */
104 	@Column(name="DID")
105 	private Long deviceId;
106 
107 	/**
108 	 * Last time the status of this object has changed
109 	 */
110 	@Column(name="PST_TS")
111 	private Timestamp postedTimestamp;
112 
113 	/**
114 	 * The current status of this <code>PushDeviceTuple</code>
115 	 */
116 	@Column(name="STATUS")
117 	private int status;
118 
119 	/**
120 	 * The incremental version number for this 
121 	 */
122 	@Version
123 	@Column(name="VER_NBR")
124 	private Long versionNumber;
125 
126 	/**
127 	 * Creates a new instance of a <code>PushDeviceTuple</code>
128 	 */
129 	public PushDeviceTuple(){}
130 
131 	/**
132 	 * Returns the id of this <code>PushDeviceTuple</code>
133 	 * @return
134 	 */
135 	public Long getId() {
136 		return tupleId;
137 	}
138 
139 
140 	/**
141 	 * Sets the id of this <code>
142 	 * @param tupleId
143 	 */
144 	public void setId(Long tupleId) {
145 		this.tupleId = tupleId;
146 	}
147 
148 
149 	/**
150 	 * Gets the ID of the <code>Push</code> this <code>PushDeviceTuple</code> is
151 	 * linked to.
152 	 * @return
153 	 */
154 	public Long getPushId() {
155 		return pushId;
156 	}
157 
158 
159 	/**
160 	 * Sets the ID of the <code>Push</code> this <code>PushDeviceTuple</code> is
161 	 * linked to.
162 	 * @return ID of the <code>Push</code> this <code>PushDeviceTuple</code> is
163 	 * linked to.
164 	 */
165 	public void setPushId(Long pushId) {
166 		this.pushId = pushId;
167 	}
168 
169 
170 	/**
171 	 * Gets the ID of the device this <code>PushDeviceTuple</code> is linked to.
172 	 * @return ID of the device this <code>PushDeviceTuple</code> is linked to.
173 	 */
174 	public Long getDeviceId() {
175 		return deviceId;
176 	}
177 
178 
179 	/**
180 	 * Gets the ID of the device this <code>PushDeviceTuple</code> is linked to.
181 	 * @return ID of the device this <code>PushDeviceTuple</code> is linked to.
182 	 */
183 	public void setDeviceId(Long deviceId) {
184 		this.deviceId = deviceId;
185 	}
186 
187 
188 	/**
189 	 * Gets the timestamp of the last update of this <code>PushDeviceTuple</code>
190 	 * @return
191 	 */
192 	public Timestamp getPostedTimestamp() {
193 		return postedTimestamp;
194 	}
195 
196 
197 	/**
198 	 * Gets the timestamp of the last update of this <code>PushDeviceTuple</code>
199 	 * @return The timestamp of the last update of this <code>PushDeviceTuple</code>
200 	 */
201 	public void setPostedTimestamp(Timestamp postedTimestamp) {
202 		this.postedTimestamp = postedTimestamp;
203 	}
204 
205 
206 	/**
207 	 * Sets the status of this <code>PushDeviceTuple</code>
208 	 * @param status New status for this <code>PushDeviceTuple</code>
209 	 */
210 	public void setStatus(int status){
211 		this.status = status;
212 	}
213 
214 	/**
215 	 * Returns this status of this  <code>PushDeviceTuple</code>
216 	 * @return Status of this  <code>PushDeviceTuple</code>
217 	 */
218 	public int getStatus(){
219 		return this.status;
220 	}
221 
222 
223 	/**
224 	 * Sets the status of this <code>PushDeviceTuple</code> to 
225 	 * <code>PushDeviceTuple.STATUS_SENT</code>
226 	 */
227 	public void setSent() {
228 		this.setStatus(STATUS_SENT);
229 	}
230 
231 	/**
232 	 * Returns true if the status of this <code>PushDeviceTuple</code> is
233 	 * <code>PushDeviceTuple.STATUS_SENT</code>
234 	 * @return
235 	 */
236 	public boolean isSent() {
237 		return this.status == STATUS_SENT;
238 	}
239 
240 
241 	/**
242 	 * Returns true if the status of this <code>PushDeviceTuple</code> is
243 	 * <code>PushDeviceTuple.STATUS_WAITING_RETRY</code>
244 	 * @return
245 	 */
246 	public boolean isWaitingRetry(){
247 		return this.status == STATUS_WAITING_RETRY;
248 	}
249 
250 
251 	/**
252 	 * Sets the status of this <code>PushDeviceTuple</code> to 
253 	 * <code>PushDeviceTuple.STATUS_WAITING_RETRY</code>
254 	 */
255 	public void setWaitingRetry(){
256 		this.setStatus(STATUS_WAITING_RETRY);
257 	}
258 
259 
260 	/**
261 	 * Gets the version number for this <code>PushDeviceTuple</code>
262 	 * @return Version number for this <code>PushDeviceTuple</code>
263 	 */
264 	public Long getVersionNumber() {
265 		return versionNumber;
266 	}
267 
268 
269 	/**
270 	 * Sets the version number for this <code>PushDeviceTuple</code>
271 	 * @param Version number for this <code>PushDeviceTuple</code>
272 	 */
273 	public void setVersionNumber(Long versionNumber) {
274 		this.versionNumber = versionNumber;
275 	}
276 
277 	/*
278 	 * (non-Javadoc)
279 	 * @see java.lang.Object#toString()
280 	 */
281 	@Override
282 	public String toString() {
283 		String newline = "\r\n";
284 
285 		String str = newline + "ID:          " + this.getId();
286 		str = str + newline + "PushID:     " + this.getPushId();
287 		str = str + newline + "DeviceID:   " + this.getDeviceId();
288 		str = str + newline + "Sent:       " + (this.isSent() ? "True" : "False");
289 		str = str + newline + "Timestamp:  " + this.getPostedTimestamp();    
290 		return str;
291 	}
292 
293 
294 }