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

base.jee.api.sql.GetBatchResults 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.StringQueryResult;
import base.jee.Constants;
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 java.util.Map;
import java.util.UUID;

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

public class GetBatchResults extends Query {

	private SqlAPI api;
	private User user;
	private UUID batch;

	public GetBatchResults(SqlAPI api, User user, UUID batch) throws PermissionException {
		this.api = api;
		this.user = user;
		this.batch = batch;

		if(api == null) {
			throw new IllegalArgumentException("\"api\" parameter must not be null.");
		}
		if(user == null || !user.isAuthenticated()) {
			throw new PermissionException(this.getClass().getSimpleName(), user, "You don't have permission to complete this operation.", null);
		}
		if(batch == null) {
			throw new IllegalArgumentException("Please specify batch uuid.");
		}

	}

	public GetBatchResults() {
	}

	@Override
	public Query newWithParameters(Map parameters) throws PermissionException {
		return new GetBatchResults(
				(SqlAPI)parameters.get("api"),
				(User)parameters.get("user"),
				UUID.fromString((String) parameters.get("batch")));
	}

	public List execute() throws IOException {
		List results = new LinkedList<>();
		Connection c = null;
		PreparedStatement s = null;
		PreparedStatement t = null;
		ResultSet r = null;

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

			UUID requestor = null;
			s = c.prepareStatement("select requestor, executed from batch_load_info where uuid=?");
			s.setString(1, batch.toString());
			r = s.executeQuery();
			if(r.next()) {
				requestor = UUID.fromString(r.getString(1));
			}
			r.close();
			r = null;
			s.close();
			s = null;

			if(!user.hasRole(Constants.AUDIT_ROLE) && !user.getPersonUuid().equals(requestor)) {
				log(c, "WARN", user, "Permission denied invoking: " + GetBatchResults.class.getSimpleName() + " " + getJsonParameters());
				c.commit();
				throw new PermissionException(this.getClass().getSimpleName(), user, "You do not have permission to access audit information.", Constants.AUDIT_ROLE);
			}

			t = c.prepareStatement("select value from batch_load_result where uuid=? order by sort_order");
			t.setString(1, batch.toString());
			r = t.executeQuery();
			while(r.next()) {
				results.add(new StringQueryResult(r.getString(1)));
			}
			r.close();
			r = null;
			t.close();
			t = 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) { }
			}
		}
		return results;
	}

	@Override
	public String getJsonParameters() {
		return "{" +
				"}";
	}

	private UUID nullableUuid(String uuid) {
		if(uuid == null || uuid.length() == 0) {
			return null;
		}
		return UUID.fromString(uuid);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy