| 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 |
|
|
| 24 |
|
|
| 25 |
|
@author |
| 26 |
|
|
|
|
|
| 0% |
Uncovered Elements: 74 (74) |
Complexity: 13 |
Complexity Density: 0,22 |
|
| 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 |
|
|
| 47 |
|
private static Log log = LogFactory.getLog(SearchAPIHandler.class); |
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
@param |
| 54 |
|
@param |
| 55 |
|
@param |
| 56 |
|
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 4 |
Complexity Density: 0,24 |
|
| 57 |
0
|
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 |
|
|
| 95 |
|
|
| 96 |
|
@param |
| 97 |
|
@param |
| 98 |
|
@param |
| 99 |
|
|
|
|
|
| 0% |
Uncovered Elements: 27 (27) |
Complexity: 5 |
Complexity Density: 0,22 |
|
| 100 |
0
|
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 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
|
@param |
| 151 |
|
@return |
| 152 |
|
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 3 |
Complexity Density: 0,23 |
|
| 153 |
0
|
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 |
|
|
| 175 |
|
@param |
| 176 |
|
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 1 |
Complexity Density: 0,14 |
|
| 177 |
0
|
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 |
|
} |