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

base.jee.api.cassandra.UpsertSetting 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.cassandra;

import java.io.IOException;

import com.datastax.driver.core.Session;
import com.datastax.driver.core.PreparedStatement;

import java.util.Map;

import base.Command;
import base.json.Json;
import base.jee.Constants;
import base.security.User;

import static base.jee.api.cassandra.util.InsertUpdateNotification.insertUpdateNotification;
import static base.jee.api.cassandra.util.Log.log;

public class UpsertSetting extends Command {

	private CassandraAPI c;
	private User user;
	private String name;
	private String value;

	public UpsertSetting(CassandraAPI c, User user, String name, String value) {
		this.c = c;
		this.name = name;
		this.value = value;
		this.user = user;

		if(c == null) {
			throw new IllegalArgumentException("Invalid parameter: c");
		}
		if(user != null && !user.isAuthenticated()) {
			throw new IllegalArgumentException("Requires an authenticated user.");
		}
		if(name == null || name.length() == 0) {
			throw new IllegalArgumentException("Invalid parameter: name");
		}
	}

	public UpsertSetting() {
	}

	@Override
	public Command newWithParameters(Map parameters) {
		return new UpsertSetting(
				(CassandraAPI)parameters.get("api"),
				(User)parameters.get("user"),
				(String)parameters.get("name"),
				(String)parameters.get("value"));
	}

	@Override
	protected void execute() throws IOException {
		Session s = c.getCassandraSession();

		if(user != null && !user.hasRole(Constants.SETTING_MANAGE_ROLE)) {
			log(s, "WARN", user, "Permission denied invoking: " + UpsertSetting.class.getSimpleName() + " " + getJsonParameters());
			throw new IllegalStateException("You do not have permission to manage settings.");
		}

		PreparedStatement p = s.prepare("insert into setting (name,value) values(?,?)");
		s.execute(p.bind(name, value));

		if(user != null && user.getPersonUuid() != null) {
			insertUpdateNotification(s, user, "setting", "update", name + "|" + value);
		}

		log(s, "INFO", user, "Updated setting " + name + "=" + value);
	}

	@Override
	public String getJsonParameters() {
		return "{" +
				"\"name\":\"" + Json.escape(name) + "\"," +
				"\"value\":" + (value == null?"null":"\"" + Json.escape(value) + "\"") + "\"" +
				"}";
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy