Commit a352b9ea authored by Michael Ritter's avatar Michael Ritter
Browse files

Add pagination and basic searching to the Status servlet

parent 478d663a
......@@ -30,18 +30,22 @@
// $Id$
package edu.umiacs.ace.monitor.access;
import edu.umiacs.ace.util.EntityManagerServlet;
import edu.umiacs.ace.monitor.core.Collection;
import edu.umiacs.ace.monitor.support.PageBean;
import edu.umiacs.ace.util.EntityManagerServlet;
import edu.umiacs.util.Strings;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
*
......@@ -49,10 +53,31 @@ import javax.servlet.http.HttpServletResponse;
*/
public class StatusServlet extends EntityManagerServlet {
private static final Logger LOG = Logger.getLogger(StatusServlet.class);
private static final String PAGE_COLLECTIONS = "collections";
private static final String PAGE_COUNT = "count";
private static final String PAGE_NUMBER = "page";
private static final long DEFAULT_PAGE = 0;
private static final int DEFAULT_COUNT = 20;
private static final String SESSION_WORKINGCOLLECTION = "workingCollection";
private static final String PARAM_CSV = "csv";
// Pagination stuff
private static final String PARAM_COUNT = "count";
private static final String PARAM_PAGE = "page";
// Search Params
private static final String PARAM_GROUP = "group";
private static final String PARAM_COLLECTION_LIKE = "collection";
private static final String PARAM_AUDIT_DATE = "audit";
// Filter params?
// ...group
// ...name (maybe some regex?)
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
......@@ -65,14 +90,90 @@ public class StatusServlet extends EntityManagerServlet {
RequestDispatcher dispatcher;
List<CollectionSummaryBean> collections;
List<Collection> items;
LOG.info("Processing /Status request");
LOG.info("Processing /Status additional info");
long page = getParameter(request, PARAM_PAGE, DEFAULT_PAGE);
int count = (int) getParameter(request, PARAM_COUNT, DEFAULT_COUNT);
String group = getParameter(request, PARAM_GROUP, null);
String collection = getParameter(request, PARAM_COLLECTION_LIKE, null);
// String date = getParameter(request, PARAM_GROUP, null);
PageBean pb = new PageBean((int) page, count, "Status");
long offset = page * count;
// our main query
StringBuilder queryString = new StringBuilder();
// for getting a count of items
StringBuilder countString = new StringBuilder();
// our parameters
StringBuilder params = new StringBuilder();
List<String> queries = new ArrayList<>();
if (!Strings.isEmpty(group)) {
queries.add("c.group = :group");
pb.addParam(PARAM_GROUP, group);
}
if (!Strings.isEmpty(collection)) {
queries.add("c.name LIKE :collection");
pb.addParam(PARAM_COLLECTION_LIKE, collection);
}
queryString.append("SELECT c FROM Collection c");
countString.append("SELECT COUNT(c.id) FROM Collection c");
if (queries.size() > 0) {
params.append(" WHERE");
}
Iterator<String> it = queries.iterator();
while (it.hasNext()) {
String query = it.next();
params.append(" ")
.append(query);
if (it.hasNext()) {
params.append(" AND");
}
}
queryString.append(params);
// allows us to keep a consistent order when displaying collections
queryString.append(" ORDER BY c.group ASC, c.name ASC");
countString.append(params);
Query query =
em.createNamedQuery("Collection.listAllCollections");
em.createQuery(queryString.toString());
// em.createNamedQuery("Collection.listAllCollections");
query.setFirstResult((int) offset);
query.setMaxResults(count);
Query countQuery = em.createQuery(countString.toString());
if (!Strings.isEmpty(group)) {
query.setParameter(PARAM_GROUP, group);
countQuery.setParameter(PARAM_GROUP, group);
}
if (!Strings.isEmpty(collection)) {
query.setParameter(PARAM_COLLECTION_LIKE, "%" + collection + "%");
countQuery.setParameter(PARAM_COLLECTION_LIKE, "%" + collection + "%");
}
items = query.getResultList();
collections = new ArrayList<CollectionSummaryBean>();
// We only need to execute this query if we have parameters
// and need to update the total count of collections
if (queries.size() > 0) {
long totalResults = (long) countQuery.getSingleResult();
LOG.info("Total results from query: " + totalResults);
pb.update(totalResults);
}
collections = new ArrayList<>();
if ( Strings.isValidLong(request.getParameter(PARAM_COLLECTION_ID)) && -1 == Long.parseLong(request.getParameter(
PARAM_COLLECTION_ID)) ) {
......@@ -109,6 +210,8 @@ public class StatusServlet extends EntityManagerServlet {
}
request.setAttribute(PAGE_COLLECTIONS, collections);
request.setAttribute(PAGE_COUNT, count);
request.setAttribute(PAGE_NUMBER, pb);
if ( hasJson(request) ) {
dispatcher = request.getRequestDispatcher("status-json.jsp");
} else if ( hasCsv(request) ) {
......
package edu.umiacs.ace.monitor.support;
import edu.umiacs.ace.monitor.access.CollectionCountContext;
import org.apache.log4j.Logger;
/**
*
* Created by shake on 1/28/16.
*/
public class PageBean {
private static Logger LOG = Logger.getLogger(PageBean.class);
// current page
int page;
// page number
int count;
// item offset (for db queries)
int offset;
int previous;
int next;
int end;
StringBuilder url;
private boolean firstParam;
public PageBean(int page, int count, String root) {
this.firstParam = true;
this.page = page;
this.count = count;
this.offset = page*count;
if (page == 0) {
this.previous = 0;
} else {
this.previous = page - 1;
}
setPages(CollectionCountContext.getTotalCollections());
this.url = new StringBuilder(root);
LOG.info("Next page is " + next);
}
private void setPages(int totalCollections) {
double endPrecise = totalCollections / (double) count;
double endRounded = Math.floor(endPrecise);
// If divided evenly, the last page will have nothing on it
// Else keep it to show the remainder
if (endRounded >= endPrecise) {
this.end =(int) endRounded - 1;
} else {
this.end = (int) endRounded;
}
if (page == end) {
this.next = end;
} else {
this.next = page + 1;
}
}
public void setUrl(StringBuilder url) {
this.url = url;
}
public void addParam(String param, String value) {
if (firstParam) {
url.append("?");
firstParam = false;
} else {
url.append("&");
}
url.append(param).append("=").append(value);
}
public int getPage() {
return page;
}
public int getCount() {
return count;
}
public int getOffset() {
return offset;
}
public String getFirst() {
return getUrl(0, count);
}
public String getPrevious() {
return getUrl(previous, count);
}
public String getNext() {
return getUrl(next, count);
}
public String getEnd() {
return getUrl(end, count);
}
public String getCount(int count) {
return getUrl(0, count);
}
private String getUrl(int page, int count) {
StringBuilder copy = new StringBuilder(url);
if (firstParam) {
copy.append("?");
} else {
copy.append("&");
}
copy.append("page=").append(page);
copy.append("&count=").append(count);
return copy.toString();
}
public void update(long totalResults) {
setPages((int) totalResults);
}
}
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib tagdir="/WEB-INF/tags" prefix="h" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@taglib uri="/WEB-INF/tlds/monitor" prefix="d"%>
......@@ -80,10 +80,14 @@
background-color: #FFFFFF;
}
#linktable {
width: 655px;
margin-left: auto;
margin-right: auto;
}
#threaddetailsTD:hover div {
display: block;
}
#threaddetailsDIV {
......@@ -128,6 +132,49 @@
{
background-color: #e8e8e8;
}
#searchtable {
margin-top: 10px;
margin-bottom: -10px;
margin-left: auto;
margin-right: auto;
width: 650px;
}
.input {
padding: 2px;
display: flex;
width: 650px;
}
.input-group-addon {
border: 1px solid #ccc;
font-size: 12px;
background-color: #e8e8e8;
text-align: center;
width: 75px;
height: 20px;
line-height: 20px;
padding: 3px 10px;
}
.form-input {
border: 1px solid #ccc;
width: 100%;
height: 20px;
padding: 3px 8px;
margin-left: -1px;
margin-right: 5px;
}
.btn {
padding: 2px;
width: 75px;
height: 25px;
border: 1px solid #e8e8e8;
margin-left: 2px;
margin-top: 2px;
}
</style>
</head>
......@@ -141,6 +188,20 @@
</c:if>
<div id="searchtable">
<form method="GET" role="form">
<div class="input">
<span class="input-group-addon">Group</span>
<input type="text" class="form-input" id="group-filter" name="group" placeholder="Search Group"/>
</div>
<div class="input">
<span class="input-group-addon">Collection</span>
<input type="text" class="form-input" id="coll-filter" name="collection" placeholder="Search Collection"/>
</div>
<button type="submit" class="btn" value="Submit"><span>Submit</span></button>
</form>
</div>
<table id="statustable">
<thead>
<td></td><td width="45%">Collection Name</td>
......@@ -257,6 +318,30 @@
</table>
<table id="linktable">
<tr>
<td align="left">
<%-- <a href="Status?count=${count}">|&lt;</a>&nbsp;&nbsp;&nbsp;
<a href="Status?page=${page - 1}&count=${count}">&lt;&lt;</a> --%>
<a href="${page.first}">|&lt;</a>&nbsp;&nbsp;&nbsp;
<a href="${page.previous}">&lt;&lt;</a>
</td>
<td align="center">
Show per page:
<a href="${page.getCount(20)}">20</a>
<a href="${page.getCount(50)}">50</a>
<a href="${page.getCount(100)}">100</a>
</td>
<td align="right">
<%--
<a href="Status?page=${page + 1}&count=${count}">&gt;&gt;</a>&nbsp;&nbsp;&nbsp;
<a href="Status?count=${count}&start=0&top=0">&gt;|</a> --%>
<a href="${page.next}">&gt;&gt;</a>&nbsp;&nbsp;&nbsp;
<a href="${page.end}">&gt;|</a>
</td>
</tr>
</table>
<jsp:include page="footer.jsp" />
</body>
</html>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment