package org.apache.tools.ant.taskdefs.optional.starteam;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import com.starbase.starteam.Server;
import com.starbase.starteam.ServerException;

/**
 * Title: StarTeam Encrypted File Log On
 * Description: This module utilities the EncryptPassword class from the Borland SDK Utilities
 * which can be obtained from <url>http://support.borland.com/community/index.php?t=getfile&id=177&</url>.
 * This class extents the org.apache.tools.ant.Task to create the Encrypted Password file which is used 
 * by the class when it's used to implement the StarTeamLogOnProvider interface.
 * 
 * As an ANT Task example:
 *  
 * <stencryptpassword " 
 *  username="<valid starteam username>"
 *  password="<password for starteam user>"
 *  filename="<fully qualified path to the file to store the password>"
 * />
 *  
 *  Implentation for StarTeamLogOnProvider example:
 *  
 * <stcheckout URL="myserver:49201/myproject" 
 *  username="<valid starteam username used to encrypt the file>"
 *  passwordfile="<fully qualified path to the file to store the password>"
 *  logonprovider="org.apache.tools.ant.taskdefs.optional.starteam.StarTeamEncryptLogOn"
 * />
 *  
 * @author John Ballment
 * @ant.task name="stencryptpassword" category="scm" product="Starteam"
 */
public class StarTeamEncryptLogOn extends Task implements
		StarTeamLogOnProvider {

	private String password;
	
	private String username;
	
	private String filename;
	
	/**
	 * Setter for the password value to be encrypted into the file.
	 * @param password
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * Setter for the username value to be used to encrypt the password to file.
	 * @param username
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * Setter for fully qualified path/filename for the file that the username/password combination to be saved to.
	 * @param filename
	 */
	public void setFilename(String filename) {
		this.filename = filename;
	}

	/**
	 * This task stores the username/password combination to a file which will be used by StarTeam Tasks 
	 * as a source for of an encrypted password. 
	 * @see org.apache.tools.ant.Task#execute()
	 */
	public final void execute() throws BuildException {
		try {
			borlandutilities.EncryptPassword encryptPwd = new borlandutilities.EncryptPassword();
			
			encryptPwd.encrypt(username, password, filename);
		} catch (Exception e) {
			throw new BuildException(e);
		}
	}
	
	/**
	 * The LogOn method implements the StarTeamLogOnProvider interface for retrieving a password from an
	 * encrypted password file.  The password file needs to be created either by the stencryptpassword ANT Task or
	 * directly by the Borland SDK Utilities EncryptPassword package.
	 * 
	 * @param task : StarTeam parent Task that is requesting an encrypted log on
	 * @param server : the server instance that has been configured by the StarTeam parent Task and is ready to be
	 *                 logged into.
	 * @param task->username : the StarTeam username has to be a defined parameter of the parent task object.
	 * @param task->passwordfile : the location for the encrypted password file that is a defined parameter of the parent task object.
	 * @output task->password : the parent's task password will be updated with the decrypted password value from the password file.  
	 * @see org.apache.tools.ant.taskdefs.optional.starteam.StarTeamLogOnProvider#LogOn(org.apache.tools.ant.taskdefs.optional.starteam.StarTeamTask, com.starbase.starteam.Server)
	 */
	public void LogOn(StarTeamTask task, Server server) throws BuildException {
		this.username = task.getUserName();
		this.filename = task.getPasswordFile();

    	borlandutilities.EncryptPassword encryptPwd = new borlandutilities.EncryptPassword();

		try {
			this.password = encryptPwd.decrypt(this.username, this.filename);
		} catch (Exception e) {
			throw new BuildException(e);
		}
		task.setPassword(password);
		try {
			server.logOn(task.getUserName(), task.getPassword());
		} catch (ServerException e) {
			throw new BuildException("Unable to log onto server because " + e.getErrorMessage());
		}
	}
	
}
