
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.taskdefs.optional.clearcase.*;

/**
 * <code>CCLabelType</code> performs the Clearcase label
 * type operation
 *
 * <p>
 * The following attributes are interpreted:
 * <table border="1">
 *   <tr>
 *     <th>Attribute</th>
 *     <th>Values</th>
 *     <th>Required</th>
 *   </tr>
 *   <tr>
 *      <td>viewpath</td>
 *      <td>Path to the Clearcase view file or directory that the command will
 *      operate on</td>
 *      <td>Yes</td>
 *   </tr>
 *   <tr>
 *       <td>labelComment</td>
 *       <td>Specifies the label comment</td>
 *       <td>Yes</td>
 *    </tr>    
 *    <tr>
 *       <td>labelName</td>
 *       <td>Specifies the label name</td>
 *       <td>Yes</td>
 *     </tr>
 *     <tr>
 *        <td>directory</td>
 *        <td>Specifies the file or the branch for labeling</td>
 *        <td>Yes</td>
 *     </tr>   
 *    </table> 
 *    <h3>Examples</h3>
 *    <blockquote>
 *     <pre>
 *        &lt;cclabel viewpath="c:/views/viewdir"
 *                 labelComment="Comment"
 *                 labelName="labelname"
 *                 directory="s.txt"/&gt;
 *     </pre>            
 *     </blockquote>            
 *     Does a Clearcase label on the file c:/views/viewdir. The label name and
 *     the comment is added for the directory.
 *     
 */

public class CCLabel extends ClearCase {

   /**
    * Variable to store the label name
    */ 
   private String labelName;
   
   /**
    * Variable to store the label comments
    */ 
   private String comments;
   
   /**
    * Variable to store the directory for labeling
    */ 
   private String directory;

   /**
    * The 'mklabel' command
    */
   private static final String COMMAND_LABEL="mklabel";

   /**
    * The 'mklbtype' command
    */
   private static final String COMMAND_LABEL_TYPE="mklbtype";
      
   /**
    * -c flag -- comments for the label
    */
   private static final String COMMENT="-c";
    
   /**
    * @vob: -- Accessing the vobs
    */
   private static final String VOB="@vob:";
       
   /**
    * -rep -- Replace the existing label
    */
   private static final String REPLACE="-rep";
   
   /**
    * -rec flag -- for recursive
    */
   private static final String RECURSE="-rec";
   
   /**
    * lstype -- for finding the label types
    */ 
   private static final String findLabelTypes ="lstype -kind lbtype";
    
   /**
    * Executes the task
    * <p>
    * Builds aa command line to execute cleartool and then calls
    * Exec's run method to execute the command line.
    */ 
    public void execute() throws BuildException {
        Commandline commandLine = new Commandline();
        Project aProj = getProject();
        int result=0;
        
        //Default the viewpath to basedir if it is not specified 
        if (getViewPath() == null) {
            setViewPath(aProj.getBaseDir().getPath());
        }
        commandLine.setExecutable(getClearToolCommand());
        commandLine.createArgument().setValue(COMMAND_LABEL_TYPE);
        formatLabelTypeOptions(commandLine,false);
        // Make label Type command
        result = run(commandLine);
        if(result != 0) {
            commandLine.clearArgs();
            commandLine.createArgument().setValue(COMMAND_LABEL_TYPE);
            formatLabelTypeOptions(commandLine,true);   
            // Make label Type command
            result = run(commandLine);
            if (result !=0) {
                String msg = "Failed executing: "+commandLine.toString();
                throw new BuildException(msg,location);
            }
        }
        //Clear the command line arguments
        commandLine.clearArgs();
        commandLine.createArgument().setValue(COMMAND_LABEL);
        formatLabelOptions(commandLine);
        // Make label Type command
        result = run(commandLine);
        if(result != 0) {
            String msg = "Failed executing: "+commandLine.toString();
            throw new BuildException(msg,location);
        }
    }
    /**
     * Set label name
     *
     * @param labelName
     */ 
    public void setLabelType(String labelName) {
        this.labelName = labelName;
    }
    /**
     * Get Label name
     *
     * @return String containing the label name
     */ 
    public String getLabelType(){
        return labelName;
    }
    
    /**
     * Set comment file
     *
     * @param comment 
     */ 
    public void setLabelTypeComment(String comment) {
        this.comments = comment;
    }
    
    /**
     * Get comment string
     *
     * @return String containing the comment
     */ 
    public String getLabelTypeComment() {
        return comments;
    }    
    
    /**
     * Set label name
     *
     * @param labelName
     */ 
    public void setLabelName(String labelName) {
        this.labelName = labelName;
    }
    /**
     * Get Label name
     *
     * @return String containing the label name
     */ 
    public String getLabelName(){
        return labelName;
    }
    
    /**
     * Set comment file
     *
     * @param comment 
     */ 
    public void setLabelComment(String comment) {
        this.comments = comment;
    }
    
    /**
     * Get comment string
     *
     * @return String containing the comment
     */ 
    public String getLabelComment() {
        return comments;
    }    
    /**
     * Set directory
     *
     * @param directory
     */ 
    public void setDirectory(String directory){
        this.directory = directory;
    }
    
    /**
     * Get the directory
     *
     * @return String containing the directory
     */ 
    public String getDirectory(){
        return directory;
    }
    /**
     * Format the command line options for the label type
     * command
     * 
     */ 
    private void formatLabelTypeOptions(Commandline cmd,boolean flag){
        if(flag == true) {
            cmd.createArgument().setValue(REPLACE);
        }
        cmd.createArgument().setValue(COMMENT);
        
        cmd.createArgument().setValue(getLabelComment());
        // viewpath
        String viewLocation = getLabelName()+VOB+getViewPath();
        cmd.createArgument().setValue(viewLocation);
    }
    /**
     * Format the command line options for the label command
     *
     */ 
    private void formatLabelOptions(Commandline cmd){
        cmd.createArgument().setValue(REPLACE);
        cmd.createArgument().setValue(RECURSE);
        cmd.createArgument().setValue(COMMENT);
        cmd.createArgument().setValue(getLabelComment());
        cmd.createArgument().setValue(getLabelName());
        String dir = getViewPath()+"\\"+getDirectory();
        System.out.println(dir);
        cmd.createArgument().setValue(dir);
    }
    
    /**
     * Method to find the label types
     */ 
    private void findLabelTypes(Commandline cmd) {
       cmd.createArgument().setValue(findLabelTypes);
    }    

    
}
