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

base.jee.api.cassandra.SetPendingEmailStatus Maven / Gradle / Ivy

Go to download

A collection of basic java utility classes that provide basic features for a standalone/simple JEE application. Backed by a Cassandra, MySQL, or SQLite database, it provides, web page templates, user and group management, and a searchable online audit log of all user activity.

The newest version!
/*
 This is free and unencumbered software released into the public domain.

 Anyone is free to copy, modify, publish, use, compile, sell, or
 distribute this software, either in source code form or as a compiled
 binary, for any purpose, commercial or non-commercial, and by any
 means.

 In jurisdictions that recognize copyright laws, the author or authors
 of this software dedicate any and all copyright interest in the
 software to the public domain. We make this dedication for the benefit
 of the public at large and to the detriment of our heirs and
 successors. We intend this dedication to be an overt act of
 relinquishment in perpetuity of all present and future rights to this
 software under copyright law.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
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