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

base.jee.api.cassandra.util.AddGroup Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 1.5.4
Show newest version
/*
 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.util;

import base.json.Json;
import base.security.ResourceUid;
import base.security.User;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

import java.util.UUID;

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

public class AddGroup {

	public static UUID addGroup(Session s, String group, int type, User updator) {
		UUID uuid = new base.uuid.UUID().toJavaUUID();

		PreparedStatement p = s.prepare("select site, name from base_group where name = ?");
		ResultSet r = s.execute(p.bind(group));

		for(Row row : r) {
			if(row.getString(0).equals(updator.getSite())) {
				throw new IllegalArgumentException("A group with this name already exists.");
			}
		}

		p = s.prepare("insert into base_group (site, uuid, name, type) values(?,?,?,?)");
		s.execute(p.bind(updator.getSite(), uuid, group, type));
		log(s, "INFO", updator, "Added group \"" + Json.escape(group) + "\"", new ResourceUid("Person", updator.getPersonUuid().toString()), new ResourceUid("Group", uuid));
		return uuid;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy