Collection<OutboundMessage> msgList = new ArrayList<OutboundMessage>();
while (true)
{
try
{
OutboundMessage msg;
Statement cmd;
PreparedStatement pst;
ResultSet rs;
int msgCount;
msgCount = 1;
con = getDbConnection();
cmd = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
pst = con.prepareStatement("update " + getProperty("tables.sms_out", "smsserver_out") + " set status = 'Q' where id = ? ");
rs = cmd.executeQuery("select id, type, recipient, text, wap_url, wap_expiry_date, wap_signal, create_date, originator, encoding, status_report, flash_sms, src_port, dst_port, sent_date, ref_no, priority, status, errors, gateway_id from " + getProperty("tables.sms_out", "smsserver_out") + " where status = 'U' order by priority desc, id");
while (rs.next())
{
if (msgCount > Integer.parseInt(getProperty("batch_size"))) break;
if (getServer().checkPriorityTimeFrame(rs.getInt("priority")))
{
switch (rs.getString("type").charAt(0))
{
case 'O':
switch (rs.getString("encoding").charAt(0))
{
case '7':
msg = new OutboundMessage(rs.getString("recipient").trim(), rs.getString("text").trim());
msg.setEncoding(MessageEncodings.ENC7BIT);
break;
case '8':
{
String text = rs.getString("text").trim();
byte bytes[] = new byte[text.length() / 2];
for (int i = 0; i < text.length(); i += 2)
{
int value = (Integer.parseInt("" + text.charAt(i), 16) * 16) + (Integer.parseInt("" + text.charAt(i + 1), 16));
bytes[i / 2] = (byte) value;
}
msg = new OutboundBinaryMessage(rs.getString("recipient").trim(), bytes);
}
break;
case 'U':
msg = new OutboundMessage(rs.getString("recipient").trim(), rs.getString("text").trim());
msg.setEncoding(MessageEncodings.ENCUCS2);
break;
default:
msg = new OutboundMessage(rs.getString("recipient").trim(), rs.getString("text").trim());
msg.setEncoding(MessageEncodings.ENC7BIT);
break;
}
if (rs.getInt("flash_sms") == 1) msg.setFlashSms(true);
if (rs.getInt("src_port") != -1)
{
msg.setSrcPort(rs.getInt("src_port"));
msg.setDstPort(rs.getInt("dst_port"));
}
break;
case 'W':
Date wapExpiryDate;
WapSISignals wapSignal;
if (rs.getTime("wap_expiry_date") == null)
{
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DAY_OF_YEAR, 7);
wapExpiryDate = cal.getTime();
}
else wapExpiryDate = rs.getTimestamp("wap_expiry_date");
if (rs.getString("wap_signal") == null) wapSignal = WapSISignals.NONE;
else
{
switch (rs.getString("wap_signal").charAt(0))
{
case 'N':
wapSignal = WapSISignals.NONE;
break;
case 'L':
wapSignal = WapSISignals.LOW;
break;
case 'M':
wapSignal = WapSISignals.MEDIUM;
break;
case 'H':
wapSignal = WapSISignals.HIGH;
break;
case 'D':
wapSignal = WapSISignals.DELETE;
break;
default:
wapSignal = WapSISignals.NONE;
}
}
msg = new OutboundWapSIMessage(rs.getString("recipient").trim(), new URL(rs.getString("wap_url").trim()), rs.getString("text").trim(), wapExpiryDate, wapSignal);
break;
default:
throw new Exception("Message type '" + rs.getString("type") + "' is unknown!");
}
msg.setPriority(rs.getInt("priority"));
if (rs.getInt("status_report") == 1) msg.setStatusReport(true);
if ((rs.getString("originator") != null) && (rs.getString("originator").length() > 0)) msg.setFrom(rs.getString("originator").trim());
msg.setGatewayId(rs.getString("gateway_id").trim());
msgList.add(msg);
getMessageCache().put(msg.getMessageId(), rs.getInt("id"));
pst.setInt(1, rs.getInt("id"));
pst.executeUpdate();
con.commit();
msgCount++;
}