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

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

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

public class GetTemplateResources extends Query {

	private DataSource ds;
	private String template;

	public GetTemplateResources(DataSource ds, String template) {
		this.ds = ds;
		this.template = template;
	}

	public List execute() throws IOException {
		List results = new LinkedList<>();
		Connection c = null;
		try {
			c = ds.getConnection();
			c.setAutoCommit(false);

			PreparedStatement s = c.prepareStatement(
					"select resource, content, isbinary "+
					"from template_resource " +
					"where template=?");
			s.setString(1, template);
			ResultSet r = s.executeQuery();
			while(r.next()) {
				results.add(new base.template.Resource(r.getString(1), r.getBytes(2), r.getBoolean(3)));
			}
			r.close();
			s.close();

			c.rollback();
		} catch(SQLException e) {
			throw new IOException(e);
		} finally {
			if(c != null) {
				try { c.rollback(); } catch (SQLException e) { }
				try { c.close(); } catch (SQLException e) { }
			}
		}
		return results;
	}

	@Override
	public String getJsonParameters() {
		return "{" +
				"\"template\":\"" + Json.escape(template) + "\"" +
				"}";
	}
	@Override
	public Query newWithParameters(Map parameters) throws IOException, PermissionException {
		User user = (User)parameters.get("user");

		if(!user.hasRole(Constants.TEMPLATE_MANAGE_ROLE)) {
			throw new PermissionException(getClass().getSimpleName(), user, "You do not have permission to manage templates.", Constants.TEMPLATE_MANAGE_ROLE);
		}

		return new GetTemplateResources(
				(DataSource)parameters.get("ds"),
				(String)parameters.get("template"));
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy