/* ************************************************************************
#
#  DivConq
#
#  http://divconq.com/
#
#  Copyright:
#    Copyright 2012 eTimeline, LLC. All rights reserved.
#
#  License:
#    See the license.txt file in the project's top-level directory for details.
#
#  Authors:
#    * Andy White
#
************************************************************************ */
package divconq.db.common;

import org.joda.time.DateTime;

import divconq.db.IDatabaseManager;
import divconq.db.UpdateRequest;
import divconq.struct.FieldStruct;
import divconq.struct.RecordStruct;
import divconq.util.StringUtil;

/**
 * Start sign on recovery for "lost passwords".  A recovery code is automatically generated,
 * so "getCode" for value.  Or you may optionally create your own code, see "setCode".
 * This code should be emailed to the user so they can use the code + username to login
 * instead of their password.
 * 
 * @author Andy
 *
 */
public class InitiateRecoveryRequest extends UpdateRequest {
	protected String name = null;
	protected String code = StringUtil.buildSecurityCode();
	
	/**
	 * @param v user name used in query
	 */
	public void setUserName(String v) {
		this.name = v;
	}

	/**
	 * @return recovery code for user
	 */
	public String getCode() {
		return this.code;
	}
	
	/**
	 * @param v recovery code entered by user
	 */
	public void setCode(String v) {
		this.code = v;
	}
	
	/**
	 * Initiate sign on recovery.
	 * 
	 * @param username entered by user
	 */
	public InitiateRecoveryRequest(String username) {
		super("dcInitiateRecovery");
		this.name = username;
	}
	
	/**
	 * Initiate sign on recovery, provide a custom recovery code instead
	 * of using the automatically generated one.
	 * 
	 * @param username entered by user
	 * @param code confirm code entered by user
	 */
	public InitiateRecoveryRequest(String username, String code) {
		super("dcInitiateRecovery");
		this.name = username;
		this.code = code;
	}
	
	/* (non-Javadoc)
	 * @see divconq.db.IDatabaseRequest#getDatabaseParams()
	 */
	@Override
	public RecordStruct getDatabaseParams(IDatabaseManager dbm) {
		return new RecordStruct(
				new FieldStruct("UserName", (this.name != null) ? this.name.trim().toLowerCase() : null),
				new FieldStruct("At", new DateTime()),
				new FieldStruct("Code", this.code)
		);
	}
}
