* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
getServletContext().log("-> QuerySvr.doGet()");
JsonQueryData queryData = null;
try {
queryData = JsonQueryData.decode(request.getInputStream());
} catch (Exception ex) {
getServletContext().log("Cannot get query parameters", ex);
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
EntityManager em = ModelFactory.getEntityManager();
EntityTransaction tx = null;
try {
tx = em.getTransaction();
tx.begin();
List<Image> images = null;
// Prepare dynamic query string
String queryString;
String attributes[] = queryData.getAttributes();
int attributeNbr = (attributes.length > maxAttributeNbr ? maxAttributeNbr
: attributes.length);
// Pickup query string start
queryString = QUERIES[attributeNbr];
int paramIx = attributeNbr;
// append rating query
if (queryData.minRating >= 0) {
if (paramIx == 0)
queryString += WHERE_RATING_QUERY + (++paramIx);
else
queryString += AND_RATING_QUERY + (++paramIx);
}
// append from date query
if (queryData.getFromDate() != null) {
if (paramIx == 0)
queryString += WHERE_FROM_QUERY + (++paramIx);
else
queryString += AND_FROM_QUERY + (++paramIx);
}
// append to date query
if (queryData.getToDate() != null) {
if (paramIx == 0)
queryString += WHERE_TO_QUERY + (++paramIx);
else
queryString += AND_TO_QUERY + (++paramIx);
}
getServletContext().log("Query string: " + queryString);
// Pass parameters to query
paramIx = 0;
try {
TypedQuery<Image> query = em.createQuery(queryString,
Image.class);
for (String attribute : attributes) {
query.setParameter(++paramIx, attribute);
}
if (queryData.minRating >= 0) {
query.setParameter(++paramIx, queryData.minRating);
}
if (queryData.getFromDate() != null) {
query.setParameter(++paramIx, queryData.getFromDate());
}
if (queryData.getToDate() != null) {
query.setParameter(++paramIx, queryData.getToDate());
}
if (queryData.getMaxQueryLength() != null) {
query.setMaxResults(queryData.getMaxQueryLength());
}
images = query.getResultList();
} catch (NoResultException ex) {
getServletContext().log("No image found: ", ex);
// create an empty list