1 package org.tigris.scarab.tools; 2 3 48 49 import java.util.Date ; 50 import java.util.Calendar ; 51 import java.util.List ; 52 import java.util.ArrayList ; 53 import java.util.StringTokenizer ; 54 import java.util.Enumeration ; 55 56 import org.apache.commons.lang.StringUtils; 57 import org.apache.log4j.Logger; 58 import org.apache.fulcrum.TurbineServices; 59 import org.apache.fulcrum.security.TurbineSecurity; 60 import org.apache.fulcrum.security.entity.User; 61 import org.apache.fulcrum.velocity.TurbineVelocityService; 62 import org.apache.fulcrum.velocity.VelocityService; 63 import org.apache.turbine.services.pull.ApplicationTool; 64 65 import org.apache.velocity.app.FieldMethodizer; 66 67 import org.tigris.scarab.om.AttributePeer; 68 import org.tigris.scarab.om.IssueTypePeer; 69 70 import org.tigris.scarab.om.GlobalParameter; 71 import org.tigris.scarab.om.ScarabUser; 72 import org.tigris.scarab.om.ScarabUserImplPeer; 73 import org.tigris.scarab.om.GlobalParameterManager; 74 import org.tigris.scarab.om.ModuleManager; 75 import org.tigris.scarab.om.Module; 76 import org.tigris.scarab.om.MITListManager; 77 import org.tigris.scarab.services.security.ScarabSecurity; 78 import org.tigris.scarab.workflow.Workflow; 79 import org.tigris.scarab.workflow.WorkflowFactory; 80 import org.tigris.scarab.util.IssueIdParser; 81 import org.tigris.scarab.util.Log; 82 import org.tigris.scarab.util.SkipFiltering; 83 import org.tigris.scarab.util.SimpleSkipFiltering; 84 import org.tigris.scarab.util.ScarabLink; 85 import org.tigris.scarab.util.ScarabUtil; 86 87 import org.apache.torque.util.Criteria; 88 import org.apache.torque.TorqueException; 89 90 import org.apache.turbine.Turbine; 91 92 107 public class ScarabGlobalTool 108 implements ApplicationTool 109 { 110 private static int moduleCodeLength = 0; 111 112 private static final Logger LOG = 113 Logger.getLogger("org.tigris.scarab"); 114 115 118 private FieldMethodizer constant = null; 119 120 123 private FieldMethodizer security = null; 124 125 128 private FieldMethodizer parameterName = null; 129 130 private static final String BUILD_VERSION = 131 Turbine.getConfiguration().getString("scarab.build.version", ""); 132 133 134 135 public void init(Object data) 136 { 137 } 138 139 public void refresh() 140 { 141 } 142 143 146 public ScarabGlobalTool() 147 { 148 constant = new FieldMethodizer( 149 "org.tigris.scarab.util.ScarabConstants"); 150 security = new FieldMethodizer( 151 "org.tigris.scarab.services.security.ScarabSecurity"); 152 parameterName = new FieldMethodizer( 153 "org.tigris.scarab.om.GlobalParameter"); 154 } 155 156 159 public String getBuildVersion() 160 { 161 return BUILD_VERSION; 162 } 163 164 168 public FieldMethodizer getConstant() 169 { 170 return constant; 171 } 172 173 177 public FieldMethodizer getPermission() 178 { 179 return security; 180 } 181 182 185 public FieldMethodizer getParameterName() 186 { 187 return parameterName; 188 } 189 190 public String replace(String text, String a, String b) 191 { 192 return StringUtils.replace(text, a, b); 193 } 194 195 public GlobalParameterManager getParameter() 196 { 197 return GlobalParameterManager.getManager(); 198 } 199 200 205 public List getAllPermissions() 206 { 207 return ScarabSecurity.getAllPermissions(); 208 } 209 210 213 public List getAllAttributes() 214 throws Exception 215 { 216 return AttributePeer.getAttributes(); 217 } 218 219 222 public List getAttributes(String attributeType) 223 throws Exception 224 { 225 return AttributePeer.getAttributes(attributeType, false); 226 } 227 228 232 public List getAllAttributes(String attributeType, boolean includeDeleted, 233 String sortColumn, String sortPolarity) 234 throws Exception 235 { 236 return AttributePeer.getAttributes(attributeType, includeDeleted, 237 sortColumn, sortPolarity); 238 } 239 240 244 public List getAllAttributes(String sortColumn, String sortPolarity) 245 throws Exception 246 { 247 return AttributePeer.getAttributes(sortColumn, sortPolarity); 248 } 249 250 253 public List getUserAttributes() 254 throws Exception 255 { 256 return AttributePeer.getAttributes("user"); 257 } 258 259 260 263 public List getUserAttributes(String sortColumn, String sortPolarity) 264 throws Exception 265 { 266 return AttributePeer.getAttributes("user", false, sortColumn, sortPolarity); 267 } 268 269 272 public List getUserAttributes(boolean includeDeleted, String sortColumn, 273 String sortPolarity) 274 throws Exception 275 { 276 return AttributePeer.getAttributes("user", includeDeleted, sortColumn, sortPolarity); 277 } 278 279 282 public List getAttributes(String attributeType, boolean includeDeleted, 283 String sortColumn, String sortPolarity) 284 throws Exception 285 { 286 return AttributePeer.getAttributes(attributeType, includeDeleted, 287 sortColumn, sortPolarity); 288 } 289 290 public List getAllIssueTypes() 291 throws Exception 292 { 293 return IssueTypePeer.getAllIssueTypes(false, "name", "asc"); 294 } 295 296 299 public List getAllIssueTypes(boolean deleted) 300 throws Exception 301 { 302 return IssueTypePeer.getAllIssueTypes(deleted, "name", "asc"); 303 } 304 305 308 public List getAllIssueTypes(boolean deleted, String sortColumn, 309 String sortPolarity) 310 throws Exception 311 { 312 return IssueTypePeer.getAllIssueTypes(deleted, sortColumn, sortPolarity); 313 } 314 315 318 public Workflow getWorkflow() 319 throws Exception 320 { 321 return WorkflowFactory.getInstance(); 322 } 323 324 333 public List getSearchUsers(String searchField, String searchCriteria) 334 throws Exception 335 { 336 return (getSearchUsers(searchField, searchCriteria, null, null)); 337 } 338 339 352 362 public List getSearchUsers(String searchField, String searchCriteria, 363 String orderByField, String ascOrDesc) 364 throws Exception 365 { 366 ArrayList userSearchList = new ArrayList (); 367 String lSearchField = ""; 368 String lOrderByField = ""; 369 370 Criteria criteria = new Criteria(); 371 372 if (searchCriteria != null && searchCriteria.length() > 0) 374 { 375 if (searchField.equals("FIRST_NAME")) 376 { 377 lSearchField = ScarabUser.FIRST_NAME; 378 } 379 else if (searchField.equals("LAST_NAME")) 380 { 381 lSearchField = ScarabUser.LAST_NAME; 382 } 383 else if (searchField.equals("LOGIN_NAME")) 384 { 385 lSearchField = ScarabUser.USERNAME; 386 } 387 else 388 { 389 lSearchField = ScarabUser.EMAIL; 390 } 391 392 lSearchField = ScarabUserImplPeer.getTableName() + '.' + lSearchField; 395 396 criteria = criteria.add(lSearchField, 397 (Object )('%' + searchCriteria.trim() + '%'),Criteria.LIKE); 398 } 399 400 if (orderByField != null && orderByField.length() > 0) 402 { 403 if (orderByField.equals("FIRST_NAME")) 404 { 405 lOrderByField = ScarabUser.FIRST_NAME; 406 } 407 else if (orderByField.equals("LAST_NAME")) 408 { 409 lOrderByField = ScarabUser.LAST_NAME; 410 } 411 else if (orderByField.equals("LOGIN_NAME")) 412 { 413 lOrderByField = ScarabUser.USERNAME; 414 } 415 else 416 { 417 lOrderByField = ScarabUser.EMAIL; 418 } 419 420 lOrderByField = ScarabUserImplPeer.getTableName() + '.' + lOrderByField; 423 424 if (ascOrDesc != null && ascOrDesc.equalsIgnoreCase("DESC")) 425 { 426 criteria = criteria.addDescendingOrderByColumn(lOrderByField); 427 } 428 else 429 { 430 criteria = criteria.addAscendingOrderByColumn(lOrderByField); 431 } 432 } 433 434 User[] tempUsers = TurbineSecurity.getUsers(criteria); 435 for (int i=0; i < tempUsers.length; i++) 436 { 437 userSearchList.add(i, tempUsers[i]); 438 } 439 return (userSearchList); 440 } 441 442 447 public List getModulesFromIssueList(List issues) 448 throws TorqueException 449 { 450 return ModuleManager.getInstancesFromIssueList(issues); 451 } 452 453 public MITListManager getMITListManager() 454 { 455 return MITListManager.getManager(); 456 } 457 458 462 public Date getNow() 463 { 464 return new Date (); 465 } 466 467 474 public Object [] reverse(Object [] a) 475 { 476 Object [] b = new Object [a.length]; 477 for (int i=a.length-1; i>=0; i--) 478 { 479 b[a.length-1-i] = a[i]; 480 } 481 return b; 482 } 483 484 492 public List reverse(List a) 493 { 494 int size = a.size(); 495 List b = new ArrayList (size); 496 for (int i=size-1; i>=0; i--) 497 { 498 b.add(a.get(i)); 499 } 500 return b; 501 } 502 503 515 public List subset(List a, Integer fromIndex, Integer toIndex) 516 { 517 int from = Math.min(fromIndex.intValue(), a.size()); 518 from = Math.max(from, 0); 519 int to = Math.min(toIndex.intValue(), a.size()); 520 to = Math.max(to, from); 521 return a.subList(from, to); 522 } 523 524 533 public Object [] subset(Object [] a, Integer fromIndex, Integer toIndex) 534 { 535 int from = Math.min(fromIndex.intValue(), a.length); 536 from = Math.max(from, 0); 537 int to = Math.min(toIndex.intValue(), a.length); 538 to = Math.max(to, from); 539 Object [] b = new Object [from-to]; 540 for (int i=from-1; i>=to; i--) 541 { 542 b[i-to] = a[i]; 543 } 544 return b; 545 } 546 547 554 public int sizeOfArray(Object [] obj) 555 { 556 return (obj == null) ? -1 : obj.length; 557 } 558 559 public boolean isString(Object obj) 560 { 561 return obj instanceof String ; 562 } 563 564 573 public Enumeration tokenize(String text, String delimiters) 574 { 575 if (delimiters == null || delimiters.length() == 0) 576 { 577 delimiters = "\n"; 578 } 579 580 if (text == null) 581 { 582 text = ""; 583 } 584 585 StringTokenizer st = new StringTokenizer (text, delimiters); 586 return st; 587 } 588 589 public List linkIssueIds(Module module, String text) 590 { 591 List result = null; 592 try 593 { 594 result = IssueIdParser.tokenizeText(module, text); 595 } 596 catch (Exception e) 597 { 598 result = new ArrayList (1); 600 result.add(text); 601 Log.get().warn("Could not linkify text: " + text, e); 602 } 603 return result; 604 } 605 606 public SkipFiltering getCommentText(String text, ScarabLink link, Module currentModule) 607 { 608 return this.textToHTML(text, link, currentModule); 609 } 610 611 623 public SkipFiltering textToHTML(String text, 624 ScarabLink link, 625 Module currentModule) 626 { 627 SkipFiltering sf = null; 628 try 629 { 630 sf = new SimpleSkipFiltering(ScarabUtil.linkifyText(text, 631 link, 632 currentModule)); 633 } 634 catch (Exception e) 635 { 636 sf = new SimpleSkipFiltering(text); 637 } 638 return sf; 639 } 640 641 647 public void log(String s) 648 { 649 LOG.debug(s); 650 } 651 652 659 public void log(String category, String s) 660 { 661 Logger.getLogger(category).debug(s); 662 } 663 664 670 public void print(String s) 671 { 672 System.out.println(s); 673 } 674 675 680 public String getSiteName() 681 { 682 String siteName = 683 Turbine.getConfiguration().getString("scarab.site.name",""); 684 685 if (siteName == null) 686 { 687 siteName = ""; 688 } 689 return siteName; 690 } 691 692 697 public String getSiteLogo() 698 { 699 String siteLogo = 700 Turbine.getConfiguration().getString("scarab.site.logo",""); 701 702 if (siteLogo == null) 703 { 704 siteLogo = ""; 705 } 706 return siteLogo; 707 } 708 709 717 public int getPublicModulesDisplayCount() 718 { 719 String publicModulesDisplayCount = 720 Turbine.getConfiguration().getString("scarab.public.modules.display.count","-1"); 721 return Integer.parseInt(publicModulesDisplayCount); 722 } 723 724 733 public boolean isIssueReasonRequired(Module module) 734 { 735 String key = GlobalParameter.ISSUE_REASON_REQUIRED; 736 boolean result = GlobalParameterManager. 737 getBooleanFromHierarchy(key, module, true); 738 return result; 739 } 740 741 742 750 public int getInt(Object obj) 751 { 752 int result = 0; 753 if (obj != null) 754 { 755 try 756 { 757 result = Integer.parseInt(obj.toString()); 758 } 759 catch (Exception e) 760 { 761 Log.get().error(obj + " cannot convert to an integer.", e); 762 } 763 } 764 return result; 765 } 766 767 public int getCALENDAR_YEAR_FIELD() 768 { 769 return Calendar.YEAR; 770 } 771 772 public int getCALENDAR_MONTH_FIELD() 773 { 774 return Calendar.MONTH; 775 } 776 777 public int getCALENDAR_DAY_FIELD() 778 { 779 return Calendar.DAY_OF_MONTH; 780 } 781 782 public int getCALENDAR_HOUR_FIELD() 783 { 784 return Calendar.HOUR_OF_DAY; 785 } 786 787 public Date addApproxOneHour(Date date) 788 { 789 date.setTime(date.getTime() + 3599999); 790 return date; 791 } 792 793 796 public boolean templateExists(String template) 797 { 798 return ((TurbineVelocityService) TurbineServices 799 .getInstance().getService(VelocityService.SERVICE_NAME)) 800 .templateExists(template); 801 } 802 803 806 public synchronized static int getModuleCodeLength() { 807 if (moduleCodeLength == 0) 808 { 809 try 810 { 811 moduleCodeLength = Integer.parseInt(Turbine.getConfiguration(). 812 getString("scarab.module.code.length", "4")); 813 } 814 catch (Exception e) 815 { 816 e.printStackTrace(); 817 moduleCodeLength = 4; 818 } 819 } 820 return moduleCodeLength; 821 } 822 823 826 public static int getModuleCodeLengthPadded() { 827 return getModuleCodeLength() + 6; 828 } 829 830 833 public org.apache.commons.configuration.Configuration getTurbineConfiguration() 834 { 835 return Turbine.getConfiguration(); 836 } 837 838 } 839 | Popular Tags |