View Javadoc

1   package com.atlassian.user.search.page;
2   
3   import com.atlassian.user.Entity;
4   import com.opensymphony.util.TextUtils;
5   import org.apache.log4j.Category;
6   
7   import java.util.ArrayList;
8   import java.util.Iterator;
9   import java.util.List;
10  
11  /***
12   * Util class for making it easier to work with paging iterators
13   *
14   * TODO: Roll these methods into an extension of the Pager interface
15   */
16  public class PagerUtils
17  {
18      public static final Category log = Category.getInstance(PagerUtils.class);
19      public static final Pager EMPTY_PAGER = Pager.EMPTY_PAGER;
20      public static final String ESCAPEDCOMMA = "ESCAPEDCOMMA";
21  
22      public static <T> List<T> toList(Pager<T> pager)
23      {
24          ArrayList<T> list = new ArrayList<T>();
25  
26          for (T obj : pager)
27          {
28              list.add(obj);
29          }
30  
31          return list;
32      }
33  
34      public static int count(Pager pager)
35      {
36          Iterator iter = pager.iterator();
37  
38          int count = 0;
39  
40          while (iter.hasNext())
41          {
42              iter.next();
43              count++;
44          }
45  
46         return count;
47      }
48  
49      /**
50       * @deprecated since 1.25. In general, the Pager should not be used wherever possible. Manually extract entity names
51       * from the Pager itself if it is still required. 
52       */
53      @SuppressWarnings("unchecked") /* not genericised because it's deprecated */
54      public static List toListOfEntityNames(Pager pager)
55      {
56          ArrayList list = new ArrayList();
57  
58          for (Object aPager : pager)
59          {
60              Entity e = (Entity) aPager;
61              if (e == null)
62              {
63                  log.error("null entity in pager");
64              }
65              else
66              {
67                  list.add(e.getName());
68              }
69          }
70  
71          return list;
72      }
73  
74      /**
75       * For example, for cn=jsmith,dc=atlassian,dc=com this returns jsmith.
76       * If the distinguishedName is not well formed, this will be logged and it will be returned as is
77       */
78      public static String extractSearchResultName(String distinguishedName)
79      {
80          String result = distinguishedName;
81  
82          if (!TextUtils.stringSet(distinguishedName))
83              return result;
84  
85          distinguishedName = distinguishedName.replaceAll("\\\\,", ESCAPEDCOMMA);
86          String[] rdns = distinguishedName.split(",");
87  
88          boolean invalidDN = true;
89          if (rdns.length >= 1)
90          {
91              String[] firstPhrase = rdns[0].split("=");
92              if (firstPhrase.length >= 2)
93              {
94                  result = firstPhrase[1].replaceAll(ESCAPEDCOMMA, "\\\\,");
95                  invalidDN = false;
96              }
97          }
98  
99          if (log.isDebugEnabled() && invalidDN)
100             log.debug("Could not extract name from search result: " + distinguishedName);
101 
102         return result;
103     }
104 }