/*
 * Copyright  2001-2004 The Apache Software Foundation
 *
 *  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.apache.tools.ant.taskdefs.optional.starteam;

import com.starbase.starteam.Label;
import com.starbase.starteam.PromotionState;
import com.starbase.starteam.View;
import org.apache.tools.ant.BuildException;

/**
 * Updates a Promotion State in StarTeam at the specified view.
 *
 * Ant Usage:
 * <pre>
 * &lt;taskdef name="stpromote"
 *          classname="org.apache.tools.ant.taskdefs.optional.starteam.StarTeamPromote"/&lt;
 *     &lt;stpromote
 * label="1.0" promotionstate="DevUnitTesting"
 * username="BuildMaster" password="ant"
 * starteamurl="server:port/project/view"/&gt;
 * </pre>
 *
 * @see <A HREF="http://www.borland.com/">Borland Web Site</A>
 *
 * @ant.task name="stpromote" category="scm"
 */
public class StarTeamPromote extends StarTeamTask {

    /**
     * The name of the label to be set in Starteam.
     */
    private String labelName;

    /**
     * The promotion state to be set in Starteam.
     */
	private String promotionState;

    /**
    * The name of the label to be used; required.
    */
    public void setLabel(String label) {
        this.labelName = label;
    }

    /**
     * The name of the promotion state to be updated; required.
     */
     public void setPromotionState(String promotionState) {
         this.promotionState = promotionState;
     }

    /**
     * This method does the work of creating the new view and checking it into
     * Starteam.
     *
     */
    public void execute() throws BuildException {

        try {
            View v = openView();
            
            PromotionState ps = findPromotionState(v, promotionState);
            Label stLabel = findLabel(v, this.labelName);
            
            if (!stLabel.isBuildLabel())
            {
            	throw new BuildException("Cannot set a promotion state to a non-build label.");
            }
            
            ps.setLabelID(stLabel.getID());
            v.update();
            v.close(); //Free up server resources
        } catch (Exception e) {
            throw new BuildException(e);
        } finally {
            disconnectFromServer();
        }

    }

    /**
     * Override of base-class abstract function creates an
     * appropriately configured view.  For labels this a view
     * configured as of this.lastBuild.
     *
     * @param raw the unconfigured <code>View</code>
     * @return the snapshot <code>View</code> appropriately configured.
     */
    protected View createSnapshotView(View raw) {
        return raw;
    }

}

