base.jee.api.cassandra.UpsertPersonSetting Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of base Show documentation
Show all versions of base Show documentation
A collection of basic java utility classes that provide basic features for a standalone/simple JEE application. Backed by a Cassandra, MySQL, or SQLite database, it provides, web page templates, user and group management, and a searchable online audit log of all user activity.
/*
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 base.Command;
import base.jee.Constants;
import base.json.Json;
import base.security.User;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.Session;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;
import static base.jee.api.cassandra.util.Log.log;
public class UpsertPersonSetting extends Command {
private CassandraAPI c;
private User user;
private UUID person;
private String name;
private String value;
public UpsertPersonSetting(CassandraAPI c, User user, UUID person, String name, String value) {
this.c = c;
this.person = person;
this.name = name;
this.value = value;
this.user = user;
if(person == null) {
throw new IllegalArgumentException("Invalid parameter: person");
}
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 UpsertPersonSetting() {
}
@Override
public Command newWithParameters(Map parameters) {
return new UpsertPersonSetting(
(CassandraAPI)parameters.get("api"),
(User)parameters.get("user"),
UUID.fromString((String)parameters.get("person")),
(String)parameters.get("name"),
(String)parameters.get("value"));
}
@Override
protected void execute() throws IOException {
Session s = c.getCassandraSession();
if(user == null || (!user.hasRole(Constants.PERSON_MANAGE_ROLE) && !user.getPersonUuid().equals(person))) {
log(s, "WARN", user, "Permission denied invoking: " + UpsertPersonSetting.class.getSimpleName() + " " + getJsonParameters());
throw new IllegalStateException("You do not have permission to manage settings.");
}
if(value != null) {
PreparedStatement p = s.prepare("insert into person_setting (uuid,name,value) values(?,?,?)");
s.execute(p.bind(person, name, value));
log(s, "INFO", user, "Updated person setting {\"uuid\":\"" + person + "\",\"name\":\"" + Json.escape(name) + "\",\"value\":\"" + Json.escape(value) + "\"}");
} else {
PreparedStatement p = s.prepare("delete from person_setting where uuid=? and name=?");
s.execute(p.bind(person, name));
log(s, "INFO", user, "Cleared person setting {\"uuid\":\"" + person + "\",\"name\":\"" + Json.escape(name) + "\"}");
}
}
@Override
public String getJsonParameters() {
return "{" +
"\"name\":\"" + Json.escape(name) + "\"," +
"\"value\":" + (value == null?"null":"\"" + Json.escape(value) + "\"") + "\"" +
"}";
}
}