1 37 package net.sourceforge.cruisecontrol.sourcecontrols; 38 39 import java.text.DateFormat ; 40 import java.text.ParseException ; 41 import java.text.SimpleDateFormat ; 42 import java.util.ArrayList ; 43 import java.util.Date ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 47 import org.apache.log4j.Logger; 48 import org.jdom.CDATA; 49 import org.jdom.Element; 50 import org.jdom.output.XMLOutputter; 51 52 import net.sourceforge.cruisecontrol.Modification; 53 54 60 public class CMSynergyModification extends Modification { 61 62 private static final Logger LOG = Logger.getLogger(CMSynergyModification.class); 63 64 private static final String MODIFICATION_TYPE = "ccmtask"; 65 private static final String TAGNAME_MODIFICATION = "modification"; 66 private static final String TAGNAME_OBJECT = "ccmobject"; 67 private static final String TAGNAME_CHANGEREQUEST = "ccmcr"; 68 private static final String TAGNAME_NAME = "name"; 69 private static final String TAGNAME_TASKNUMBER = "task"; 70 private static final String TAGNAME_VERSION = "version"; 71 private static final String TAGNAME_TYPE = "type"; 72 private static final String TAGNAME_INSTANCE = "instance"; 73 private static final String TAGNAME_PROJECT = "project"; 74 private static final String TAGNAME_COMMENT = "comment"; 75 private static final String TAGNAME_DATE = "date"; 76 private static final String TAGNAME_USER = "user"; 77 private static final String TAGNAME_EMAIL = "email"; 78 private static final String TAGNAME_REVISION = "revision"; 79 private static final String TAGNAME_HTML_LINK = "a"; 80 private static final String TAGNAME_HTML_LINK_HREF = "href"; 81 private static final String TAGNAME_HTML_INS = "ins"; 82 83 86 public String taskNumber; 87 88 91 public List changeRequests = new ArrayList (); 92 93 97 public CMSynergyModification() { 98 super(MODIFICATION_TYPE); 99 } 100 101 107 public final ModifiedObject createModifiedObject() { 108 ModifiedObject obj = new ModifiedObject(); 109 files.add(obj); 110 return obj; 111 } 112 113 132 public final ModifiedObject createModifiedObject(String name, 133 String version, String type, String instance, String project, 134 String comment) { 135 ModifiedObject obj = createModifiedObject(); 136 obj.name = name; 137 obj.version = version; 138 obj.type = type; 139 obj.instance = instance; 140 obj.project = project; 141 obj.comment = comment; 142 return obj; 143 } 144 145 153 public final ChangeRequest createChangeRequest(String number) { 154 ChangeRequest cr = new ChangeRequest(); 155 cr.number = number; 156 changeRequests.add(cr); 157 return cr; 158 } 159 160 165 public Element toElement(DateFormat formatter) { 166 Element modificationElement = new Element(TAGNAME_MODIFICATION); 167 modificationElement.setAttribute(TAGNAME_TYPE, type); 168 169 if (modifiedTime != null) { 170 Element dateElement = new Element(TAGNAME_DATE); 171 dateElement.addContent(formatter.format(modifiedTime)); 172 modificationElement.addContent(dateElement); 173 } 174 175 if (userName != null) { 176 Element userElement = new Element(TAGNAME_USER); 177 userElement.addContent(userName); 178 modificationElement.addContent(userElement); 179 } 180 181 if (comment != null) { 182 Element commentElement = new Element(TAGNAME_COMMENT); 183 CDATA cd; 184 try { 185 cd = new CDATA(comment); 186 } catch (org.jdom.IllegalDataException e) { 187 LOG.error(e); 188 cd = new CDATA( 189 "Unable to parse comment. It contains illegal data."); 190 } 191 commentElement.addContent(cd); 192 modificationElement.addContent(commentElement); 193 } 194 195 if (taskNumber != null) { 196 Element taskNumberElement = new Element(TAGNAME_TASKNUMBER); 197 taskNumberElement.addContent(taskNumber); 198 modificationElement.addContent(taskNumberElement); 199 } 200 201 if (revision != null) { 202 Element revisionElement = new Element(TAGNAME_REVISION); 203 revisionElement.addContent(revision); 204 modificationElement.addContent(revisionElement); 205 } 206 207 if (emailAddress != null) { 208 Element emailAddressElement = new Element(TAGNAME_EMAIL); 209 emailAddressElement.addContent(emailAddress); 210 modificationElement.addContent(emailAddressElement); 211 } 212 213 Iterator i = files.iterator(); 214 while (i.hasNext()) { 215 ModifiedObject obj = (ModifiedObject) i.next(); 216 modificationElement.addContent(obj.toElement(formatter)); 217 } 218 219 i = changeRequests.iterator(); 220 while (i.hasNext()) { 221 ChangeRequest cr = (ChangeRequest) i.next(); 222 modificationElement.addContent(cr.toElement(formatter)); 223 } 224 225 return modificationElement; 226 } 227 228 231 public String toString() { 232 SimpleDateFormat formatter = 233 new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss"); 234 StringBuffer sb = new StringBuffer (); 235 236 sb.append("Task Number: ").append(taskNumber).append('\n'); 237 sb.append("Owner: ").append(userName).append('\n'); 238 sb.append("Release: ").append(revision).append('\n'); 239 sb.append("Completion Date: ").append(formatter.format(modifiedTime)).append('\n'); 240 sb.append("Synopsis: ").append(comment).append('\n'); 241 242 Iterator i = changeRequests.iterator(); 243 while (i.hasNext()) { 244 ChangeRequest cr = (ChangeRequest) i.next(); 245 sb.append("\tChange Request: ").append(cr.number).append('\n'); 246 } 247 248 i = files.iterator(); 249 while (i.hasNext()) { 250 ModifiedObject obj = (ModifiedObject) i.next(); 251 sb.append("\tAssociated Object: ").append(obj.name).append('\n'); 252 sb.append("\tVersion: ").append(obj.version).append('\n'); 253 sb.append("\tType: ").append(obj.type).append('\n'); 254 sb.append("\tInstance: ").append(obj.instance).append('\n'); 255 sb.append("\tProject: ").append(obj.project).append('\n'); 256 sb.append("\tComment: ").append(obj.comment).append('\n'); 257 } 258 259 sb.append('\n'); 260 return sb.toString(); 261 } 262 263 266 public void log(DateFormat formatter) { 267 if (LOG.isDebugEnabled()) { 268 LOG.debug("Task Number: " + taskNumber); 269 LOG.debug("Owner: " + userName); 270 LOG.debug("Release: " + revision); 271 LOG.debug("Completion Date: " + formatter.format(modifiedTime)); 272 LOG.debug("Synopsis: " + comment); 273 274 Iterator i = changeRequests.iterator(); 275 while (i.hasNext()) { 276 ChangeRequest cr = (ChangeRequest) i.next(); 277 LOG.debug("\tChange Request: " + cr.number + "\n"); 278 } 279 280 i = files.iterator(); 281 while (i.hasNext()) { 282 ModifiedObject obj = (ModifiedObject) i.next(); 283 LOG.debug("\tAssociated Object: " + obj.name); 284 LOG.debug("\tVersion: " + obj.version); 285 LOG.debug("\tType: " + obj.type); 286 LOG.debug("\tInstance: " + obj.instance); 287 LOG.debug("\tProject: " + obj.project); 288 LOG.debug("\tComment: " + obj.comment); 289 } 290 291 LOG.debug(""); 292 } 293 } 294 295 298 public void fromElement(Element modification, DateFormat formatter) { 299 300 type = modification.getAttributeValue(TAGNAME_TYPE); 301 302 try { 303 String s = modification.getChildText(TAGNAME_DATE); 304 if (s == null) { 305 XMLOutputter outputter = new XMLOutputter(); 306 LOG.info("XML: " + outputter.outputString(modification)); 307 } 308 modifiedTime = formatter.parse(s); 309 } catch (ParseException e) { 310 modifiedTime = new Date (); 311 } 312 313 taskNumber = modification.getChildText(TAGNAME_TASKNUMBER); 314 revision = modification.getChildText(TAGNAME_REVISION); 315 userName = modification.getChildText(TAGNAME_USER); 316 comment = modification.getChildText(TAGNAME_COMMENT); 317 emailAddress = modification.getChildText(TAGNAME_EMAIL); 318 319 files.clear(); 320 List modfiles = modification.getChildren(TAGNAME_OBJECT); 321 if (modfiles != null) { 322 Iterator it = modfiles.iterator(); 323 while (it.hasNext()) { 324 Element modfileElement = (Element) it.next(); 325 ModifiedObject modfile = new ModifiedObject(); 326 modfile.fromElement(modfileElement, formatter); 327 files.add(modfile); 328 } 329 } 330 331 changeRequests.clear(); 332 List crs = modification.getChildren(TAGNAME_CHANGEREQUEST); 333 if (crs != null) { 334 Iterator it = crs.iterator(); 335 while (it.hasNext()) { 336 Element crElement = (Element) it.next(); 337 ChangeRequest cr = new ChangeRequest(); 338 cr.fromElement(crElement, formatter); 339 changeRequests.add(cr); 340 } 341 } 342 } 343 344 347 public boolean equals(Object o) { 348 if (o == null || !(o instanceof CMSynergyModification)) { 349 return false; 350 } 351 CMSynergyModification mod = (CMSynergyModification) o; 352 return (type.equals(mod.type) && taskNumber.equals(mod.taskNumber)); 353 } 354 355 361 public class ModifiedObject { 362 363 public String name = ""; 365 public String version = ""; 366 public String type = ""; 367 public String instance = ""; 368 public String project = ""; 369 public String comment = ""; 370 371 protected ModifiedObject() { 373 } 374 375 378 public Element toElement(DateFormat formatter) { 379 Element element = new Element(TAGNAME_OBJECT); 380 381 Element nameElement = new Element(TAGNAME_NAME); 382 nameElement.addContent(name); 383 element.addContent(nameElement); 384 385 Element versionElement = new Element(TAGNAME_VERSION); 386 versionElement.addContent(version); 387 element.addContent(versionElement); 388 389 Element typeElement = new Element(TAGNAME_TYPE); 390 typeElement.addContent(type); 391 element.addContent(typeElement); 392 393 Element instanceElement = new Element(TAGNAME_INSTANCE); 394 instanceElement.addContent(instance); 395 element.addContent(instanceElement); 396 397 Element projectElement = new Element(TAGNAME_PROJECT); 398 projectElement.addContent(project); 399 element.addContent(projectElement); 400 401 Element commentElement = new Element(TAGNAME_COMMENT); 402 CDATA cd; 403 try { 404 cd = new CDATA(comment); 405 } catch (org.jdom.IllegalDataException e) { 406 LOG.error(e); 407 cd = new CDATA( 408 "Unable to parse comment. It contains illegal data."); 409 } 410 commentElement.addContent(cd); 411 element.addContent(commentElement); 412 413 return element; 414 } 415 416 419 public void fromElement(Element modification, DateFormat formatter) { 420 name = modification.getChildText(TAGNAME_NAME); 421 version = modification.getChildText(TAGNAME_VERSION); 422 type = modification.getChildText(TAGNAME_TYPE); 423 instance = modification.getChildText(TAGNAME_INSTANCE); 424 project = modification.getChildText(TAGNAME_PROJECT); 425 comment = modification.getChildText(TAGNAME_COMMENT); 426 } 427 } 428 429 435 public class ChangeRequest { 436 437 public String href = null; 438 public String number = ""; 439 440 protected ChangeRequest() { 442 } 443 444 447 public Element toElement(DateFormat formatter) { 448 Element element = new Element(TAGNAME_CHANGEREQUEST); 449 450 if (href != null) { 451 Element linkElement = new Element(TAGNAME_HTML_LINK); 452 linkElement.setAttribute(TAGNAME_HTML_LINK_HREF, href); 453 linkElement.addContent(number); 454 element.addContent(linkElement); 455 } else { 456 Element insElement = new Element(TAGNAME_HTML_INS); 457 insElement.addContent(number); 458 element.addContent(insElement); 459 } 460 461 return element; 462 } 463 464 467 public void fromElement(Element modification, DateFormat formatter) { 468 Element linkElement = modification.getChild(TAGNAME_HTML_LINK); 469 if (linkElement != null) { 470 href = linkElement.getAttributeValue(TAGNAME_HTML_LINK_HREF); 471 number = linkElement.getText(); 472 } else { 473 Element insElement = modification.getChild(TAGNAME_HTML_INS); 474 if (insElement != null) { 475 number = insElement.getText(); 476 } 477 } 478 } 479 } 480 } 481 | Popular Tags |