summaryrefslogtreecommitdiff
path: root/src/main/org/apache/tools/ant/taskdefs/optional/starteam/StarTeamLabel.java
blob: 1b86e596e8cd734ebea595608a7957a171d14537 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* 
 * Copyright  2001-2004 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.View;
import com.starbase.util.OLEDate;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.tools.ant.BuildException;

/**
 * Creates a view label in StarTeam at the specified view.
 *
 * Ant Usage:
 * <pre>
 * &lt;taskdef name="stlabel"
 *          classname="org.apache.tools.ant.taskdefs.optional.starteam.StarTeamLabel"/&lt;
 *     &lt;stlabel
 * label="1.0" lastbuild="20011514100000" description="Successful Build"
 * username="BuildMaster" password="ant"
 * starteamurl="server:port/project/view"/&gt;
 * </pre>
 *
 * @author Christopher Charlier, ThoughtWorks, Inc. 2001
 * @author <a href="mailto:jcyip@thoughtworks.com">Jason Yip</a>
 * @author <a href="mailto:scohen@apache.org">Steve Cohen</a>
 * @see <A HREF="http://www.starbase.com/">StarBase Web Site</A>
 *
 * @ant.task name="stlabel" category="scm"
 */
public class StarTeamLabel extends StarTeamTask {

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

    /**
     * The label description to be set in Starteam.
     */
    private String description;

    /**
     * If true, this will be a build label.  If false, it will be a non-build
     * label.  The default is false.  Has no effect if revision label is
     * true.
     */
    private boolean buildlabel = false;

    /**
     * If true, this will be a revision label.  If false, it will be a build
     * label.  The default is false.
     */
    private boolean revisionlabel = false;

    /**
     * The time of the last successful. The new label will be a snapshot of the
     * repository at this time. String should be formatted as "yyyyMMddHHmmss"
     */
    private OLEDate lastBuild = null;

    private static final SimpleDateFormat DATE_FORMAT =
            new SimpleDateFormat("yyyyMMddHHmmss");


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

    /**
     * Description of the label to be stored in the StarTeam project.
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * set the type of label based on the supplied value - if true, this
     * label will be a revision label, if false, a build label.
     *
     * @param buildlabel If true this will be a revision label; if false,
     * a build label
     */
    public void setBuildLabel(boolean buildlabel) {
        this.buildlabel = buildlabel;
    }

    /**
     * set the type of label based on the supplied value - if true, this
     * label will be a revision label, if false, a build label.
     *
     * @param revisionlabel If true this will be a revision label; if false,
     * a build label
     */
    public void setRevisionLabel(boolean revisionlabel) {
        this.revisionlabel = revisionlabel;
    }



    /**
     * The timestamp of the build that will be stored with the label; required.
     * Must be formatted <code>yyyyMMddHHmmss</code>
     */
    public void setLastBuild(String lastbuild) throws BuildException {
        try {
            Date lastBuildTime = DATE_FORMAT.parse(lastbuild);
            this.lastBuild = new OLEDate(lastBuildTime);
        } catch (ParseException e) {
            throw new BuildException("Unable to parse the date '"
                + lastbuild + "'", e);
        }
    }

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

        if (this.revisionlabel && this.buildlabel) {
            throw new BuildException("'revisionlabel' and 'buildlabel' "
                + "both specified.  A revision label cannot be a build label.");
        }

        try {
            View snapshot = openView();

            // Create the new label and update the repository

            if (this.revisionlabel) {
                new Label(snapshot, this.labelName, this.description).update();
                log("Created Revision Label " + this.labelName);
            } else if (null != lastBuild) {
                new Label(snapshot, this.labelName, this.description, this.lastBuild,
                          this.buildlabel).update();
                log("Created View Label ("
                    + (this.buildlabel ? "" : "non-") + "build) " + this.labelName
                    + " as of " + this.lastBuild.toString());
            } else {
                new Label(snapshot, this.labelName, this.description,
                          this.buildlabel).update();
                log("Created View Label ("
                    + (this.buildlabel ? "" : "non-") + "build) " + this.labelName);
            }
        } 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) {
        /*
        if (this.revisionlabel) {
            return raw;
        }
        return new View(raw, ViewConfiguration.createFromTime(this.lastBuild));
        */
        return raw;
    }

}