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

base.jee.api.cassandra.UpdatePerson 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 java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.UUID;

import base.Command;
import base.email.EmailAddressParse;
import base.jee.Constants;
import base.json.Json;
import base.security.PermissionException;
import base.security.ResourceUid;
import base.security.User;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

import static base.jee.api.cassandra.util.Log.log;
import static base.text.StringHelper.chomp;

public class UpdatePerson extends Command {

	private CassandraAPI c;
	private UUID uuid;
	private User user;
	private String firstName;
	private String lastName;
	private String email;
	private Date expiry;

	public UpdatePerson() {
	}

	public UpdatePerson(CassandraAPI c, User user, UUID uuid, String firstName, String lastName, String email, String expiry) throws PermissionException {
		if(c == null) {
			throw new IllegalArgumentException("Invalid parameter: c");
		}
		if(firstName == null || chomp(firstName).length() == 0) {
			throw new IllegalArgumentException("Invalid parameter: firstName");
		}
		if(lastName == null || chomp(lastName).length() == 0) {
			throw new IllegalArgumentException("Invalid parameter: lastName");
		}
		if(email == null || chomp(email).length() == 0) {
			throw new IllegalArgumentException("Invalid parameter: email");
		}
		if(chomp(email).length() > Constants.MAX_EMAIL_LENGTH) {
			throw new IllegalArgumentException("Please choose a shorter email address. Email should not have more than " + Constants.MAX_EMAIL_LENGTH + " characters.");
		}
		EmailAddressParse parse = new EmailAddressParse();
		if(!parse.isValid(chomp(email).toLowerCase())) {
			throw new IllegalArgumentException("Invalid email address. " + parse.getError());
		}
		if(user == null || !user.isAuthenticated()) {
			throw new PermissionException(this.getClass().getSimpleName(), user, "Requires an authenticated user.", "");
		}
		if(uuid == null) {
			throw new IllegalArgumentException("Invalid parameter: person_uuid");
		}

		if(chomp(firstName).length() > Constants.MAX_FIRST_NAME_LENGTH) {
			throw new IllegalArgumentException("Please choose a shorter first name. First name should not have more than " + Constants.MAX_FIRST_NAME_LENGTH + " characters.");
		}
		if(chomp(lastName).length() > Constants.MAX_LAST_NAME_LENGTH) {
			throw new IllegalArgumentException("Please choose a shorter last name. Last name should not have more than " + Constants.MAX_FIRST_NAME_LENGTH + " characters.");
		}
		if(expiry != null && expiry.length() > 0) {
			try {
				this.expiry = Constants.DATE_FORMAT.parse(expiry);
			} catch (ParseException e1) {
				throw new IllegalArgumentException("Please leave expiry field empty, or enter date using date format: " + Constants.DATE_FORMAT_STRING);
			}
		}

		this.c = c;
		this.uuid = uuid;
		this.firstName = chomp(firstName);
		this.lastName = chomp(lastName);
		this.email = chomp(email).toLowerCase();
		this.user = user;
	}

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


		if(!user.hasRole(Constants.PERSON_MANAGE_ROLE)) {
			log(s, "WARN", user, "Permission denied invoking: " + UpdatePerson.class.getSimpleName() + " " + getJsonParameters());
			throw new IllegalStateException("You do not have permission to create new user accounts.");
		}

		PreparedStatement p = s.prepare("select uuid from person where email = ? limit 1");
		for(Row r : s.execute(p.bind(email))) {
			if(!r.getUUID(0).equals(uuid)) {
				throw new IllegalArgumentException("Person already exists with this email address.");
			}
		}

		PreparedStatement t = s.prepare("update person set first_name=?, last_name=?, email=?, updated=?, expiry=? where uuid=?");
		s.execute(t.bind(firstName, lastName, email, new Date().getTime(), expiry == null ? 0 : expiry.getTime(), uuid));

		log(s, "INFO", user, "Updated person " + firstName + " " + lastName, new ResourceUid("Person", uuid));
	}

	@Override
	public String getJsonParameters() {
		return "{" +
				"\"first_name\":\"" + Json.escape(firstName)+ "\"," +
				"\"last_name\":\"" + Json.escape(lastName)+ "\"," +
				"\"email\":\"" + Json.escape(email)+ "\"" +
				"}";
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy