io.robe.common.service.search.SearchFactory Maven / Gradle / Ivy
package io.robe.common.service.search;
import io.robe.common.service.search.model.SearchModel;
import org.glassfish.jersey.server.internal.inject.AbstractContainerRequestValueFactory;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;
import java.util.List;
import java.util.Map;
public class SearchFactory extends AbstractContainerRequestValueFactory {
@Context
private UriInfo uriInfo;
@Context
private HttpServletResponse response;
public SearchFactory() {
}
/**
* implemented just to GET method.
*
* @return SearchModel
*/
@Override
public SearchModel provide() {
SearchModel searchModel = new SearchModel();
searchModel.setResponse(response);
String method = getContainerRequest().getMethod();
if ("GET".equals(method)) {
MultivaluedMap queryParameters = uriInfo.getQueryParameters();
for (Map.Entry> param : queryParameters.entrySet()) {
if (param.getValue().get(0) == null)
continue;
if ("_q".equalsIgnoreCase(param.getKey())) {
searchModel.setQ(param.getValue().get(0));
} else if ("_limit".equalsIgnoreCase(param.getKey())) {
searchModel.setLimit(Integer.parseInt(param.getValue().get(0)));
} else if ("_offset".equalsIgnoreCase(param.getKey())) {
searchModel.setOffset(Integer.parseInt(param.getValue().get(0)));
} else if ("_fields".equalsIgnoreCase(param.getKey())) {
if (param.getValue().get(0) != null)
searchModel.setFields(param.getValue().get(0).split(","));
} else if ("_sort".equalsIgnoreCase(param.getKey())) {
searchModel.setSort(param.getValue().get(0).split(","));
} else if ("_filter".equalsIgnoreCase(param.getKey())) {
searchModel.setFilter(param.getValue().get(0));
}
}
}
return searchModel;
}
}