1 package org.tigris.scarab.util; 2 3 48 49 import java.util.ArrayList ; 50 import java.util.List ; 51 52 import org.apache.commons.lang.StringUtils; 53 import org.apache.fulcrum.pool.InitableRecyclable; 54 import org.tigris.scarab.om.Issue; 55 import org.tigris.scarab.om.Module; 56 57 62 public class EmailLink 63 implements InitableRecyclable, SkipFiltering 64 { 65 private String label; 66 private String attributeText; 67 private String alternateText; 68 private Integer currentModuleId; 69 private Module currentModule; 70 private boolean isOmitModule; 71 private boolean isOmitIssueType; 72 private boolean overrideSecurity; 73 74 private boolean disposed = false; 75 76 77 private List pathInfo = new ArrayList (); 78 79 80 public static final String HTTP = "http"; 81 82 83 public static final String HTTPS = "https"; 84 85 90 public EmailLink() 91 { 92 } 93 94 97 public EmailLink(Module currentModule) 98 { 99 setCurrentModule(currentModule); 100 } 101 102 public void init(Object obj) 103 { 104 if (obj instanceof Module) 105 { 106 setCurrentModule((Module)obj); 107 } 108 } 109 110 public Module getCurrentModule() 111 { 112 return this.currentModule; 113 } 114 115 public void setCurrentModule(Module cM) 116 { 117 currentModuleId = cM.getModuleId(); 118 this.currentModule = cM; 119 } 120 121 public void refresh() 122 { 123 label = null; 124 attributeText = null; 125 alternateText = null; 126 currentModuleId = null; 127 currentModule = null; 128 isOmitModule = false; 129 isOmitIssueType = false; 130 this.pathInfo.clear(); 131 } 132 133 private String convertAndTrim(String value) 134 { 135 String tmp = null; 136 if (value != null) 137 { 138 tmp = value.trim(); 139 tmp = tmp.toLowerCase(); 140 } 141 return tmp; 142 } 143 144 152 protected void addPair(List list, 153 String name, 154 String value) 155 { 156 Object [] tmp = new Object [2]; 157 158 tmp[0] = convertAndTrim(name); 159 tmp[1] = value; 160 161 list.add(tmp); 162 } 163 164 170 public EmailLink addPathInfo(String name, String value) 171 { 172 addPair(pathInfo, name, value); 173 return this; 174 } 175 176 182 public EmailLink addPathInfo(String name, Object value) 183 { 184 addPathInfo(name, value.toString()); 185 return this; 186 } 187 188 194 public EmailLink addPathInfo(String name, double value) 195 { 196 addPathInfo(name, Double.toString(value)); 197 return this; 198 } 199 200 206 public EmailLink addPathInfo(String name, int value) 207 { 208 addPathInfo(name, Integer.toString(value)); 209 return this; 210 } 211 212 218 public EmailLink addPathInfo(String name, long value) 219 { 220 addPathInfo(name, Long.toString(value)); 221 return this; 222 } 223 224 230 public EmailLink addPathInfo(String name, boolean value) 231 { 232 addPathInfo(name, (value ? "true" : "false")); 233 return this; 234 } 235 236 public EmailLink setPathInfo(String key, String value) 237 { 238 removePathInfo(key); 239 addPathInfo(key, value); 240 return this; 241 } 242 243 249 protected void removePairByName(List pairs, String name) 250 { 251 name = convertAndTrim(name); 252 for (int i = 0; i < pairs.size(); i++) 255 { 256 Object [] pair = (Object [])pairs.get(i); 257 if ( name.equals( pair[0] ) ) 258 { 259 pairs.remove(i); 260 } 261 } 262 } 263 264 267 public void removePathInfo() 268 { 269 this.pathInfo.clear(); 270 } 271 272 277 public void removePathInfo(String name) 278 { 279 removePairByName( pathInfo, name ); 280 } 281 282 287 public String getServerName() 288 { 289 String domain = null; 290 if (currentModule != null) 291 { 292 domain = currentModule.getHttpDomain(); 293 if (domain == null || domain.length() == 0) 294 { 295 domain = "check.Scarab.properties"; 296 } 297 } 298 299 return domain; 300 } 301 302 307 public int getServerPort() 308 { 309 int result = -1; 310 if (currentModule != null) 311 { 312 try 313 { 314 String port = currentModule.getPort(); 315 if (StringUtils.isNotEmpty(port)) 316 { 317 result = Integer.parseInt(port); 318 } 319 } 320 catch (Exception e) 321 { 322 Log.get().debug(e); 323 } 324 } 325 return result; 326 } 327 328 333 public String getServerScheme() 334 { 335 String result = null; 336 try 337 { 338 if (currentModule != null) 339 { 340 result = currentModule.getScheme(); 341 } 342 } 343 catch (Exception e) 344 { 345 Log.get().debug(e); 346 } 347 return result; 348 } 349 350 355 public String getScriptName() 356 { 357 String result = null; 358 try 359 { 360 if (currentModule != null) 361 { 362 result = currentModule.getScriptName(); 363 } 364 } 365 catch (Exception e) 366 { 367 Log.get().debug(e); 368 } 369 return result; 370 } 371 372 375 public boolean hasPathInfo() 376 { 377 return ! pathInfo.isEmpty(); 378 } 379 380 387 protected String renderPathInfo(List pairs) 388 { 389 return renderPairs( pairs, '/', '/' ); 390 } 391 392 404 protected String renderPairs(List pairs, char pairSep, char keyValSep) 405 { 406 boolean first = true; 407 StringBuffer out = new StringBuffer (); 408 final int count = pairs.size(); 409 for (int i = 0; i < count; i++) 410 { 411 Object [] pair = (Object []) pairs.get(i); 412 413 if ( first ) 414 { 415 first = false; 416 } 417 else 418 { 419 out.append(pairSep); 420 } 421 422 out.append(ScarabUtil.urlEncode((String ) pair[0])); 423 out.append(keyValSep); 424 out.append(ScarabUtil.urlEncode((String ) pair[1])); 425 } 426 return out.toString(); 427 } 428 429 447 public String toString() 448 { 449 StringBuffer output = new StringBuffer (); 450 output.append(getServerScheme()); 451 output.append("://"); 452 output.append(getServerName()); 453 int port = getServerPort(); 454 if (port >= 0 455 && ((HTTP.equals(getServerScheme()) && port != 80) 456 || (HTTPS.equals(getServerScheme()) && port != 443))) 457 { 458 output.append(':'); 459 output.append(port); 460 } 461 462 output.append(getScriptName()); 463 464 if (this.hasPathInfo()) 465 { 466 output.append('/'); 467 output.append(renderPathInfo(this.pathInfo)); 468 } 469 return output.toString(); 470 } 471 472 478 public EmailLink omitModule() 479 { 480 isOmitModule = true; 481 return this; 482 } 483 484 491 public EmailLink setPage(String t) 492 { 493 addPathInfo(ScarabConstants.TEMPLATE, t); 494 495 if (!isOmitModule) 496 { 497 addPathInfo(ScarabConstants.CURRENT_MODULE, currentModuleId); 498 } 499 return this; 500 } 501 502 511 public EmailLink setAction(String action) 512 { 513 addPathInfo(ScarabConstants.ACTION, action); 514 return this; 515 } 516 517 524 public EmailLink getIssueIdLink(Issue issue) 525 throws Exception 526 { 527 this.addPathInfo(ScarabConstants.ID, issue.getUniqueId()); 528 return this; 529 } 530 531 537 540 public void recycle() 541 { 542 disposed = false; 543 } 544 545 548 public void dispose() 549 { 550 refresh(); 551 disposed = true; 552 } 553 554 559 public boolean isDisposed() 560 { 561 return disposed; 562 } 563 } 564 | Popular Tags |