KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > sourcecontrols > CMSynergyModification


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.sourcecontrols;
38
39 import java.text.DateFormat JavaDoc;
40 import java.text.ParseException JavaDoc;
41 import java.text.SimpleDateFormat JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.Date JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
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 /**
55  * Data structure which holds data specific to a single modification
56  * within a CM Synergy repository.
57  *
58  * @author <a HREF="mailto:rjmpsmith@hotmail.com">Robert J. Smith</a>
59  */

60 public class CMSynergyModification extends Modification {
61
62     private static final Logger LOG = Logger.getLogger(CMSynergyModification.class);
63
64     private static final String JavaDoc MODIFICATION_TYPE = "ccmtask";
65     private static final String JavaDoc TAGNAME_MODIFICATION = "modification";
66     private static final String JavaDoc TAGNAME_OBJECT = "ccmobject";
67     private static final String JavaDoc TAGNAME_CHANGEREQUEST = "ccmcr";
68     private static final String JavaDoc TAGNAME_NAME = "name";
69     private static final String JavaDoc TAGNAME_TASKNUMBER = "task";
70     private static final String JavaDoc TAGNAME_VERSION = "version";
71     private static final String JavaDoc TAGNAME_TYPE = "type";
72     private static final String JavaDoc TAGNAME_INSTANCE = "instance";
73     private static final String JavaDoc TAGNAME_PROJECT = "project";
74     private static final String JavaDoc TAGNAME_COMMENT = "comment";
75     private static final String JavaDoc TAGNAME_DATE = "date";
76     private static final String JavaDoc TAGNAME_USER = "user";
77     private static final String JavaDoc TAGNAME_EMAIL = "email";
78     private static final String JavaDoc TAGNAME_REVISION = "revision";
79     private static final String JavaDoc TAGNAME_HTML_LINK = "a";
80     private static final String JavaDoc TAGNAME_HTML_LINK_HREF = "href";
81     private static final String JavaDoc TAGNAME_HTML_INS = "ins";
82
83     /**
84      * The CM Synergy task number represented by this modification
85      */

86     public String JavaDoc taskNumber;
87
88     /**
89      * A list of change requests associated with this modification
90      */

91     public List JavaDoc changeRequests = new ArrayList JavaDoc();
92
93     /**
94      * Creates a new <code>CMSynergyModification</code> object and sets it's
95      * modification type to "ccmtask".
96      */

97     public CMSynergyModification() {
98         super(MODIFICATION_TYPE);
99     }
100
101     /**
102      * Creates a new <code>ModifiedObject</code>, and adds it to the list of
103      * CM Synergy objects associated with the task.
104      *
105      * @return A new <code>ModifiedObject</code>
106      */

107     public final ModifiedObject createModifiedObject() {
108         ModifiedObject obj = new ModifiedObject();
109         files.add(obj);
110         return obj;
111     }
112
113     /**
114      * Creates a new <code>ModifiedObject</code>, populates the fields, and
115      * adds it to the list of CM Synergy objects associated with the task.
116      *
117      * @param name
118      * The object's name
119      * @param version
120      * The object's version
121      * @param type
122      * The object's type within CM Synergy
123      * @param instance
124      * The object's instance
125      * @param project
126      * The project with which the object is associated
127      * @param comment
128      * The comment provided when checking in the object
129      *
130      * @return A new <code>ModifiedObject</code>
131      */

132     public final ModifiedObject createModifiedObject(String JavaDoc name,
133             String JavaDoc version, String JavaDoc type, String JavaDoc instance, String JavaDoc project,
134             String JavaDoc 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     /**
146      * Creates a new <code>ChangeRequest</code>, and adds it to the list of
147      * change requests associated with the task.
148      *
149      * @param number The CR number
150      *
151      * @return A new <code>ChangeRequest</code>
152      */

153     public final ChangeRequest createChangeRequest(String JavaDoc number) {
154         ChangeRequest cr = new ChangeRequest();
155         cr.number = number;
156         changeRequests.add(cr);
157         return cr;
158     }
159
160     /*
161      * (non-Javadoc)
162      *
163      * @see net.sourceforge.cruisecontrol.Modification#toElement(java.text.DateFormat)
164      */

165     public Element toElement(DateFormat JavaDoc 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 JavaDoc 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     /* (non-Javadoc)
229      * @see java.lang.Object#toString()
230      */

231     public String JavaDoc toString() {
232         SimpleDateFormat JavaDoc formatter =
233             new SimpleDateFormat JavaDoc("yyyy/MM/dd HH:mm:ss");
234         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc 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     /* (non-Javadoc)
264      * @see net.sourceforge.cruisecontrol.Modification#log(java.text.DateFormat)
265      */

266     public void log(DateFormat JavaDoc 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 JavaDoc 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     /* (non-Javadoc)
296      * @see net.sourceforge.cruisecontrol.Modification#fromElement(org.jdom.Element, java.text.DateFormat)
297      */

298     public void fromElement(Element modification, DateFormat JavaDoc formatter) {
299
300         type = modification.getAttributeValue(TAGNAME_TYPE);
301
302         try {
303             String JavaDoc 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 JavaDoc e) {
310             modifiedTime = new Date JavaDoc();
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 JavaDoc modfiles = modification.getChildren(TAGNAME_OBJECT);
321         if (modfiles != null) {
322             Iterator JavaDoc 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 JavaDoc crs = modification.getChildren(TAGNAME_CHANGEREQUEST);
333         if (crs != null) {
334             Iterator JavaDoc 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     /* (non-Javadoc)
345      * @see java.lang.Object#equals(java.lang.Object)
346      */

347     public boolean equals(Object JavaDoc 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     /**
356      * Data structure which holds data specific to a single object included in a
357      * modification within a CM Synergy repository.
358      *
359      * @author <a HREF="mailto:rjmpsmith@hotmail.com">Robert J. Smith </a>
360      */

361     public class ModifiedObject {
362
363         // Let's not deal with possible null values
364
public String JavaDoc name = "";
365         public String JavaDoc version = "";
366         public String JavaDoc type = "";
367         public String JavaDoc instance = "";
368         public String JavaDoc project = "";
369         public String JavaDoc comment = "";
370
371         // Only the parent class should call the constructor
372
protected ModifiedObject() {
373         }
374
375         /* (non-Javadoc)
376          * @see net.sourceforge.cruisecontrol.Modification#fromElement(org.jdom.Element, java.text.DateFormat)
377          */

378         public Element toElement(DateFormat JavaDoc 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         /* (non-Javadoc)
417          * @see net.sourceforge.cruisecontrol.Modification#fromElement(org.jdom.Element, java.text.DateFormat)
418          */

419         public void fromElement(Element modification, DateFormat JavaDoc 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     /**
430      * Data structure which holds data specific to a Change Request associated
431      * with a modification within a CM Synergy repository.
432      *
433      * @author <a HREF="mailto:rjmpsmith@hotmail.com">Robert J. Smith </a>
434      */

435     public class ChangeRequest {
436
437         public String JavaDoc href = null;
438         public String JavaDoc number = "";
439
440         // Only the parent class should call the constructor
441
protected ChangeRequest() {
442         }
443
444         /* (non-Javadoc)
445          * @see net.sourceforge.cruisecontrol.Modification#fromElement(org.jdom.Element, java.text.DateFormat)
446          */

447         public Element toElement(DateFormat JavaDoc 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         /* (non-Javadoc)
465          * @see net.sourceforge.cruisecontrol.Modification#fromElement(org.jdom.Element, java.text.DateFormat)
466          */

467         public void fromElement(Element modification, DateFormat JavaDoc 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