001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.optional.starteam;
019:
020: import com.starbase.starteam.Label;
021: import com.starbase.starteam.View;
022: import com.starbase.util.OLEDate;
023: import java.text.ParseException;
024: import java.text.SimpleDateFormat;
025: import java.util.Date;
026: import org.apache.tools.ant.BuildException;
027:
028: /**
029: * Creates a view label in StarTeam at the specified view.
030: *
031: * Ant Usage:
032: * <pre>
033: * <taskdef name="stlabel"
034: * classname="org.apache.tools.ant.taskdefs.optional.starteam.StarTeamLabel"/<
035: * <stlabel
036: * label="1.0" lastbuild="20011514100000" description="Successful Build"
037: * username="BuildMaster" password="ant"
038: * starteamurl="server:port/project/view"/>
039: * </pre>
040: *
041: * @see <a href="http://www.borland.com/us/products/starteam/index.html"
042: * >borland StarTeam Web Site</a>
043: *
044: * @ant.task name="stlabel" category="scm"
045: */
046: public class StarTeamLabel extends StarTeamTask {
047:
048: /**
049: * The name of the label to be set in Starteam.
050: */
051: private String labelName;
052:
053: /**
054: * The label description to be set in Starteam.
055: */
056: private String description;
057:
058: /**
059: * If true, this will be a build label. If false, it will be a non-build
060: * label. The default is false. Has no effect if revision label is
061: * true.
062: */
063: private boolean buildlabel = false;
064:
065: /**
066: * If true, this will be a revision label. If false, it will be a build
067: * label. The default is false.
068: */
069: private boolean revisionlabel = false;
070:
071: /**
072: * The time of the last successful. The new label will be a snapshot of the
073: * repository at this time. String should be formatted as "yyyyMMddHHmmss"
074: */
075: private OLEDate lastBuild = null;
076:
077: private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(
078: "yyyyMMddHHmmss");
079:
080: /**
081: * The name to be given to the label; required.
082: * @param label the name to be used
083: */
084: public void setLabel(String label) {
085: this .labelName = label;
086: }
087:
088: /**
089: * Description of the label to be stored in the StarTeam project.
090: * @param description the description to be used
091: */
092: public void setDescription(String description) {
093: this .description = description;
094: }
095:
096: /**
097: * set the type of label based on the supplied value - if true, this
098: * label will be a revision label, if false, a build label.
099: *
100: * @param buildlabel If true this will be a revision label; if false,
101: * a build label
102: */
103: public void setBuildLabel(boolean buildlabel) {
104: this .buildlabel = buildlabel;
105: }
106:
107: /**
108: * set the type of label based on the supplied value - if true, this
109: * label will be a revision label, if false, a build label.
110: *
111: * @param revisionlabel If true this will be a revision label; if false,
112: * a build label
113: */
114: public void setRevisionLabel(boolean revisionlabel) {
115: this .revisionlabel = revisionlabel;
116: }
117:
118: /**
119: * The timestamp of the build that will be stored with the label; required.
120: * Must be formatted <code>yyyyMMddHHmmss</code>
121: * @param lastbuild the timestamp of the last build
122: * @throws BuildException on error
123: */
124: public void setLastBuild(String lastbuild) throws BuildException {
125: try {
126: Date lastBuildTime = DATE_FORMAT.parse(lastbuild);
127: this .lastBuild = new OLEDate(lastBuildTime);
128: } catch (ParseException e) {
129: throw new BuildException("Unable to parse the date '"
130: + lastbuild + "'", e);
131: }
132: }
133:
134: /**
135: * This method does the work of creating the new view and checking it into
136: * Starteam.
137: * @throws BuildException on error
138: */
139: public void execute() throws BuildException {
140:
141: if (this .revisionlabel && this .buildlabel) {
142: throw new BuildException(
143: "'revisionlabel' and 'buildlabel' "
144: + "both specified. A revision label cannot be a build label.");
145: }
146:
147: try {
148: View snapshot = openView();
149:
150: // Create the new label and update the repository
151:
152: if (this .revisionlabel) {
153: new Label(snapshot, this .labelName, this .description)
154: .update();
155: log("Created Revision Label " + this .labelName);
156: } else if (null != lastBuild) {
157: new Label(snapshot, this .labelName, this .description,
158: this .lastBuild, this .buildlabel).update();
159: log("Created View Label ("
160: + (this .buildlabel ? "" : "non-") + "build) "
161: + this .labelName + " as of "
162: + this .lastBuild.toString());
163: } else {
164: new Label(snapshot, this .labelName, this .description,
165: this .buildlabel).update();
166: log("Created View Label ("
167: + (this .buildlabel ? "" : "non-") + "build) "
168: + this .labelName);
169: }
170: } catch (Exception e) {
171: throw new BuildException(e);
172: } finally {
173: disconnectFromServer();
174: }
175:
176: }
177:
178: /**
179: * Override of base-class abstract function creates an
180: * appropriately configured view. For labels this a view
181: * configured as of this.lastBuild.
182: *
183: * @param raw the unconfigured <code>View</code>
184: * @return the snapshot <code>View</code> appropriately configured.
185: */
186: protected View createSnapshotView(View raw) {
187: /*
188: if (this.revisionlabel) {
189: return raw;
190: }
191: return new View(raw, ViewConfiguration.createFromTime(this.lastBuild));
192: */
193: return raw;
194: }
195:
196: }
|