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
13
14
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
51
52
53 @SuppressWarnings("unchecked")
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
76
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 }