/*******************************************************************************
 * Copyright 2012 Apigee Corporation
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.usergrid.management;

import static org.apache.commons.lang.StringUtils.isNotBlank;
import static org.usergrid.persistence.Schema.PROPERTY_ACTIVATED;
import static org.usergrid.persistence.Schema.PROPERTY_DISABLED;
import static org.usergrid.persistence.Schema.PROPERTY_EMAIL;
import static org.usergrid.persistence.Schema.PROPERTY_NAME;
import static org.usergrid.persistence.Schema.PROPERTY_USERNAME;
import static org.usergrid.persistence.Schema.PROPERTY_UUID;
import static org.usergrid.persistence.cassandra.CassandraService.MANAGEMENT_APPLICATION_ID;
import static org.usergrid.utils.ConversionUtils.getBoolean;
import static org.usergrid.utils.ConversionUtils.string;
import static org.usergrid.utils.ConversionUtils.uuid;

import java.util.Map;
import java.util.UUID;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class UserInfo {

	private final UUID applicationId;
	private final UUID id;
	private final String username;
	private final String name;
	private final String email;
	private final boolean activated;
	private final boolean disabled;

	public UserInfo(UUID applicationId, UUID id, String username, String name,
			String email, boolean activated, boolean disabled) {
		this.applicationId = applicationId;
		this.id = id;
		this.username = username;
		this.name = name;
		this.email = email;
		this.activated = activated;
		this.disabled = disabled;
	}

	public UserInfo(UUID applicationId, Map<String, Object> properties) {
		this.applicationId = applicationId;
		id = uuid(properties.get(PROPERTY_UUID));
		username = string(properties.get(PROPERTY_USERNAME));
		name = string(properties.get(PROPERTY_NAME));
		email = string(properties.get(PROPERTY_EMAIL));
		activated = getBoolean(properties.get(PROPERTY_ACTIVATED));
		disabled = getBoolean(properties.get(PROPERTY_DISABLED));
	}

	public UUID getApplicationId() {
		return applicationId;
	}

	public UUID getUuid() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getUsername() {
		return username;
	}

	public String getEmail() {
		return email;
	}

	@Override
	public String toString() {
		return id + "/" + name + "/" + email;
	}

	public String getDisplayEmailAddress() {
		if (isNotBlank(name)) {
			return name + " <" + email + ">";
		}
		return email;
	}

	public String getHTMLDisplayEmailAddress() {
		if (isNotBlank(name)) {
			return name + " <a href=\"mailto:" + email + "\">" + email + "</a>";
		}
		return email;
	}

	public boolean isActivated() {
		return activated;
	}

	public boolean isDisabled() {
		return disabled;
	}

	public boolean isAdminUser() {
		return MANAGEMENT_APPLICATION_ID.equals(applicationId);
	}

}

@@ -3153,16 +3153,20 @@
 ame + %22 
+&lt;
 %3Ca href=
@@ -3203,16 +3203,25 @@
 mail
- 
+%0A%09%09%09%09%09
 + %22%3C/a%3E
+&gt;
 %22;%0A%09

fix email address formatting