base.jee.api.cassandra.SetPendingEmailStatus Maven / Gradle / Ivy
/**
* Creative commons Attribution-NonCommercial license.
*
* http://creativecommons.org/licenses/by-nc/2.5/au/deed.en_GB
*
* NO WARRANTY IS GIVEN OR IMPLIED, USE AT YOUR OWN RISK.
*/
package base.jee.api.cassandra;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import base.Query;
import base.StringQueryResult;
import base.json.Json;
import base.security.PermissionException;
import com.datastax.driver.core.Session;
import static base.jee.api.cassandra.util.Log.log;
/**
*/
public class SetPendingEmailStatus extends Query {
private CassandraAPI c;
private UUID uuid;
private String error;
public SetPendingEmailStatus(CassandraAPI c, UUID uuid, String error) {
this.c = c;
this.uuid = uuid;
this.error = error;
if(c == null) {
throw new IllegalArgumentException("Invalid parameter: c");
}
if(uuid == null) {
throw new IllegalArgumentException("Invalid parameter: uuid");
}
}
@Override
public Query newWithParameters(Map parameters) throws IOException, PermissionException {
throw new IllegalArgumentException("SetPendingEmailStatus may not be instantiated with parameter map");
}
public List execute() throws IOException {
List results = new LinkedList<>();
String token = null;
Connection c = null;
PreparedStatement t = null;
PreparedStatement s = null;
Session sn = this.c.getCassandraSession();
try {
c = this.c.getDataSource().getConnection();
c.setAutoCommit(false);
s = c.prepareStatement(
"select to_address,retries from email " +
"where uuid=? "
);
s.setString(1, uuid.toString());
ResultSet r = s.executeQuery();
if(r.next()) {
String to = r.getString(1);
int retries = r.getInt(2);
if(error != null) {
t = c.prepareStatement("update email set in_progress=0 where uuid=?");
t.setString(1, uuid.toString());
t.executeUpdate();
t.close();
log(sn, "WARN", null, "Failure delivering email uuid " + uuid + " to " + to + ". Attempt " + retries + ". Error: " + error);
} else {
t = c.prepareStatement("delete from email where uuid=?");
t.setString(1, uuid.toString());
t.executeUpdate();
log(sn, "INFO", null, "Delivered email uuid " + uuid + " to " + to);
t.close();
}
} else {
log(sn, "WARN", null, "SetPendingEmail called with invalid message uuid " + uuid);
}
r.close();
s.close();
c.commit();
} catch(SQLException e) {
throw new IOException(e);
} finally {
if(c != null) {
try { c.rollback(); } catch (SQLException e) { }
try { c.close(); } catch (SQLException e) { }
}
if(t != null) { try { t.close(); } catch (SQLException e) {} }
if(s != null) { try { s.close(); } catch (SQLException e) {} }
}
results.add(new StringQueryResult(token));
return results;
}
@Override
public String getJsonParameters() {
return "{" +
"\"error\":" + (error == null?"null":"\""+Json.escape(error)+"\"") + "" +
"\"uuid\":\"" + uuid + "\"" +
"}";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy