KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > util > SimpleHandler


1 package org.tigris.scarab.util;
2
3 import java.util.*;
4 import java.io.*;
5
6 import org.tigris.scarab.om.ActivitySet;
7 import org.tigris.scarab.om.Attachment;
8 import org.tigris.scarab.om.Attribute;
9 import org.tigris.scarab.om.AttributeOption;
10 import org.tigris.scarab.om.AttributeValue;
11 import org.tigris.scarab.om.GlobalParameter;
12 import org.tigris.scarab.om.GlobalParameterManager;
13 import org.tigris.scarab.om.Issue;
14 import org.tigris.scarab.om.IssueManager;
15 import org.tigris.scarab.om.MITListManager;
16 import org.tigris.scarab.om.Module;
17 import org.tigris.scarab.om.ModuleManager;
18 import org.tigris.scarab.om.ScarabUser;
19 import org.tigris.scarab.om.ScarabUserManager;
20
21 import org.tigris.scarab.services.cache.ScarabCache;
22 import org.tigris.scarab.util.word.IssueSearch;
23 import org.tigris.scarab.util.word.IssueSearchFactory;
24 import org.tigris.scarab.util.word.QueryResult;
25
26 import org.apache.log4j.Category;
27 import org.apache.turbine.Turbine;
28
29 /**
30  * Provides a basic API for XML-RPC requests to the Scarab server.
31  * It makes the server side for scarab-post-commit.py script (in the
32  * extensions/script directory).
33  *
34  * @author Mick
35  * @version $Id$
36  * @see org.tigris.scarab.util.SimpleHandlerClient
37  */

38 public class SimpleHandler
39 {
40     private static final Category category = Category
41             .getInstance(SimpleHandler.class);
42
43     /**
44      * Maximum number of characters to put into one comment, add multiple
45      * comments if neccessary. Really only applies to oracle databases where
46      * varchar2 has a limit of 4000, and has to be used because oracle's clob/blob
47      * doesn't work w/ torque. <br/>
48      * <b>
49      * A value of zero means no limit of comment size.
50      * </b>
51      */

52     private static final int MAX_COMMENT_SIZE;
53
54     protected static void log(String JavaDoc str)
55     {
56         category.info("SimpleHandler: " + str);
57     }
58
59     static
60     {
61         log("loading");
62     final String JavaDoc db = Turbine.getConfiguration().getString("scarab.database.type");
63         MAX_COMMENT_SIZE = (db.equalsIgnoreCase("oracle")) ? 4000 : 0;
64     }
65
66     /**
67      * Append a comment to the specified issue. Wrapper method with string
68      * parameters.
69      *
70      * @return Vector with first item a boolean indicating success.
71      * @param issues
72      * string with the ids of the issue to add the comment to
73      * (can contain other words which will simply be ignored)
74      * @param user
75      * the username of the user who is adding the comment
76      * @param comment
77      * the comment to append to the issue
78      * @param disableEmailsInt
79      * do we want this change to trigger an email to every person assigned a role.
80      */

81     public Vector addComment( final String JavaDoc issues,
82             final String JavaDoc user, final String JavaDoc comment, final int disableEmailsInt)
83     {
84
85         final Vector retValue = new Vector();
86         boolean success = false;
87         final String JavaDoc c = fixEOLs(comment);
88         final boolean disableEmails = (disableEmailsInt != 0);
89         log("addComment: issues=" + issues
90                 + ", user=" + user + ", comment=\"" + c + ", disableEmails="
91                 + disableEmailsInt);
92
93         try
94         {
95
96             // issues to add comment to
97
final Set issueSet = new HashSet();
98
99             // find user
100
final ScarabUser u = ScarabUserManager
101                     .getInstance(user, null /* XXX ??? */);
102
103             // find modules in moduleDomain
104
List modules = MITListManager.getAllModulesAllIssueTypesList(u)
105                     .getModules();
106
107             for (Iterator it = modules.iterator(); it.hasNext();)
108             {
109                 final Module m = (Module) it.next();
110                 // Parse issues from issues string
111
final List issueTokens = IssueIdParser.tokenizeText(m, issues);
112
113                 for (Iterator it2 = issueTokens.iterator(); it2.hasNext();)
114                 {
115                     final Object JavaDoc o = it2.next();
116                     // find issue
117
if (o instanceof List && ((List) o).get(1) != null
118                             && ((List) o).get(1) instanceof String JavaDoc)
119                     {
120                         final Issue i = Issue.getIssueById((String JavaDoc) ((List) o)
121                                 .get(1));
122                         issueSet.add(i);
123                     }
124                 }
125             }
126             // now add the comments to each issue found
127
for (Iterator it = issueSet.iterator(); it.hasNext();)
128             {
129                 final Issue i = (Issue) it.next();
130                 success |= addComment(i, u, c, disableEmails);
131             }
132             retValue.add(new Boolean JavaDoc(success));
133         }
134         catch (RuntimeException JavaDoc e)
135         {
136             retValue.add(Boolean.FALSE);
137             retValue.add(e);
138             e.printStackTrace();
139             throw e;
140         }
141         catch (Exception JavaDoc e)
142         {
143             retValue.add(Boolean.FALSE);
144             retValue.add(e);
145             e.printStackTrace();
146         }
147         return retValue;
148     }
149
150     /**
151      * Append a comment to the specified issue.
152      *
153      * @return success.
154      * @param issueId
155      * the id of the issue to add the comment to
156      * @param username
157      * the username of the user who is adding the comment
158      * @param comment
159      * the comment to append to the issue
160      */

161     protected boolean addComment(final Issue issue, final ScarabUser user,
162             final String JavaDoc comment, final boolean disableEmails) throws Exception JavaDoc
163     {
164
165         log("adding comment to " + issue.getIssueId());
166         // turn off emailing? we need to remember original state anyway...
167
final boolean originalEmailState = GlobalParameterManager
168                 .getBoolean(GlobalParameter.EMAIL_ENABLED);
169         // buffer the comment so we can break it down if MAX_COMMENT_SIZE != 0
170
final BufferedReader reader = new BufferedReader(new StringReader(
171                 comment));
172         // The comment Attachments that will be added to the issue.
173
// This will only contain one Attachment when MAX_COMMENT_SIZE == 0
174
final List comments = new LinkedList();
175         final StringBuffer JavaDoc nextComment = new StringBuffer JavaDoc();
176         String JavaDoc nextLine = "";
177         do
178         {
179             nextLine = reader.readLine();
180
181             if (nextLine == null ||
182                   ( MAX_COMMENT_SIZE > 0
183                       && nextComment.length() + nextLine.length() > MAX_COMMENT_SIZE ) )
184             {
185                 final Attachment attachment = new Attachment();
186                 attachment.setData(nextComment.toString());
187                 comments.add(0, attachment); // reverse order the list, it looks better on issue page.
188
nextComment.setLength(0);
189             }
190             if (nextLine != null)
191             {
192                 nextComment.append(nextLine + '\n');
193             }
194
195         } while (nextLine != null);
196
197         // actually add the comment Attachments to issue
198
synchronized (issue)
199         {
200             if (disableEmails)
201             {
202                 GlobalParameterManager.setBoolean(GlobalParameter.EMAIL_ENABLED, false);
203             }
204             for( Iterator it = comments.iterator(); it.hasNext(); )
205             {
206         Attachment attachment = (Attachment)it.next();
207         if( attachment.getData() != null && attachment.getData().length() >0 )
208         {
209                     issue.addComment(attachment, user);
210         }
211         }
212             if (disableEmails)
213             {
214                 GlobalParameterManager.setBoolean(GlobalParameter.EMAIL_ENABLED,
215                           originalEmailState);
216             }
217         }
218
219         // [TODO] It isn't neccessary to do all of these, which one is the correct one?!?
220
ScarabCache.clear();
221         IssueManager.getMethodResult().clear();
222         ModuleManager.getMethodResult().clear();
223
224         return true;
225     }
226
227     /**
228      * Update Issue's attribute option with a given user and description.
229      * Wrapper method with string parameters.
230      *
231      * @return Vector with first item a boolean indicating success.
232      */

233     public Vector changeIssueAttributeOption(final String JavaDoc issue,
234             final String JavaDoc user, final String JavaDoc attribute, final String JavaDoc option,
235             final String JavaDoc description)
236     {
237
238         log("changeIssueAttributeOption: issue=" + issue + ", user=" + user
239                 + ", attribute=\"" + attribute + "\"" + ", option=\"" + option
240                 + "\"" + ", description=\"" + description + "\"");
241
242         final Vector retValue = new Vector();
243         try
244         {
245             // find issue
246
final Issue i = Issue.getIssueById(issue);
247             // find user
248
final ScarabUser u = ScarabUserManager
249                     .getInstance(user, null /* XXX ??? */);
250             // find attribute
251
final Attribute a = Attribute.getInstance(attribute);
252             // find attributeOption
253
final AttributeOption o = AttributeOption.getInstance(a, option);
254             // proper method call
255

256             retValue.add(new Boolean JavaDoc(changeIssueAttributeOption(i, u, a, o,
257                     description)));
258         }
259         catch (RuntimeException JavaDoc e)
260         {
261             retValue.add(Boolean.FALSE);
262             retValue.add(e);
263             e.printStackTrace();
264             throw e;
265         }
266         catch (Exception JavaDoc e)
267         {
268             retValue.add(Boolean.FALSE);
269             retValue.add(e);
270             e.printStackTrace();
271         }
272         return retValue;
273     }
274
275     /**
276      * Update Issue's attribute option with a given user and description
277      *
278      * @return success.
279      */

280     protected boolean changeIssueAttributeOption(final Issue issue,
281             final ScarabUser user, final Attribute attribute,
282             final AttributeOption option, final String JavaDoc description)
283             throws Exception JavaDoc
284     {
285
286         final AttributeValue status = issue.getAttributeValue(attribute);
287         final AttributeValue nyStatus = AttributeValue.getNewInstance(
288                 attribute, issue);
289         nyStatus.setOptionId(option.getOptionId());
290         nyStatus.setActivityDescription(description);
291         final HashMap newAttVals = new HashMap();
292         newAttVals.put(status.getAttributeId(), nyStatus);
293         final ActivitySet activitySet = issue.setAttributeValues(null,
294                 newAttVals, null, user);
295         return true;
296     }
297
298     public Vector findIssuesWithAttributeValue(final String JavaDoc user,
299             final String JavaDoc attribute, final String JavaDoc value)
300     {
301
302         log("findIssuesWithAttributeValue: user=" + user + ", attribute=\""
303                 + attribute + "\"" + ", value=\"" + value + "\"");
304
305         final Vector retValue = new Vector();
306         try
307         {
308
309             // find user
310
final ScarabUser u = ScarabUserManager
311                     .getInstance(user, null /* XXX ??? */);
312             // find attribute
313
final Attribute a = Attribute.getInstance(attribute);
314             // proper method call
315
retValue.add(findIssuesWithAttributeValue(u, a, value));
316         }
317         catch (RuntimeException JavaDoc e)
318         {
319             retValue.add(null);
320             retValue.add(e);
321             e.printStackTrace();
322             throw e;
323         }
324         catch (Exception JavaDoc e)
325         {
326             retValue.add(null);
327             retValue.add(e);
328             e.printStackTrace();
329         }
330         return retValue;
331     }
332
333     protected Vector findIssuesWithAttributeValue(final ScarabUser user,
334             final Attribute attribute, final String JavaDoc value) throws Exception JavaDoc
335     {
336
337         final Vector retValue = new Vector();
338         final IssueSearch search = IssueSearchFactory.INSTANCE.getInstance(
339                 MITListManager.getAllModulesAllIssueTypesList(user), user);
340         final AttributeValue av = AttributeValue.getNewInstance(attribute,
341                 search);
342         av.setValue(value);
343         search.addAttributeValue(av);
344         final Iterator queryresults = search.getQueryResults();
345
346         while (queryresults.hasNext())
347         {
348             final QueryResult qr = (QueryResult) queryresults.next();
349             retValue.add(qr.getIssueId());
350             //log(" Adding to results "+qr.getIssueId());
351
}
352
353         // close search
354
search.close();
355         IssueSearchFactory.INSTANCE.notifyDone();
356         // return matching issues
357
return retValue;
358     }
359
360     /**
361      * Hack to replace the string "\n" with EOL characters...
362      * string.replaceAll("\\n","\n") does not work.
363      */

364     private static String JavaDoc fixEOLs(final String JavaDoc str)
365     {
366         final int idx = str.indexOf("\\n");
367         if (idx != -1)
368         {
369             return str.substring(0, idx) + "\n"
370                     + fixEOLs(str.substring(idx + "\\n".length()));
371         }
372         else
373         {
374             return str;
375         }
376     }
377
378 }
379
Popular Tags