Commits

Vadim Ne. committed 2a12911

Task 3. Small fixes and improvements.

  • Participants
  • Parent commits 2f1e413

Comments (0)

Files changed (5)

ngram-stats-web-view/src/main/java/servlets/ngram/ViewNgram.java

         String order = restParser.getOrder();
 
         int amountOfPages = (int) Math.ceil(dbreader.getAmountOfWordsWithCountersInDatabase(ngramData, patternToFilter) * 1.0 / 10);
-
+        // In the case if input page is wrong or even negative, first page id will be applied by Rest Parser
         if (pageId > amountOfPages) {
-            pageId = amountOfPages; // If the page number exceeds general amount of pages, set up current page id as maximal page id available
+            pageId = amountOfPages;
         }
 
         try {
             } else {
                 listOfWordsForNgram = dbreader.getListOfTenWordsWithCounters(pageId - 1, ngramData, patternToFilter);
             }
-
         } catch (IllegalArgumentException e) {
             listOfWordsForNgram = dbreader.getListOfTenWordsWithCounters(0, ngramData, REGEX_PATTERN_TO_EXTRACT_ALL_WORDS);
         }
             request.setAttribute("paginatorRestQueryParameters", pathPostConditions);
         }
 
+        request.setAttribute("searchInfo", new HashMap<String, String>() {{
+            if (!patternToFilter.equals(".")) {
+                put("searchPlaceholder", patternToFilter);
+            } else {
+                put("searchPlaceholder", "Search among all words for this ngram (partial/regex)");
+            }
+            put("searchAction", "/ngram/" + ngramData + "/");
+        }});
+
         final List<WordForNgramWithCount> finalListOfWordsForNgram = listOfWordsForNgram;
         request.setAttribute("listResult", new HashMap<String, List>() {{
             put("listOfWordsForNgram", finalListOfWordsForNgram);
 
         String wordSearchData = request.getParameter("query");
         RequestDispatcher view;
-        request.setAttribute("searchPlaceholder", "Search among all words for this ngram (partial/regex)");
         try {
             final ViewNgramRest restParser = new ViewNgramRest(request.getRequestURI());
 
             LOGGER.debug("WrongURIException catched");
             response.sendError(HttpServletResponse.SC_NOT_FOUND);
         } catch (Exception e) {
-            LOGGER.debug("Exception catched");
+            LOGGER.debug("Exception catched", e);
             response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
         }
     }

ngram-stats-web-view/src/main/java/servlets/ngrams/ViewNgrams.java

         NgramsDatabaseReaderInterface dbreader = new NgramsJdbcReader();
         int pageId; // = 1 is minimum index at current stage. It's important, that database extracts pages from 0-index
         List<Ngram> listOfNgrams;
-
         String pathConditionsPart = "";
 
         final NgramsViewRest restParser = new NgramsViewRest(request.getRequestURI());
         String orderFieldName = restParser.getOrderByFieldName();
         String order = restParser.getOrder();
 
+        int amountOfPages = (int) Math.ceil(dbreader.getAmountOfNgramsInDatabase() * 1.0 / 10);
+        // In the case if input page is wrong or even negative, first page id will be applied by Rest Parser
+        if (pageId > amountOfPages) {
+            pageId = amountOfPages;
+        }
+
         try {
             if (StringUtils.isNotEmpty(orderFieldName)) {
                 if (StringUtils.isNotEmpty(order)) {
             put("URISortMoulderOrder", restParser.getOrder());
         }});
 
-        request.setAttribute("paginatorAmountOfPages", (int) Math.ceil(dbreader.getAmountOfNgramsInDatabase() * 1.0 / 10));
+        request.setAttribute("paginatorAmountOfPages", amountOfPages);
         request.setAttribute("paginatorCurrentPage", pageId);
         request.setAttribute("paginatorRestCollectionPath", COLLECTION_URL_SERVLET_IS_MAPPED);
         request.setAttribute("paginatorRestQueryParameters", pathConditionsPart);
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
         String ngramSearchData = request.getParameter("query");
-        request.setAttribute("searchPlaceholder", "Search for ngrams (full match)");
+        request.setAttribute("searchInfo", new HashMap<String, String>() {{
+            put("searchPlaceholder", "Search for ngrams (full match)");
+            //put("searchAction", "ngram/${ngramDataString}/1/");
+        }});
 
         RequestDispatcher view;
         try {
                 view.forward(request, response);
             }
         } catch (WrongURIException e) {
+            LOGGER.debug("WrongURIException catched");
             response.sendError(HttpServletResponse.SC_NOT_FOUND);
+        } catch (Exception e) {
+            LOGGER.debug("Exception catched", e);
+            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
         }
     }
 }

ngram-stats-web-view/src/main/webapp/WEB-INF/pages/includes/search.jspf

 <%--
     The universal search component. To use this component you need to include it into page and pass through the model:
     -- searchPlaceholder -- String placeholder to put into search box.
+    <jsp:useBean id="searchPlaceholder" scope="request" type="java.lang.String"/>
+<jsp:useBean id="searchAction" scope="request" type="java.lang.String"/>
 --%>
 
-<jsp:useBean id="searchPlaceholder" scope="request" type="java.lang.String"/>
+<jsp:useBean id="searchInfo" scope="request" type="java.util.Map<java.lang.String,java.lang.String>"/>
 
-<form class="form-search text-center">
+<form class="form-search text-center" action="${searchInfo.searchAction}">
     <div class="input-append">
-        <input name="query" type="text" class="clearable span4 search-query" placeholder="${searchPlaceholder}">
+        <input name="query" type="text" class="clearable span4 search-query" placeholder="${searchInfo.searchPlaceholder}">
         <button type="submit" class="btn"><i class="icon-search"></i></button>
     </div>
 </form>

ngram-stats-web-view/src/main/webapp/WEB-INF/pages/ngram/search-result.jsp

 
 <div class="container" id="container">
     <div class="span6 center-element">
-        <h1>Search result for n-gram: '${ngramDataString}'</h1>
+        <h1>Search result for n-gram: '<a href="<c:url value="/ngram/${ngramDataString}"/>">${ngramDataString}</a>'</h1>
 
         <h1>Word pattern: '${searchWordPatternDataString}'</h1>
         <br/>

ngram-stats-web-view/src/main/webapp/WEB-INF/tags/anchors-buttons/table-sort-icon-generator.tag

 <%--This tag generates anchor (link) and an icon to refer to appropriate tabe sorting url.
     -- sortInfo bean refers to Map<String, String> which contains
         ---- URISortMoulderCurrentCollectionPath -- contain current path info without last slash (e.g. for http://example.com/one/two/, this value may be "/one/two")
-        ---- URISortMoulderCurrentPageNumber -- page to get after the sort will be applied
+        ---- URISortMoulderCurrentPageNumber -- page to get after the sort will be applied ${sortInfo.URISortMoulderCurrentPageNumber}
         ---- URISortMoulderFieldToSort -- name of the field to sort by
         ---- URISortMoulderOrder -- order of sort [asc|desc]
         ---- URISortMoulderAdditionalParameters -- parameters that will be added to the end of generated URL
 <c:choose>
     <c:when test="${sortInfo.URISortMoulderFieldToSort == currentFieldName}">
         <c:if test="${sortInfo.URISortMoulderOrder == 'asc'}"> <%--From asc switch to default sort method.--%>
-            <a href="<c:out value="${sortInfo.URISortMoulderCurrentCollectionPath}/${sortInfo.URISortMoulderCurrentPageNumber}/${sortInfo.URISortMoulderAdditionalParameters}"/>">
+            <a href="<c:out value="${sortInfo.URISortMoulderCurrentCollectionPath}/1/${sortInfo.URISortMoulderAdditionalParameters}"/>">
                 <i class="icon-chevron-up element-glow"></i>
             </a>
         </c:if>
         <c:if test="${sortInfo.URISortMoulderOrder == 'desc'}"> <%--From desc switch to asc sort method.--%>
-            <a href="<c:out value="${sortInfo.URISortMoulderCurrentCollectionPath}/${sortInfo.URISortMoulderCurrentPageNumber}/${currentFieldName}/asc/${sortInfo.URISortMoulderAdditionalParameters}"/>">
+            <a href="<c:out value="${sortInfo.URISortMoulderCurrentCollectionPath}/1/${currentFieldName}/asc/${sortInfo.URISortMoulderAdditionalParameters}"/>">
                 <i class="icon-chevron-down element-glow"></i>
             </a>
         </c:if>
     </c:when>
     <c:otherwise> <%--From default switch to desc sort method.--%>
-        <a href="<c:out value="${sortInfo.URISortMoulderCurrentCollectionPath}/${sortInfo.URISortMoulderCurrentPageNumber}/${currentFieldName}/desc/${sortInfo.URISortMoulderAdditionalParameters}"/>">
+        <a href="<c:out value="${sortInfo.URISortMoulderCurrentCollectionPath}/1/${currentFieldName}/desc/${sortInfo.URISortMoulderAdditionalParameters}"/>">
             <i class="icon-list element-glow"></i>
         </a>
     </c:otherwise>