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

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

import base.Query;
import base.jee.Constants;
import base.security.Group;
import base.security.PermissionException;
import base.security.User;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class GetGroup extends Query {

	private CassandraAPI api;
	private User user;
	private UUID uuid;

	public GetGroup() {
	}

	public GetGroup(CassandraAPI api, User user, UUID uuid) throws PermissionException {

		if(api == null) {
			throw new IllegalArgumentException("Invalid parameter: api");
		}
		if(uuid == null) {
			throw new IllegalArgumentException("Invalid parameter: uuid");
		}
		if(user == null || !user.isAuthenticated()) {
			throw new PermissionException(getClass().getSimpleName(), user, "Requires authenticated user.", Constants.PERSON_MANAGE_ROLE);
		}

		this.api = api;
		this.user = user;
		this.uuid = uuid;
	}

	@Override
	public Query newWithParameters(Map parameters) throws PermissionException {
		return new GetGroup(
				(CassandraAPI)parameters.get("api"),
				(User)parameters.get("user"),
				UUID.fromString((String)parameters.get("uuid")));
	}

	public List execute() throws IOException {
		List results = new LinkedList<>();
		Session s = api.getCassandraSession();

		for(Row r : s.execute(s.prepare("select name, type from base_group where uuid=?").bind(uuid))) {
			Group g = new Group(uuid, r.getString(0), r.getInt(1));
			results.add(g);
			return results;
		}

		return results;
	}

	@Override
	public String getJsonParameters() {
		return "{" +
				"\"person\":\"" + user.getPersonUuid() + "\"," +
				"\"uuid\":\"" + uuid + "\"" +
				"}";
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy