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
79
80
81 public static String extractSearchResultName(String distinguishedName)
82 {
83 String result = distinguishedName;
84
85 if (!TextUtils.stringSet(distinguishedName))
86 return result;
87
88 distinguishedName = distinguishedName.replaceAll("\\\\,", ESCAPEDCOMMA);
89 String[] rdns = distinguishedName.split(",");
90
91 boolean invalidDN = true;
92 if (rdns.length >= 1)
93 {
94 String[] firstPhrase = rdns[0].split("=");
95 if (firstPhrase.length >= 2)
96 {
97 result = firstPhrase[1].replaceAll(ESCAPEDCOMMA, "\\\\,");
98 invalidDN = false;
99 }
100 }
101
102 if (log.isDebugEnabled() && invalidDN)
103 log.debug("Could not extract name from search result: " + distinguishedName);
104
105 return result;
106 }
107 }