001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2003, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol;
037:
038: import java.io.File;
039: import java.text.ParseException;
040: import java.util.Date;
041: import java.util.Locale;
042:
043: import net.sourceforge.cruisecontrol.util.DateHelper;
044:
045: /**
046: * @author Jared Richardson User: jfredrick Adapted from StatusPage.java,
047: * submitted by Jared to the cruisecontrol-devel mailing list.
048: * @author <a href="mailto:hak@2mba.dk">Hack Kampbjorn </a>
049: * @author <a href="mailto:jeffjensen@upstairstechnology.com">Jeff Jensen </a>
050: */
051: public class StatusHelper {
052: private BuildInfo newestBuild;
053: private BuildInfo newestSuccessfulBuild;
054:
055: public static final String PASSED = "passed";
056: public static final String FAILED = "failed";
057:
058: public void setProjectDirectory(File directory) {
059: LogFile newestLogfile = LogFile.getLatestLogFile(directory);
060: if (newestLogfile == null) {
061: newestBuild = null;
062: } else {
063: try {
064: newestBuild = new BuildInfo(newestLogfile);
065: } catch (ParseException pe) {
066: newestBuild = null;
067: }
068: }
069: LogFile newestSuccessfulLogfile = LogFile
070: .getLatestSuccessfulLogFile(directory);
071: if (newestSuccessfulLogfile == null) {
072: newestSuccessfulBuild = null;
073: } else {
074: try {
075: newestSuccessfulBuild = new BuildInfo(
076: newestSuccessfulLogfile);
077: } catch (ParseException pe) {
078: newestBuild = null;
079: }
080: }
081: }
082:
083: public String getLastBuildResult() {
084: if (newestBuild == null) {
085: return null;
086: }
087:
088: return newestBuild.isSuccessful() ? PASSED : FAILED;
089: }
090:
091: public String getLastBuildTimeString(Locale locale) {
092: if (newestBuild == null) {
093: return null;
094: }
095: return getBuildTimeString(newestBuild, locale);
096: }
097:
098: public Date getLastBuildTime() {
099: if (newestBuild == null) {
100: return null;
101: }
102: return newestBuild.getBuildDate();
103: }
104:
105: public BuildInfo getLastBuild() {
106: return newestBuild;
107: }
108:
109: public String getLastSuccessfulBuildLabel() {
110: if (newestSuccessfulBuild == null) {
111: return null;
112: }
113:
114: return newestSuccessfulBuild.getLabel();
115: }
116:
117: public String getLastSuccessfulBuildTimeString(Locale locale) {
118: if (newestSuccessfulBuild == null) {
119: return null;
120: }
121: return getBuildTimeString(newestSuccessfulBuild, locale);
122: }
123:
124: private String getBuildTimeString(BuildInfo logInfo, Locale locale) {
125: Date date = logInfo.getBuildDate();
126: return DateHelper.createDateFormat(locale).format(date);
127: }
128:
129: public String getCurrentStatus(String singleProject,
130: String logDirPath, String projectName, String statusFile) {
131: boolean isSingleProject = Boolean.getBoolean(singleProject);
132:
133: return getCurrentStatus(isSingleProject, logDirPath,
134: projectName, statusFile);
135: }
136:
137: public String getCurrentStatus(boolean isSingleProject,
138: String logDirPath, String projectName, String statusFile) {
139: String status = null;
140:
141: try {
142: status = BuildStatus.getStatusHtml(isSingleProject,
143: logDirPath, projectName, statusFile,
144: BuildStatus.READ_ONLY_STATUS_LINES);
145: } catch (CruiseControlWebAppException e) {
146: status = '(' + e.getMessage() + ')';
147: }
148:
149: return status;
150: }
151: }
|