base.jee.api.sql.RemoveSetting 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.SQLException;
import java.util.Map;
import javax.sql.DataSource;
import base.Command;
import base.jee.Constants;
import base.json.Json;
import base.security.PermissionException;
import base.security.User;
import static base.jee.api.sql.util.InsertUpdateNotification.insertUpdateNotification;
import static base.jee.api.sql.util.Log.log;
public class RemoveSetting extends Command {
private DataSource ds;
private User user;
private String name;
public RemoveSetting(DataSource ds, User user, String name) throws PermissionException {
this.ds = ds;
this.name = name;
this.user = user;
if(ds == null) {
throw new IllegalArgumentException("Invalid parameter: ds");
}
if(name == null || name.length() == 0) {
throw new IllegalArgumentException("Invalid parameter: name");
}
if(user == null || !user.isAuthenticated()) {
throw new PermissionException(this.getClass().getSimpleName(), user, "Requires an authenticated user.", "");
}
}
public RemoveSetting() {
}
@Override
public Command newWithParameters(Map parameters) throws PermissionException {
return new RemoveSetting(
(DataSource)parameters.get("ds"),
(User)parameters.get("user"),
(String)parameters.get("name"));
}
@Override
protected void execute() throws IOException {
Connection c = null;
try {
c = ds.getConnection();
c.setAutoCommit(false);
if(!user.hasRole(Constants.SETTING_MANAGE_ROLE)) {
c.rollback();
log(c, "WARN", user, "Permission denied invoking: " + RemoveSetting.class.getSimpleName() + " " + getJsonParameters());
c.commit();
throw new IllegalStateException("You do not have permission to manage settings.");
}
PreparedStatement s = c.prepareStatement("delete from setting where name=?");
s.setString(1, name);
s.execute();
s.close();
s = null;
log(c, "INFO", user, "Remove setting " + name);
insertUpdateNotification(c, user, "setting", "delete", name);
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) { }
}
}
}
@Override
public String getJsonParameters() {
return "{" +
"\"name\":\"" + Json.escape(name) + "\"" +
"}";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy