View Javadoc

1   package com.atlassian.user.impl.delegation.search.query;
2   
3   import com.atlassian.user.EntityException;
4   import com.atlassian.user.search.DefaultSearchResult;
5   import com.atlassian.user.search.SearchResult;
6   import com.atlassian.user.search.query.AllRepositoriesQueryContext;
7   import com.atlassian.user.search.query.EntityQueryParser;
8   import com.atlassian.user.search.query.Query;
9   import com.atlassian.user.search.query.QueryContext;
10  import org.apache.log4j.Logger;
11  
12  import java.util.Iterator;
13  import java.util.List;
14  
15  public class DelegatingEntityQueryParser implements EntityQueryParser
16  {
17      private static final Logger log = Logger.getLogger(DelegatingEntityQueryParser.class);
18      private final List entityQueryParsers;
19  
20      public DelegatingEntityQueryParser(List entityQueryParsers)
21      {
22          this.entityQueryParsers = entityQueryParsers;
23      }
24  
25      /**
26       * Generates a {@link com.atlassian.user.search.SearchResult} object for each {@link com.atlassian.user.repository.RepositoryIdentifier} the
27       * entityQueryParser can search. These are grouped in an array and returned.
28       * <p/>
29       * Thus, if the entityQueryParser only knows about one configuration, the array
30       * will either have a size of 0 (no sucesses) or 1.
31       */
32      public SearchResult findUsers(Query query) throws EntityException
33      {
34          return findUsers(query, new AllRepositoriesQueryContext());
35      }
36  
37      /**
38       * Generates a {@link com.atlassian.user.search.SearchResult} object for each {@link com.atlassian.user.repository.RepositoryIdentifier} the
39       * entityQueryParser can search. These are grouped in an array and returned.
40       * <p/>
41       * Thus, if the entityQueryParser only knows about one configuration, the array
42       * will either have a size of 0 (no sucesses) or 1.
43       */
44      public SearchResult findGroups(Query query) throws EntityException
45      {
46          return findGroups(query, new AllRepositoriesQueryContext());
47      }
48  
49      public SearchResult findUsers(Query query, QueryContext context) throws EntityException
50      {
51          Iterator iter = entityQueryParsers.iterator();
52          DefaultSearchResult result = new DefaultSearchResult();
53          boolean withinContext = false;
54  
55          while (iter.hasNext())
56          {
57              EntityQueryParser parser = (EntityQueryParser) iter.next();
58  
59              SearchResult returned = null;
60  
61              try
62              {
63                  returned = parser.findUsers(query, context);
64              }
65              catch (EntityException e)
66              {
67                  log.info(e.getMessage());
68              }
69  
70              if (returned != null)
71              {
72                  withinContext = true;
73                  Iterator keysIter = returned.repositoryKeyset().iterator();
74  
75                  while (keysIter.hasNext())
76                  {
77                      String repokey = (String) keysIter.next();
78                      result.addToResults(repokey, returned.pager(repokey));
79                  }
80              }
81          }
82  
83          // if the search was not within the context, return null as per the method contract
84          return (withinContext) ? result : null;
85      }
86  
87      public SearchResult findGroups(Query query, QueryContext context) throws EntityException
88      {
89          Iterator iter = entityQueryParsers.iterator();
90          DefaultSearchResult result = new DefaultSearchResult();
91          boolean withinContext = false;
92  
93          while (iter.hasNext())
94          {
95              EntityQueryParser parser = (EntityQueryParser) iter.next();
96  
97              SearchResult returned = null;
98  
99              try
100             {
101                 returned = parser.findGroups(query, context);
102             }
103             catch (EntityException e)
104             {
105                 log.info(e.getMessage());
106             }
107 
108             if (returned != null)
109             {
110                 withinContext = true;
111                 Iterator keysIter = returned.repositoryKeyset().iterator();
112 
113                 while (keysIter.hasNext())
114                 {
115                     String repokey = (String) keysIter.next();
116                     result.addToResults(repokey, returned.pager(repokey));
117                 }
118             }
119         }
120 
121         // if the search was not within the context, return null as per the method contract
122         return (withinContext) ? result : null;
123     }
124 }