*
* @param params the request params from a request (do not include headers)
* @return a search filter object
*/
public static Search makeSearchFromRequestParams(Map<String, Object> params) {
Search search = new Search();
int page = -1;
int limit = -1;
try {
if (params != null) {
for (Entry<String, Object> entry : params.entrySet()) {
String key = entry.getKey();
// filter out certain keys
if (getIgnoreSet().contains(key)) {
continue; // skip this key
}
Object value = entry.getValue();
if (value == null) {
// in theory this should not happen
continue;
} else if (value.getClass().isArray()) {
// use the value as is
} else {
// get paging values out if possible
if ("_limit".equals(key)
|| "_perpage".equals(key)
|| "perpage".equals(key)
|| "count".equals(key)
|| "itemsPerPage".equals(key)) {
try {
limit = Integer.valueOf(value.toString()).intValue();
search.setLimit(limit);
} catch (NumberFormatException e) {
System.out.println("WARN Invalid non-number passed in for _limit/_perpage param: " + value + ":" + e);
}
continue;
} else if ("_start".equals(key)
|| "startIndex".equals(key)) {
try {
int start = Integer.valueOf(value.toString()).intValue();
search.setStart(start);
} catch (NumberFormatException e) {
System.out.println("WARN Invalid non-number passed in for '_start' param: " + value + ":" + e);
}
continue;
} else if ("_page".equals(key)
|| "page".equals(key)
|| "startPage".equals(key)) {
try {
page = Integer.valueOf(value.toString()).intValue();
} catch (NumberFormatException e) {
System.out.println("WARN Invalid non-number passed in for '_page' param: " + value + ":" + e);
}
continue;
} else if ("_order".equals(key)
|| "_sort".equals(key)
|| "sort".equals(key)) {
if (value != null) {
String val = value.toString();
String[] sortBy = new String[] {val};
if (val.indexOf(',') > 0) {
// multiple sort params
sortBy = val.split(",");
}
try {
for (int i = 0; i < sortBy.length; i++) {
String sortItem = sortBy[i].trim();
if (sortItem.endsWith("_reverse")) {
search.addOrder( new Order(sortItem.substring(0, sortItem.length()-8), false) );
} else if (sortItem.endsWith("_desc")) {
search.addOrder( new Order(sortItem.substring(0, sortItem.length()-5), false) );
} else if (sortItem.endsWith("_asc")) {
search.addOrder( new Order(sortItem.substring(0, sortItem.length()-4)) );
} else {
search.addOrder( new Order(sortItem) );
}
}
} catch (RuntimeException e) {
System.out.println("WARN Failed while getting the sort/order param: " + val + ":" + e);
}
}
continue;
} else if ("_searchTerms".equals(key)
|| "searchTerms".equals(key)) {
// indicates a space delimited list of search terms
if (value != null) {
String val = value.toString();
String[] terms = val.split(" ");
search.addRestriction( new Restriction("searchTerms", terms) );
}
continue;
}
}
search.addRestriction( new Restriction(key, value) );
}
}
} catch (Exception e) {
// failed to translate the request to a search, not really much to do here
System.out.println("WARN Could not translate entity request into search params: " + e.getMessage() + ":" + e);
}
// translate page into start/limit
if (page > 0) {
if (limit <= -1) {
limit = 10; // set to a default value
search.setLimit(limit);
System.out.println("WARN page is set without a limit per page, setting per page limit to default value of 10");
}
search.setStart( (page-1) * limit );
}
return search;
}