All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.jsmpp.bean.DeliveryReceipt Maven / Gradle / Ivy
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.jsmpp.bean;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.jsmpp.util.DeliveryReceiptState;
import org.jsmpp.util.InvalidDeliveryReceiptException;
/**
* @author uudashr
*
*/
public class DeliveryReceipt {
// attributes of delivery receipt
public static final String DELREC_ID = "id";
public static final String DELREC_SUB = "sub";
public static final String DELREC_DLVRD = "dlvrd";
public static final String DELREC_SUBMIT_DATE = "submit date";
public static final String DELREC_DONE_DATE = "done date";
public static final String DELREC_STAT = "stat";
public static final String DELREC_ERR = "err";
public static final String DELREC_TEXT = "Text";
/**
* Date format for the submit date and done date attribute
*/
private final SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyMMddHHmm");
private String id;
private int submitted;
private int delivered;
private Date submitDate;
private Date doneDate;
private DeliveryReceiptState finalStatus;
private String error;
private String text;
public DeliveryReceipt() {
}
public DeliveryReceipt(String formattedDelieryReceipt)
throws InvalidDeliveryReceiptException {
/*
* id:IIIIIIIIII sub:SSS dlvrd:DDD submit date:YYMMDDhhmm done
* date:YYMMDDhhmm stat:DDDDDDD err:E Text: ..........
*/
try {
id = getDeliveryReceiptValue(DeliveryReceipt.DELREC_ID, formattedDelieryReceipt);
submitted = Integer.parseInt(getDeliveryReceiptValue(
DeliveryReceipt.DELREC_SUB, formattedDelieryReceipt));
delivered = Integer.parseInt(getDeliveryReceiptValue(
DeliveryReceipt.DELREC_DLVRD, formattedDelieryReceipt));
submitDate = string2Date(getDeliveryReceiptValue(
DeliveryReceipt.DELREC_SUBMIT_DATE, formattedDelieryReceipt));
doneDate = string2Date(getDeliveryReceiptValue(
DeliveryReceipt.DELREC_DONE_DATE, formattedDelieryReceipt));
finalStatus = DeliveryReceiptState
.getByName(getDeliveryReceiptValue(
DeliveryReceipt.DELREC_STAT, formattedDelieryReceipt));
error = getDeliveryReceiptValue(DeliveryReceipt.DELREC_ERR, formattedDelieryReceipt);
text = getDeliveryReceiptTextValue(formattedDelieryReceipt);
} catch (Exception e) {
throw new InvalidDeliveryReceiptException(
"There is an error found when parsing delivery receipt", e);
}
}
public DeliveryReceipt(String id, int submitted, int delivered,
Date submitDate, Date doneDate, DeliveryReceiptState finalStatus,
String error, String text) {
this.id = id;
this.submitted = submitted;
this.delivered = delivered;
this.submitDate = submitDate;
this.doneDate = doneDate;
this.finalStatus = finalStatus;
this.error = error;
if (text.length() > 20) {
this.text = text.substring(0, 20);
} else {
this.text = text;
}
}
/**
* @return Returns the delivered.
*/
public int getDelivered() {
return delivered;
}
/**
* @param delivered The delivered to set.
*/
public void setDelivered(int delivered) {
this.delivered = delivered;
}
/**
* @return Returns the doneDate.
*/
public Date getDoneDate() {
return doneDate;
}
/**
* @param doneDate The doneDate to set.
*/
public void setDoneDate(Date doneDate) {
this.doneDate = doneDate;
}
/**
* @return Returns the error.
*/
public String getError() {
return error;
}
/**
* @param error The error to set.
*/
public void setError(String error) {
this.error = error;
}
/**
* @return Returns the finalStatus.
*/
public DeliveryReceiptState getFinalStatus() {
return finalStatus;
}
/**
* @param finalStatus The finalStatus to set.
*/
public void setFinalStatus(DeliveryReceiptState finalStatus) {
this.finalStatus = finalStatus;
}
/**
* @return Returns the id.
*/
public String getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(String id) {
this.id = id;
}
/**
* @return Returns the submitDate.
*/
public Date getSubmitDate() {
return submitDate;
}
/**
* @param submitDate The submitDate to set.
*/
public void setSubmitDate(Date submitDate) {
this.submitDate = submitDate;
}
/**
* @return Returns the submitted.
*/
public int getSubmitted() {
return submitted;
}
/**
* @param submitted The submitted to set.
*/
public void setSubmitted(int submitted) {
this.submitted = submitted;
}
/**
* @return Returns the text.
*/
public String getText() {
return text;
}
/**
* Set the text of delivery receipt. Text more than 20 characters will be trim automatically.
*
* @param text the text to set.
*/
public void setText(String text) {
if (text != null && text.length() > 20) {
this.text = text.substring(0, 20);
} else {
this.text = text;
}
}
public String toString() {
/*
* id:IIIIIIIIII sub:SSS dlvrd:DDD submit date:YYMMDDhhmm done
* date:YYMMDDhhmm stat:DDDDDDD err:E Text: . . . . . . . . .
*/
StringBuffer sBuf = new StringBuffer(120);
sBuf.append(DELREC_ID + ":" + id);
sBuf.append(" ");
sBuf.append(DELREC_SUB + ":" + intToString(submitted, 3));
sBuf.append(" ");
sBuf.append(DELREC_DLVRD + ":" + intToString(delivered, 3));
sBuf.append(" ");
sBuf.append(DELREC_SUBMIT_DATE + ":" + dateFormat.format(submitDate));
sBuf.append(" ");
sBuf.append(DELREC_DONE_DATE + ":" + dateFormat.format(doneDate));
sBuf.append(" ");
sBuf.append(DELREC_STAT + ":" + finalStatus);
sBuf.append(" ");
sBuf.append(DELREC_ERR + ":" + error);
sBuf.append(" ");
sBuf.append(DELREC_TEXT.toLowerCase() + ":" + text);
return sBuf.toString();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((dateFormat == null) ? 0 : dateFormat.hashCode());
result = prime * result + delivered;
result = prime * result
+ ((doneDate == null) ? 0 : doneDate.hashCode());
result = prime * result + ((error == null) ? 0 : error.hashCode());
result = prime * result
+ ((finalStatus == null) ? 0 : finalStatus.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result
+ ((submitDate == null) ? 0 : submitDate.hashCode());
result = prime * result + submitted;
result = prime * result + ((text == null) ? 0 : text.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final DeliveryReceipt other = (DeliveryReceipt)obj;
if (!hasEqualId(other)) {
return false;
}
if (submitted != other.submitted) {
return false;
}
if (delivered != other.delivered) {
return false;
}
if (!hasEqualSubmitDate(other)) {
return false;
}
if (!hasEqualDoneDate(other)) {
return false;
}
if (!hasEqualFinalStatus(other)) {
return false;
}
if (!hasEqualError(other)) {
return false;
}
if (!hasEqualText(other)) {
return false;
}
return true;
}
/**
* Create String representation of integer. Preceding 0 will be add as
* needed.
*
* @param value is the value.
* @param digit is the digit should be shown.
* @return the String representation of int value.
*/
private static String intToString(int value, int digit) {
StringBuffer sBuf = new StringBuffer(digit);
sBuf.append(Integer.toString(value));
while (sBuf.length() < digit) {
sBuf.insert(0, "0");
}
return sBuf.toString();
}
private boolean hasEqualId(DeliveryReceipt other) {
if (id == null) {
if (other.id != null) {
return false;
}
}
return id.equals(other.id);
}
private boolean hasEqualDoneDate(DeliveryReceipt other) {
if (doneDate == null) {
if (other.doneDate != null) {
return false;
}
}
return doneDate.equals(other.doneDate);
}
private boolean hasEqualError(DeliveryReceipt other) {
if (error == null) {
if (other.error != null) {
return false;
}
}
return error.equals(other.error);
}
private boolean hasEqualFinalStatus(DeliveryReceipt other) {
if (finalStatus == null) {
if (other.finalStatus != null) {
return false;
}
}
return finalStatus.equals(other.finalStatus);
}
private boolean hasEqualSubmitDate(DeliveryReceipt other) {
if (submitDate == null) {
if (other.submitDate != null) {
return false;
}
}
return submitDate.equals(other.submitDate);
}
private boolean hasEqualText(DeliveryReceipt other) {
if (text == null) {
if (other.text != null) {
return false;
}
}
return text.equals(other.text);
}
/**
* Get the delivery receipt attribute value.
*
* @param attrName is the attribute name.
* @param source the original source id:IIIIIIIIII sub:SSS dlvrd:DDD submit
* date:YYMMDDhhmm done date:YYMMDDhhmm stat:DDDDDDD err:E
* Text:....................
* @return the value of specified attribute.
* @throws IndexOutOfBoundsException
*/
private static String getDeliveryReceiptValue(String attrName, String source)
throws IndexOutOfBoundsException {
String tmpAttr = attrName + ":";
int startIndex = source.indexOf(tmpAttr);
if (startIndex < 0)
return null;
startIndex = startIndex + tmpAttr.length();
int endIndex = source.indexOf(" ", startIndex);
if (endIndex > 0)
return source.substring(startIndex, endIndex);
return source.substring(startIndex);
}
/**
* YYMMDDhhmm where:
*
* YY = last two digits of the year (00-99)
* MM = month (01-12)
* DD = day (01-31)
* hh = hour (00-23)
* mm = minute (00-59)
*
*
* Java format is (yyMMddHHmm).
*
* @param date in String format.
* @return
* @throws NumberFormatException if there is contains non number on
* date
parameter.
* @throws IndexOutOfBoundsException if the date length in String
* format is less than 10.
*/
private static Date string2Date(String date) {
int year = Integer.parseInt(date.substring(0, 2));
int month = Integer.parseInt(date.substring(2, 4));
int day = Integer.parseInt(date.substring(4, 6));
int hour = Integer.parseInt(date.substring(6, 8));
int minute = Integer.parseInt(date.substring(8, 10));
Calendar cal = Calendar.getInstance();
cal.set(convertTwoDigitYear(year), month - 1, day, hour, minute, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
private static int convertTwoDigitYear(int year) {
if (year >=0 && year <= 37) {
return 2000 + year;
} else if (year >= 38 && year <= 99) {
return 1900 + year;
} else {
// should never happen
return year;
}
}
/**
* @param source
* @return
* @throws IndexOutOfBoundsException
*/
private static String getDeliveryReceiptTextValue(String source) {
String tmpAttr = DeliveryReceipt.DELREC_TEXT + ":";
int startIndex = source.indexOf(tmpAttr);
if (startIndex < 0) {
tmpAttr = DeliveryReceipt.DELREC_TEXT.toLowerCase() + ":";
startIndex = source.indexOf(tmpAttr);
}
if (startIndex < 0) {
return null;
}
startIndex = startIndex + tmpAttr.length();
return source.substring(startIndex);
}
}