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 38 public class SimpleHandler 39 { 40 private static final Category category = Category 41 .getInstance(SimpleHandler.class); 42 43 52 private static final int MAX_COMMENT_SIZE; 53 54 protected static void log(String str) 55 { 56 category.info("SimpleHandler: " + str); 57 } 58 59 static 60 { 61 log("loading"); 62 final String db = Turbine.getConfiguration().getString("scarab.database.type"); 63 MAX_COMMENT_SIZE = (db.equalsIgnoreCase("oracle")) ? 4000 : 0; 64 } 65 66 81 public Vector addComment( final String issues, 82 final String user, final String comment, final int disableEmailsInt) 83 { 84 85 final Vector retValue = new Vector(); 86 boolean success = false; 87 final String 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 final Set issueSet = new HashSet(); 98 99 final ScarabUser u = ScarabUserManager 101 .getInstance(user, null ); 102 103 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 final List issueTokens = IssueIdParser.tokenizeText(m, issues); 112 113 for (Iterator it2 = issueTokens.iterator(); it2.hasNext();) 114 { 115 final Object o = it2.next(); 116 if (o instanceof List && ((List) o).get(1) != null 118 && ((List) o).get(1) instanceof String ) 119 { 120 final Issue i = Issue.getIssueById((String ) ((List) o) 121 .get(1)); 122 issueSet.add(i); 123 } 124 } 125 } 126 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 (success)); 133 } 134 catch (RuntimeException e) 135 { 136 retValue.add(Boolean.FALSE); 137 retValue.add(e); 138 e.printStackTrace(); 139 throw e; 140 } 141 catch (Exception e) 142 { 143 retValue.add(Boolean.FALSE); 144 retValue.add(e); 145 e.printStackTrace(); 146 } 147 return retValue; 148 } 149 150 161 protected boolean addComment(final Issue issue, final ScarabUser user, 162 final String comment, final boolean disableEmails) throws Exception 163 { 164 165 log("adding comment to " + issue.getIssueId()); 166 final boolean originalEmailState = GlobalParameterManager 168 .getBoolean(GlobalParameter.EMAIL_ENABLED); 169 final BufferedReader reader = new BufferedReader(new StringReader( 171 comment)); 172 final List comments = new LinkedList(); 175 final StringBuffer nextComment = new StringBuffer (); 176 String 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); nextComment.setLength(0); 189 } 190 if (nextLine != null) 191 { 192 nextComment.append(nextLine + '\n'); 193 } 194 195 } while (nextLine != null); 196 197 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 ScarabCache.clear(); 221 IssueManager.getMethodResult().clear(); 222 ModuleManager.getMethodResult().clear(); 223 224 return true; 225 } 226 227 233 public Vector changeIssueAttributeOption(final String issue, 234 final String user, final String attribute, final String option, 235 final String 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 final Issue i = Issue.getIssueById(issue); 247 final ScarabUser u = ScarabUserManager 249 .getInstance(user, null ); 250 final Attribute a = Attribute.getInstance(attribute); 252 final AttributeOption o = AttributeOption.getInstance(a, option); 254 256 retValue.add(new Boolean (changeIssueAttributeOption(i, u, a, o, 257 description))); 258 } 259 catch (RuntimeException e) 260 { 261 retValue.add(Boolean.FALSE); 262 retValue.add(e); 263 e.printStackTrace(); 264 throw e; 265 } 266 catch (Exception e) 267 { 268 retValue.add(Boolean.FALSE); 269 retValue.add(e); 270 e.printStackTrace(); 271 } 272 return retValue; 273 } 274 275 280 protected boolean changeIssueAttributeOption(final Issue issue, 281 final ScarabUser user, final Attribute attribute, 282 final AttributeOption option, final String description) 283 throws Exception 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 user, 299 final String attribute, final String 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 final ScarabUser u = ScarabUserManager 311 .getInstance(user, null ); 312 final Attribute a = Attribute.getInstance(attribute); 314 retValue.add(findIssuesWithAttributeValue(u, a, value)); 316 } 317 catch (RuntimeException e) 318 { 319 retValue.add(null); 320 retValue.add(e); 321 e.printStackTrace(); 322 throw e; 323 } 324 catch (Exception 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 value) throws Exception 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 } 352 353 search.close(); 355 IssueSearchFactory.INSTANCE.notifyDone(); 356 return retValue; 358 } 359 360 364 private static String fixEOLs(final String 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 |