1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.optional.starteam;
  18. import com.starbase.starteam.Label;
  19. import com.starbase.starteam.View;
  20. import com.starbase.util.OLEDate;
  21. import java.text.ParseException;
  22. import java.text.SimpleDateFormat;
  23. import java.util.Date;
  24. import org.apache.tools.ant.BuildException;
  25. /**
  26. * Creates a view label in StarTeam at the specified view.
  27. *
  28. * Ant Usage:
  29. * <pre>
  30. * <taskdef name="stlabel"
  31. * classname="org.apache.tools.ant.taskdefs.optional.starteam.StarTeamLabel"/<
  32. * <stlabel
  33. * label="1.0" lastbuild="20011514100000" description="Successful Build"
  34. * username="BuildMaster" password="ant"
  35. * starteamurl="server:port/project/view"/>
  36. * </pre>
  37. *
  38. * @see <A HREF="http://www.starbase.com/">StarBase Web Site</A>
  39. *
  40. * @ant.task name="stlabel" category="scm"
  41. */
  42. public class StarTeamLabel extends StarTeamTask {
  43. /**
  44. * The name of the label to be set in Starteam.
  45. */
  46. private String labelName;
  47. /**
  48. * The label description to be set in Starteam.
  49. */
  50. private String description;
  51. /**
  52. * If true, this will be a build label. If false, it will be a non-build
  53. * label. The default is false. Has no effect if revision label is
  54. * true.
  55. */
  56. private boolean buildlabel = false;
  57. /**
  58. * If true, this will be a revision label. If false, it will be a build
  59. * label. The default is false.
  60. */
  61. private boolean revisionlabel = false;
  62. /**
  63. * The time of the last successful. The new label will be a snapshot of the
  64. * repository at this time. String should be formatted as "yyyyMMddHHmmss"
  65. */
  66. private OLEDate lastBuild = null;
  67. private static final SimpleDateFormat DATE_FORMAT =
  68. new SimpleDateFormat("yyyyMMddHHmmss");
  69. /**
  70. * The name to be given to the label; required.
  71. */
  72. public void setLabel(String label) {
  73. this.labelName = label;
  74. }
  75. /**
  76. * Description of the label to be stored in the StarTeam project.
  77. */
  78. public void setDescription(String description) {
  79. this.description = description;
  80. }
  81. /**
  82. * set the type of label based on the supplied value - if true, this
  83. * label will be a revision label, if false, a build label.
  84. *
  85. * @param buildlabel If true this will be a revision label; if false,
  86. * a build label
  87. */
  88. public void setBuildLabel(boolean buildlabel) {
  89. this.buildlabel = buildlabel;
  90. }
  91. /**
  92. * set the type of label based on the supplied value - if true, this
  93. * label will be a revision label, if false, a build label.
  94. *
  95. * @param revisionlabel If true this will be a revision label; if false,
  96. * a build label
  97. */
  98. public void setRevisionLabel(boolean revisionlabel) {
  99. this.revisionlabel = revisionlabel;
  100. }
  101. /**
  102. * The timestamp of the build that will be stored with the label; required.
  103. * Must be formatted <code>yyyyMMddHHmmss</code>
  104. */
  105. public void setLastBuild(String lastbuild) throws BuildException {
  106. try {
  107. Date lastBuildTime = DATE_FORMAT.parse(lastbuild);
  108. this.lastBuild = new OLEDate(lastBuildTime);
  109. } catch (ParseException e) {
  110. throw new BuildException("Unable to parse the date '"
  111. + lastbuild + "'", e);
  112. }
  113. }
  114. /**
  115. * This method does the work of creating the new view and checking it into
  116. * Starteam.
  117. *
  118. */
  119. public void execute() throws BuildException {
  120. if (this.revisionlabel && this.buildlabel) {
  121. throw new BuildException("'revisionlabel' and 'buildlabel' "
  122. + "both specified. A revision label cannot be a build label.");
  123. }
  124. try {
  125. View snapshot = openView();
  126. // Create the new label and update the repository
  127. if (this.revisionlabel) {
  128. new Label(snapshot, this.labelName, this.description).update();
  129. log("Created Revision Label " + this.labelName);
  130. } else if (null != lastBuild) {
  131. new Label(snapshot, this.labelName, this.description, this.lastBuild,
  132. this.buildlabel).update();
  133. log("Created View Label ("
  134. + (this.buildlabel ? "" : "non-") + "build) " + this.labelName
  135. + " as of " + this.lastBuild.toString());
  136. } else {
  137. new Label(snapshot, this.labelName, this.description,
  138. this.buildlabel).update();
  139. log("Created View Label ("
  140. + (this.buildlabel ? "" : "non-") + "build) " + this.labelName);
  141. }
  142. } catch (Exception e) {
  143. throw new BuildException(e);
  144. } finally {
  145. disconnectFromServer();
  146. }
  147. }
  148. /**
  149. * Override of base-class abstract function creates an
  150. * appropriately configured view. For labels this a view
  151. * configured as of this.lastBuild.
  152. *
  153. * @param raw the unconfigured <code>View</code>
  154. * @return the snapshot <code>View</code> appropriately configured.
  155. */
  156. protected View createSnapshotView(View raw) {
  157. /*
  158. if (this.revisionlabel) {
  159. return raw;
  160. }
  161. return new View(raw, ViewConfiguration.createFromTime(this.lastBuild));
  162. */
  163. return raw;
  164. }
  165. }