Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../img/srcFileCovDistChart0.png 48% of files have more coverage
60   189   13   15
10   123   0,22   4
4     3,25  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  SearchAPIHandler       Line # 27 60 0% 13 74 0% 0.0
 
No Tests
 
1   
2    package net.sourceforge.pebble.webservice;
3   
4    import java.util.Hashtable;
5    import java.util.Iterator;
6    import java.util.List;
7    import java.util.Map;
8    import java.util.Vector;
9   
10    import net.sourceforge.pebble.domain.Blog;
11    import net.sourceforge.pebble.domain.BlogEntry;
12    import net.sourceforge.pebble.domain.BlogService;
13    import net.sourceforge.pebble.domain.Category;
14    import net.sourceforge.pebble.search.SearchHit;
15    import net.sourceforge.pebble.search.SearchResults;
16    import net.sourceforge.pebble.util.Pageable;
17   
18    import org.apache.commons.logging.Log;
19    import org.apache.commons.logging.LogFactory;
20   
21   
22    /**
23    * Extension to allow search and query of Blog posting via XML-RPC.
24    *
25    * @author tcp@favoritemedium.com
26    */
 
27    public class SearchAPIHandler extends AbstractAPIHandler {
28   
29    static final String URL = "url";
30    static final String BLOG_ID = "blogid";
31    static final String BLOG_NAME = "blogName";
32    static final String DATE_CREATED = "dateCreated";
33    static final String USER_ID = "userId";
34    static final String POST_ID = "postid";
35    static final String CONTENT = "content";
36   
37    static final String TITLE_START_DELIMITER = "<title>";
38    static final String TITLE_END_DELIMITER = "</title>";
39    static final String CATEGORY_START_DELIMITER = "<category>";
40    static final String CATEGORY_END_DELIMITER = "</category>";
41    static final char BLOG_ID_SEPARATOR = '/';
42   
43    static final int PAGE_SIZE = 20;
44   
45   
46    /** the log used by this class */
47    private static Log log = LogFactory.getLog(SearchAPIHandler.class);
48   
49   
50    /**
51    * Search blog for specific string.
52    *
53    * @param blogid the ID of the blog (ignored)
54    * @param username the username used for logging in via XML-RPC
55    * @param password the password used for logging in via XML-RPC
56    */
 
57  0 toggle public Vector search(String blogid, String username, String password,
58    String searchString, String sortBy) {
59  0 log.debug("search.search(" +
60    blogid + ", " +
61    username + ", xxxxxx, \"" +
62    searchString + "," +
63    sortBy + "\")");
64   
65  0 Vector posts = new Vector();
66  0 try {
67   
68  0 Blog blog = getBlogWithBlogId(blogid);
69  0 SearchResults result = blog.getSearchIndex().search( searchString );
70   
71  0 if ( sortBy != null && sortBy.equalsIgnoreCase("date") ) {
72  0 result.sortByDateDescending();
73    } else {
74  0 result.sortByScoreDescending();
75    }
76   
77  0 List<SearchHit> hits = result.getHits();
78  0 BlogService service = new BlogService();
79   
80  0 for (SearchHit hit : hits) {
81  0 BlogEntry entry = service.getBlogEntry(hit.getBlog(), hit.getId());
82  0 adaptBlogEntry(entry);
83  0 posts.add(adaptBlogEntry(entry));
84    }
85  0 posts.add( searchResultSummary(hits, sortBy, searchString, 0, 0) );
86   
87    } catch (Exception ex) {
88  0 log.error(ex);
89    }
90  0 return posts;
91    }
92   
93    /**
94    * Search blog for specific string with pagable parameters
95    *
96    * @param blogid the ID of the blog (ignored)
97    * @param username the username used for logging in via XML-RPC
98    * @param password the password used for logging in via XML-RPC
99    */
 
100  0 toggle public Vector search(String blogid, String username, String password,
101    String searchString, String sortBy, int pageSize, int offset) {
102  0 log.debug("search.search(" +
103    blogid + ", " +
104    username + ", xxxxxx, \"" +
105    searchString + "," +
106    sortBy + "\")");
107   
108   
109  0 Vector posts = new Vector();
110  0 try {
111   
112  0 Blog blog = getBlogWithBlogId(blogid);
113  0 SearchResults result = blog.getSearchIndex().search( searchString );
114   
115  0 if ( sortBy != null && sortBy.equalsIgnoreCase("date") ) {
116  0 result.sortByDateDescending();
117    } else {
118  0 result.sortByScoreDescending();
119    }
120   
121  0 if ( pageSize <= 0 )
122  0 pageSize = PAGE_SIZE;
123   
124  0 List<SearchHit> hits = result.getHits();
125  0 Pageable pageable = new Pageable(hits);
126  0 pageable.setPageSize(pageSize);
127  0 pageable.setPage(offset);
128  0 List<SearchHit> subList = pageable.getListForPage();
129   
130  0 BlogService service = new BlogService();
131   
132  0 for (SearchHit hit : subList ) {
133  0 BlogEntry entry = service.getBlogEntry(hit.getBlog(), hit.getId());
134  0 adaptBlogEntry(entry);
135  0 posts.add(adaptBlogEntry(entry));
136    }
137  0 posts.add( searchResultSummary(subList, sortBy, searchString, pageSize, offset) );
138   
139    } catch (Exception ex) {
140  0 log.error(ex);
141    }
142  0 return posts;
143    }
144   
145    /**
146    * Helper method to adapt a blog entry into an XML-RPC compatible struct.
147    * Since the Blogger API doesn't support titles, the title is wrapped in
148    * &lt;title&gt;&lt;/title&gt; tags.
149    *
150    * @param entry the BlogEntry to adapt
151    * @return a Hashtable representing the major properties of the entry
152    */
 
153  0 toggle private Hashtable adaptBlogEntry(BlogEntry entry) {
154  0 Hashtable post = new Hashtable();
155  0 String categories = "";
156  0 Iterator it = entry.getCategories().iterator();
157  0 while (it.hasNext()) {
158  0 Category category = (Category)it.next();
159  0 categories += category.getId();
160  0 if (it.hasNext()) {
161  0 categories += ",";
162    }
163    }
164  0 post.put(DATE_CREATED, entry.getDate());
165  0 post.put(USER_ID, entry.getAuthor());
166  0 post.put(POST_ID, formatPostId(entry.getBlog().getId(), entry.getId()));
167  0 post.put(CONTENT, TITLE_START_DELIMITER + entry.getTitle() + TITLE_END_DELIMITER
168    + CATEGORY_START_DELIMITER + categories + CATEGORY_END_DELIMITER + entry.getBody());
169   
170  0 return post;
171    }
172   
173    /**
174    * Auto create a Map of search result summary.
175    * @param result of the search hits.
176    */
 
177  0 toggle private Map searchResultSummary(List<SearchHit> result, String sortBy,
178    String query, int pageIndex, int offset) {
179  0 Map data = new Hashtable();
180  0 data.put("size", result.size() );
181  0 data.put("sortBy", sortBy);
182  0 data.put("index", pageIndex);
183  0 data.put("offset", offset);
184  0 data.put("query", query);
185   
186  0 return data;
187    }
188   
189    }