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

base.jee.api.sql.DeleteThrottle Maven / Gradle / Ivy

/*
 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.sql;

import base.Query;
import base.jee.Constants;
import base.jee.api.model.Throttle;
import base.json.Json;
import base.security.PermissionException;
import base.security.User;

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 static base.jee.api.sql.util.Log.log;

public class DeleteThrottle extends Query {

	private SqlAPI api;
	private User user;
	private String uid;

	public DeleteThrottle(SqlAPI api, User user, String uid) throws PermissionException {

		if(api == null) {
			throw new IllegalArgumentException("Invalid parameter: api");
		}
		if(uid == null || uid.length() == 0 || uid.length() > Constants.MAX_EMAIL_LENGTH) {
			throw new IllegalArgumentException("Invalid parameter: uid");
		}
		if(user == null || !user.isAuthenticated()) {
			throw new PermissionException(this.getClass().getSimpleName(), user, "Requires an authenticated user.", "");
		}

		this.api = api;
		this.user = user;
		this.uid = uid;
	}

	public DeleteThrottle() {
	}

	@Override
	protected List execute() throws IOException {
		List results = new LinkedList<>();
		Connection c = null;
		PreparedStatement u = null;
		ResultSet r = null;

		try {
			c = api.getDataSource().getConnection();
			c.setAutoCommit(false);

			if(!user.hasRole(Constants.PERSON_MANAGE_ROLE)) {
				c.rollback();
				log(c, "WARN", user, "Permission denied invoking: " + DeleteThrottle.class.getSimpleName() + " " + getJsonParameters());
				c.commit();
				throw new IllegalStateException("You do not have permission to manage people.");
			}

			u = c.prepareStatement("delete from throttle where key_value=?");
			u.setString(1, uid);
			int count = u.executeUpdate();

			if(count > 0) {
				log(c, "INFO", user, "Removed throttle \"" + Json.escape(uid) + "\".");
				results.add(new Throttle(uid, null, 0));
			}

			u.close();
			u = 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(u != null) { try { u.close(); } catch(SQLException e) { } }
			if(c != null) {
				try { c.rollback(); } catch (SQLException e) { }
				try { c.close(); } catch (SQLException e) { }
			}
		}

		return results;
	}

	@Override
	public String getJsonParameters() {
		return "{" +
				"\"uid\":\"" + Json.escape(uid) + "\"," +
				"\"updator.person\":\"" + user.getPersonUuid() + "\"" +
				"}";
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy