All Downloads are FREE. Search and download functionalities are using the official Maven repository.

base.jee.api.sql.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.sql;

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 javax.sql.DataSource;

import base.Query;
import base.StringQueryResult;
import base.json.Json;
import base.security.PermissionException;

import static base.jee.api.sql.util.Log.log;

/**
 */
public class SetPendingEmailStatus extends Query {

	private DataSource ds;
	private UUID uuid;
	private String error;

	public SetPendingEmailStatus(DataSource ds, UUID uuid, String error) {
		this.ds = ds;
		this.uuid = uuid;
		this.error = error;

		if(ds == null) {
			throw new IllegalArgumentException("Invalid parameter: ds");
		}
		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;
		ResultSet r = null;
		PreparedStatement t = null;
		PreparedStatement s = null;

		try {
			c = ds.getConnection();
			c.setAutoCommit(false);

			s = c.prepareStatement(
					"select to_address,retries from email " +
					"where uuid=? "
					);
			s.setString(1, uuid.toString());
			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(c, "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(c, "INFO", null, "Delivered email uuid " + uuid.toString().toUpperCase() + " to " + to);
					t.close();
				}
			} else {
				log(c, "WARN", null, "SetPendingEmail called with invalid message uuid " + uuid);
			}
			r.close();
			r = null;
			s.close();
			s = null;

			c.commit();
			c.close();
			c = null;
		} catch(SQLException e) {
			throw new IOException(e);
		} finally {
			if(r != null) { try { r.close(); } catch (SQLException e) {} }
			if(t != null) { try { t.close(); } catch (SQLException e) {} }
			if(s != null) { try { s.close(); } catch (SQLException e) {} }
			if(c != null) {
				try { c.rollback(); } catch (SQLException e) { }
				try { c.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