1 37 package net.sourceforge.cruisecontrol.publishers.rss; 38 39 import java.io.File ; 40 import java.util.ArrayList ; 41 import java.util.Collections ; 42 import java.util.Comparator ; 43 import java.util.Date ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 import net.sourceforge.cruisecontrol.Modification; 47 import net.sourceforge.cruisecontrol.CruiseControlException; 48 import net.sourceforge.cruisecontrol.util.XMLLogHelper; 49 import net.sourceforge.cruisecontrol.util.DateUtil; 50 import org.apache.log4j.Logger; 51 52 59 public class CruiseControlItem extends Item { 60 61 private static final Logger LOG = Logger.getLogger(CruiseControlItem.class); 62 63 66 public CruiseControlItem(XMLLogHelper logHelper, String buildResultsURL) throws CruiseControlException { 67 super(); 68 this.setTitle(createTitle(logHelper)); 69 this.setLink(createLink(logHelper, buildResultsURL)); 70 this.setDescription(createDescription(logHelper)); 71 try { 72 this.setPublishDate(DateUtil.parseFormattedTime(logHelper.getBuildTimestamp(), "cctimestamp")); 73 } catch (CruiseControlException ccex) { 74 this.setPublishDate(new Date ()); 76 } 77 } 78 79 80 81 82 86 private String createTitle(XMLLogHelper logHelper) throws CruiseControlException { 87 88 StringBuffer title = new StringBuffer (); 89 title.append(logHelper.getProjectName()); 90 if (logHelper.isBuildSuccessful()) { 91 String label = logHelper.getLabel(); 92 if (label.length() > 0) { 93 title.append(" "); 94 title.append(label); 95 } 96 97 if (logHelper.isBuildFix()) { 100 title.append(" Build Fixed"); 101 } else { 102 title.append(" Build Successful"); 103 } 104 } else { 105 title.append(" Build Failed"); 106 } 107 return title.toString(); 108 } 109 110 114 private String createLink(XMLLogHelper logHelper, String buildResultsURL) throws CruiseControlException { 115 116 if (buildResultsURL == null) { 117 return ""; 118 } 119 String logFileName = logHelper.getLogFileName(); 120 121 int startName = logFileName.lastIndexOf(File.separator) + 1; 122 int endName = logFileName.lastIndexOf("."); 123 String baseLogFileName = logFileName.substring(startName, endName); 124 StringBuffer url = new StringBuffer (buildResultsURL); 125 126 if (buildResultsURL.indexOf("?") == -1) { 127 url.append("?"); 128 } else { 129 url.append("&"); 130 } 131 url.append("log="); 132 url.append(baseLogFileName); 133 134 return url.toString(); 135 } 136 137 140 static class ModificationComparator implements Comparator { 141 public int compare(Object o1, Object o2) { 142 Modification mod1 = (Modification) o1; 143 Modification mod2 = (Modification) o2; 144 long modifiedTimeDifference = mod1.modifiedTime.getTime() - mod2.modifiedTime.getTime(); 145 if (modifiedTimeDifference != 0) { 146 return modifiedTimeDifference > 0 ? +1 : -1; 147 } 148 return mod1.getFileName().compareTo(mod2.getFileName()); 149 } 150 } 151 152 private String createDescription(XMLLogHelper logHelper) throws CruiseControlException { 153 StringBuffer description = new StringBuffer (); 154 155 description.append("<em>Build Time:</em> "); 157 try { 158 description.append(DateUtil.parseFormattedTime(logHelper.getBuildTimestamp(), "cctimestamp")); 159 } catch (CruiseControlException ccex) { 160 LOG.error("exception trying to resolve cctimestamp", ccex); 161 description.append("not available"); 162 } catch (NullPointerException npe) { LOG.error("NPE trying to resolve cctimestamp", npe); 164 description.append("not available"); 165 } 166 167 description.append("<br/>"); 168 169 description.append("<em>Label:</em> "); 170 if (logHelper.getLabel() != null) { 171 description.append(logHelper.getLabel()); 172 } 173 description.append("<br/>"); 174 175 description.append("<em>Modifications: </em>"); 177 try { 178 final List modifications = new ArrayList (logHelper.getModifications()); 179 Collections.sort(modifications, new ModificationComparator()); 180 description.append(modifications.size()); 181 Iterator it = modifications.iterator(); 182 while (it.hasNext()) { 183 Modification mod = (Modification) it.next(); 184 description.append("<li>"); 185 description.append(mod.getFileName()); 186 description.append(" by "); 187 if (mod.userName != null) { 188 description.append(mod.userName); 189 } else { 190 description.append("[no user]"); 191 } 192 description.append(" ("); 193 if (mod.comment != null) { 194 description.append(mod.comment); 195 } else { 196 description.append("[no comment]"); 197 } 198 description.append(")</li>"); 199 } 200 description.append("</ul>"); 201 } catch (NullPointerException npe) { LOG.error("NPE trying to build String representation of modifications in description", npe); 203 description.append("0"); 206 } 207 description.append("<br/>\n<ul>"); 208 209 return description.toString(); 210 } 211 } 212 | Popular Tags |