base.jee.api.cassandra.Setup 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 java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;
import base.Command;
import base.security.PermissionException;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.AlreadyExistsException;
public class Setup extends Command {
private CassandraAPI c;
public Setup(CassandraAPI c) {
this.c = c;
if(c == null) {
throw new IllegalArgumentException("Invalid parameter: c");
}
}
@Override
protected void execute() throws IOException {
Session s = c.getCassandraSession();
runIfPossible(s, "create table audit_event (yyyymmdd text, uuid timeuuid, person_uuid timeuuid, first_name text, last_name text, level text, message text, ip text, primary key (yyyymmdd, uuid))");
runIfPossible(s, "create table audit_type (audit_event_uuid timeuuid primary key, resource_uid text, resource_type text)");
runIfPossible(s, "create table batch_load_info (uuid uuid, requestor uuid, executed bigint, primary key(uuid))");
runIfPossible(s, "create table batch_load_result (uuid uuid, sort_order int, value text, primary key(uuid, sort_order))");
runIfPossible(s, "create table base_group (site text, uuid uuid, name text, type int, primary key ((site, uuid)))");
runIfPossible(s, "create index base_group_index1 on base_group (name)");
runIfPossible(s, "create table group_member (person_uuid uuid, group_uuid uuid, joined bigint, primary key(person_uuid, group_uuid))");
runIfPossible(s, "create table ip_location (part int, start bigint, end bigint, location bigint, primary key (part, start, end)) with clustering order by (start desc);\n");
runIfPossible(s, "create table location (id bigint, country text, city text, latitude double, longitude double, primary key(id))");
runIfPossible(s, "create table person (site text, uuid timeuuid, first_name text, last_name text, email text, username text, password text, last_auth bigint, last_auth_ip text, created bigint, updated bigint, expiry bigint, primary key ((site), uuid))");
runIfPossible(s, "create index person_index1 on person (email)");
runIfPossible(s, "create index person_index2 on person (username)");
runIfPossible(s, "create table person_setting (uuid uuid, name text, value text, primary key(uuid, name))");
runIfPossible(s, "create table session_token (site text, uid text, person_uuid timeuuid, first_name text, last_name text, expiry bigint, roles text, created bigint, primary key ((site, uid)))");
runIfPossible(s, "create index session_token_index1 on session_token (person_uuid)");
runIfPossible(s, "create table request_token (uid text primary key, site text, person_uuid timeuuid, expiry bigint, type text, ip text, data text)");
runIfPossible(s, "create table role (role text, resource text, uid text, person_uuid timeuuid, primary key(role, person_uuid, resource, uid))");
runIfPossible(s, "create table setting (name text primary key, value text)");
runIfPossible(s, "create table template (site text, name text, primary key ((site), name))");
runIfPossible(s, "create table template_resource (site text, template text, resource text, content blob, isbinary boolean, primary key ((site, template), resource))");
runIfPossible(s, "create table throttle_lock(uid text primary key)");
runIfPossible(s, "create table throttle_counter(uid text, time timeuuid, primary key (uid, time))");
runIfPossible(s, "create table update_notification (type text, updated bigint, uuid uuid, updator uuid, action text, uid text, delivered boolean, primary key(type, updated, uuid))");
Connection c = null;
PreparedStatement p = null;
try {
c = this.c.getDataSource().getConnection();
p = c.prepareStatement("create table email (uuid text primary key, to_address text, email text, retries bigint, attempt_at bigint, in_progress boolean)");
p.execute();
} catch(SQLException e) {
} finally {
if(p != null) { try { p.close(); } catch(SQLException e) {} }
if(c != null) { try { c.close(); } catch(SQLException e) {} }
}
}
private void runIfPossible(Session s, String sql) {
try {
s.execute(sql);
} catch(AlreadyExistsException e) {
// Do nothing
} catch(Exception e) {
if(!e.getMessage().contains("Index already exists")) {
throw e;
}
}
}
@Override
public String getJsonParameters() {
return "{" +
"}";
}
@Override
public Command newWithParameters(Map parameters) throws IOException, PermissionException {
throw new IllegalArgumentException("Setup may not be instantiated with parameter map");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy