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  })
62  @Entity
63  @Table(name="KME_PSHDEV_T")
64  public class PushDeviceTuple implements Serializable {
65  
66  	/** Status indicating that the message is pending to be sent, i.e a message just added */
67  	public static final int STATUS_PENDING = 0;
68  
69  	/** Status indicatiing that the message has been sent */
70  	public static final int STATUS_SENT = 1;
71  
72  	/** Status indicating that sending the message has failed once, but waiting for a retry attempt */
73  	public static final int STATUS_WAITING_RETRY = 2;
74  
75  	/** 
76  	 * Status indicating the a maximum number of retries has failed, therefore the message is
77  	 *  seen as failed, and no further retries will be attempted */
78  	public static final int STATUS_FAILED = 3;
79  
80  	/** Serial version UID*/
81  	private static final long serialVersionUID = 9083352553678796701L;
82  
83  	/**
84  	 * Id for this PushDeviceTuple
85  	 */
86  	@Id
87  	@GeneratedValue(strategy = GenerationType.TABLE)
88  	@Column(name="ID")
89  	private Long tupleId;
90  
91  	/**
92  	 * ID of the <code>Push</code> messages this class relates to.
93  	 */
94  	@Column(name="PID")
95  	private Long pushId;
96  
97  	/**
98  	 * ID of the devices this message should be sent too.
99  	 */
100 	@Column(name="DID")
101 	private Long deviceId;
102 
103 	/**
104 	 * Last time the status of this object has changed
105 	 */
106 	@Column(name="PST_TS")
107 	private Timestamp postedTimestamp;
108 
109 	/**
110 	 * The current status of this <code>PushDeviceTuple</code>
111 	 */
112 	@Column(name="STATUS")
113 	private int status;
114 
115 	/**
116 	 * The incremental version number for this 
117 	 */
118 	@Version
119 	@Column(name="VER_NBR")
120 	private Long versionNumber;
121 
122 	/**
123 	 * Creates a new instance of a <code>PushDeviceTuple</code>
124 	 */
125 	public PushDeviceTuple(){}
126 
127 	/**
128 	 * Returns the id of this <code>PushDeviceTuple</code>
129 	 * @return
130 	 */
131 	public Long getId() {
132 		return tupleId;
133 	}
134 
135 
136 	/**
137 	 * Sets the id of this <code>
138 	 * @param tupleId
139 	 */
140 	public void setId(Long tupleId) {
141 		this.tupleId = tupleId;
142 	}
143 
144 
145 	/**
146 	 * Gets the ID of the <code>Push</code> this <code>PushDeviceTuple</code> is
147 	 * linked to.
148 	 * @return
149 	 */
150 	public Long getPushId() {
151 		return pushId;
152 	}
153 
154 
155 	/**
156 	 * Sets the ID of the <code>Push</code> this <code>PushDeviceTuple</code> is
157 	 * linked to.
158 	 * @return ID of the <code>Push</code> this <code>PushDeviceTuple</code> is
159 	 * linked to.
160 	 */
161 	public void setPushId(Long pushId) {
162 		this.pushId = pushId;
163 	}
164 
165 
166 	/**
167 	 * Gets the ID of the device this <code>PushDeviceTuple</code> is linked to.
168 	 * @return ID of the device this <code>PushDeviceTuple</code> is linked to.
169 	 */
170 	public Long getDeviceId() {
171 		return deviceId;
172 	}
173 
174 
175 	/**
176 	 * Gets the ID of the device this <code>PushDeviceTuple</code> is linked to.
177 	 * @return ID of the device this <code>PushDeviceTuple</code> is linked to.
178 	 */
179 	public void setDeviceId(Long deviceId) {
180 		this.deviceId = deviceId;
181 	}
182 
183 
184 	/**
185 	 * Gets the timestamp of the last update of this <code>PushDeviceTuple</code>
186 	 * @return
187 	 */
188 	public Timestamp getPostedTimestamp() {
189 		return postedTimestamp;
190 	}
191 
192 
193 	/**
194 	 * Gets the timestamp of the last update of this <code>PushDeviceTuple</code>
195 	 * @return The timestamp of the last update of this <code>PushDeviceTuple</code>
196 	 */
197 	public void setPostedTimestamp(Timestamp postedTimestamp) {
198 		this.postedTimestamp = postedTimestamp;
199 	}
200 
201 
202 	/**
203 	 * Sets the status of this <code>PushDeviceTuple</code>
204 	 * @param status New status for this <code>PushDeviceTuple</code>
205 	 */
206 	public void setStatus(int status){
207 		this.status = status;
208 	}
209 
210 	/**
211 	 * Returns this status of this  <code>PushDeviceTuple</code>
212 	 * @return Status of this  <code>PushDeviceTuple</code>
213 	 */
214 	public int getStatus(){
215 		return this.status;
216 	}
217 
218 
219 	/**
220 	 * Sets the status of this <code>PushDeviceTuple</code> to 
221 	 * <code>PushDeviceTuple.STATUS_SENT</code>
222 	 */
223 	public void setSent() {
224 		this.setStatus(STATUS_SENT);
225 	}
226 
227 	/**
228 	 * Returns true if the status of this <code>PushDeviceTuple</code> is
229 	 * <code>PushDeviceTuple.STATUS_SENT</code>
230 	 * @return
231 	 */
232 	public boolean isSent() {
233 		return this.status == STATUS_SENT;
234 	}
235 
236 
237 	/**
238 	 * Returns true if the status of this <code>PushDeviceTuple</code> is
239 	 * <code>PushDeviceTuple.STATUS_WAITING_RETRY</code>
240 	 * @return
241 	 */
242 	public boolean isWaitingRetry(){
243 		return this.status == STATUS_WAITING_RETRY;
244 	}
245 
246 
247 	/**
248 	 * Sets the status of this <code>PushDeviceTuple</code> to 
249 	 * <code>PushDeviceTuple.STATUS_WAITING_RETRY</code>
250 	 */
251 	public void setWaitingRetry(){
252 		this.setStatus(STATUS_WAITING_RETRY);
253 	}
254 
255 
256 	/**
257 	 * Gets the version number for this <code>PushDeviceTuple</code>
258 	 * @return Version number for this <code>PushDeviceTuple</code>
259 	 */
260 	public Long getVersionNumber() {
261 		return versionNumber;
262 	}
263 
264 
265 	/**
266 	 * Sets the version number for this <code>PushDeviceTuple</code>
267 	 * @param Version number for this <code>PushDeviceTuple</code>
268 	 */
269 	public void setVersionNumber(Long versionNumber) {
270 		this.versionNumber = versionNumber;
271 	}
272 
273 	/*
274 	 * (non-Javadoc)
275 	 * @see java.lang.Object#toString()
276 	 */
277 	@Override
278 	public String toString() {
279 		String newline = "\r\n";
280 
281 		String str = newline + "ID:          " + this.getId();
282 		str = str + newline + "PushID:     " + this.getPushId();
283 		str = str + newline + "DeviceID:   " + this.getDeviceId();
284 		str = str + newline + "Sent:       " + (this.isSent() ? "True" : "False");
285 		str = str + newline + "Timestamp:  " + this.getPostedTimestamp();    
286 		return str;
287 	}
288 
289 
290 }