Commits

Vadim Ne. committed 0932562

Task 3. Validation exception fixes. Additional type conversion to service level.

Comments (0)

Files changed (16)

database-services-api/src/main/java/dtConverters/NgramsListDaoToDto.java

+package dtConverters;
+
+import dtObjects.Ngram;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class NgramsListDaoToDto {
+    public static List<Ngram> convert(List<dao.entities.dbTables.Ngram> listToConvert) {
+        List<Ngram> listToReturn = new ArrayList<Ngram>();
+        for (dao.entities.dbTables.Ngram element : listToConvert) {
+            listToReturn.add(new Ngram(element.getData(), element.getCount()));
+        }
+
+        return listToReturn;
+    }
+}

database-services-api/src/main/java/dtConverters/WordsListDaoToDto.java

+package dtConverters;
+
+import dtObjects.WordForNgramWithCount;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class WordsListDaoToDto {
+    public static List<WordForNgramWithCount> convert(List<dao.entities.logical.WordForNgramWithCount> listToConvert) {
+        List<WordForNgramWithCount> listToReturn = new ArrayList<WordForNgramWithCount>();
+        for (dao.entities.logical.WordForNgramWithCount element : listToConvert) {
+            listToReturn.add(new WordForNgramWithCount(element.getWord(), element.getCount()));
+        }
+
+        return listToReturn;
+    }
+}

database-services-api/src/main/java/dtObjects/Ngram.java

+package dtObjects;
+
+/**
+ * It's supposed that object gets validated data. Object doesn't do validation.
+ */
+public class Ngram {
+    private final String data;
+    private final int count;
+
+    public Ngram(String data, int count) {
+        this.data = data;
+        this.count = count;
+    }
+
+    public String getData() {
+        return data;
+    }
+
+    public int getCount() {
+        return count;
+    }
+}

database-services-api/src/main/java/dtObjects/WordForNgramWithCount.java

+package dtObjects;
+
+/**
+ * It's supposed that object gets validated data. Object doesn't do validation.
+ */
+public class WordForNgramWithCount {
+    private final String word;
+    private final int count;
+
+    public WordForNgramWithCount(String word, int count) {
+        this.word = word;
+        this.count = count;
+    }
+
+    public String getWord() {
+        return word;
+    }
+
+    public int getCount() {
+        return count;
+    }
+}

database-services-api/src/main/java/queryObjects/NgramsListQueryObject.java

             return this;
         }
 
-        public NgramsListQueryObject build() {
+        public NgramsListQueryObject build() throws QueryObjectBuilderValidationError {
             if (amount < 1) {
-                throw new IllegalStateException("Amount of items in list is negative or zero.");
+                throw new QueryObjectBuilderValidationError("Amount of items in list is negative or zero.");
             }
 
             if (offsetPage < 0) {
-                throw new IllegalStateException("Offset page number is negative.");
+                throw new QueryObjectBuilderValidationError("Offset page number is negative.");
             }
 
             try {
                 orderByFieldName = NgramsSortFieldName.valueOf(orderByFieldNameString);
             } catch (Exception e) {
                 orderByFieldName = null;
-                //throw new IllegalStateException("Field name to order is incorrect.");
+                //throw new QueryObjectBuilderValidationError("Field name to order is incorrect.");
             }
 
             try {
                 order = Order.valueOf(orderString);
             } catch (Exception e) {
                 order = null;
-                //throw new IllegalStateException("Order tag is incorrect.");
+                //throw new QueryObjectBuilderValidationError("Order tag is incorrect.");
             }
 
             return new NgramsListQueryObject(this);

database-services-api/src/main/java/queryObjects/QueryObjectBuilderValidationError.java

+package queryObjects;
+
+public class QueryObjectBuilderValidationError extends Exception {
+    public QueryObjectBuilderValidationError(String message) {
+        super(message);
+    }
+}

database-services-api/src/main/java/queryObjects/WordsListQueryObject.java

             return this;
         }
 
-        public WordsListQueryObject build() {
+        public WordsListQueryObject build() throws QueryObjectBuilderValidationError {
             if (StringUtils.isEmpty(ngramData)) {
-                throw new IllegalStateException("N-gram data is not defined.");
+                throw new QueryObjectBuilderValidationError("N-gram data is not defined.");
             }
 
             if (amountOfElements < 1) {
-                throw new IllegalStateException("Amount of items in list is negative or zero.");
+                throw new QueryObjectBuilderValidationError("Amount of items in list is negative or zero.");
             }
 
             if (offsetPage < 0) {
-                throw new IllegalStateException("Offset page number is negative.");
+                throw new QueryObjectBuilderValidationError("Offset page number is negative.");
             }
 
             if (StringUtils.isEmpty(wordToSearchPattern)) {
-                throw new IllegalStateException("Word search pattern is null or empty.");
+                throw new QueryObjectBuilderValidationError("Word search pattern is null or empty.");
             }
 
             try {
                 orderByFieldName = WordsSortFieldName.valueOf(orderByFieldNameString);
             } catch (Exception e) {
                 orderByFieldName = null;
-                //throw new IllegalStateException("Field name to order is incorrect.");
+                //throw new QueryObjectBuilderValidationError("Field name to order is incorrect.");
             }
 
             try {
                 order = Order.valueOf(orderString);
             } catch (Exception e) {
                 order = null;
-                //throw new IllegalStateException("Order tag is incorrect.");
+                //throw new QueryObjectBuilderValidationError("Order tag is incorrect.");
             }
 
             return new WordsListQueryObject(this);

database-services-api/src/main/java/services/NgramsDatabaseReaderInterface.java

 package services;
 
-import dao.entities.dbTables.Ngram;
+import dtObjects.Ngram;
 import queryObjects.NgramsListQueryObject;
 
 import java.util.List;

database-services-api/src/main/java/services/WordsDatabaseReaderInterface.java

 package services;
 
-import dao.entities.logical.WordForNgramWithCount;
+import dtObjects.WordForNgramWithCount;
 import queryObjects.WordsListQueryObject;
 
 import java.util.List;

database-services-impl/src/main/java/services/NgramsJdbcReader.java

 
 import dao.NgramsDao;
 import dao.daoQueryObjects.NgramsListDaoQueryObject;
-import dao.entities.dbTables.Ngram;
 import dao.jdbcDao.JdbcNgramsDao;
+import dtConverters.NgramsListDaoToDto;
+import dtObjects.Ngram;
 import jdbcUtilities.JdbcH2DataSource;
 import queryConverters.NgramsListQueryObjectToDaoQueryObjectConverter;
 import queryObjects.NgramsListQueryObject;
 
     @Override
     public Ngram searchNgramByDataFullMatch(String ngramData) {
-        return ngramsDao.findByDataString(ngramData);
+        dao.entities.dbTables.Ngram ngram = ngramsDao.findByDataString(ngramData);
+        return new Ngram(ngram.getData(), ngram.getCount());
     }
 
     @Override
             return null;
 
         NgramsListDaoQueryObject ngramsDaoQO = NgramsListQueryObjectToDaoQueryObjectConverter.convert(ngramsListQuery);
-        return ngramsDao.extractListOfNgrams(ngramsDaoQO);
+        return NgramsListDaoToDto.convert(ngramsDao.extractListOfNgrams(ngramsDaoQO));
     }
 
     @Override

database-services-impl/src/main/java/services/WordsJdbcReader.java

 import dao.NgramsDao;
 import dao.WordsDao;
 import dao.daoQueryObjects.WordsListDaoQueryObject;
-import dao.entities.logical.WordForNgramWithCount;
 import dao.jdbcDao.JdbcNgramsDao;
 import dao.jdbcDao.JdbcWordsDao;
+import dtConverters.WordsListDaoToDto;
+import dtObjects.WordForNgramWithCount;
 import jdbcUtilities.JdbcH2DataSource;
 import org.apache.commons.lang3.StringUtils;
 import queryConverters.WordsListQueryObjectToDaoQueryObjectConverter;
 
         int ngramId = ngramsDao.getIdForNgram(wordsListQuery.getNgramData());
         WordsListDaoQueryObject wrdsDaoQO = WordsListQueryObjectToDaoQueryObjectConverter.convert(ngramId, wordsListQuery);
-        return wordsDao.findAndExtractListOfWordsForNgramByPartialWordMatch(wrdsDaoQO);
+        return WordsListDaoToDto.convert(wordsDao.findAndExtractListOfWordsForNgramByPartialWordMatch(wrdsDaoQO));
     }
 
     @Override

libngram-database/src/main/java/dao/jdbcDao/JdbcNgramsDao.java

         return ngram.getId();
     }
 
-    private List<Ngram> extractListOfTenItems(String query, int amountOfItems, int offsetPage) {
+    private List<Ngram> extractListOfItems(String query, int amountOfItems, int offsetPage) {
         if (offsetPage < 0 || amountOfItems < 1)
             return null;
         try {
 
         if (ngramsListDaoQueryObject.getOrder() == null && ngramsListDaoQueryObject.getOrderByFieldName() == null) {
             querySB = SELECT_TEN_NGRAMS_GENERAL_PART + QUERY_LIMITATION_PART;
-            return extractListOfTenItems(querySB, amountOfItems, offsetPage);
+            return extractListOfItems(querySB, amountOfItems, offsetPage);
         }
 
         if (ngramsListDaoQueryObject.getOrder() == Order.asc) {
             if (ngramsListDaoQueryObject.getOrderByFieldName() == NgramsSortFieldName.name) { // ASC by name
                 querySB = SELECT_TEN_NGRAMS_GENERAL_PART + QUERY_ORDER_BY_CLAUSE + SELECT_TEN_NGRAMS_QUERY_DATA_FIELD_NAME_PART + "ASC" + QUERY_LIMITATION_PART;
-                return extractListOfTenItems(querySB, amountOfItems, offsetPage);
+                return extractListOfItems(querySB, amountOfItems, offsetPage);
             } else { // ASC by count
                 querySB = SELECT_TEN_NGRAMS_GENERAL_PART + QUERY_ORDER_BY_CLAUSE + SELECT_TEN_NGRAMS_QUERY_COUNTER_FIELD_NAME_PART + "ASC" + QUERY_LIMITATION_PART;
-                return extractListOfTenItems(querySB, amountOfItems, offsetPage);
+                return extractListOfItems(querySB, amountOfItems, offsetPage);
             }
         } else {
             if (ngramsListDaoQueryObject.getOrderByFieldName() == NgramsSortFieldName.name) { // DESC by name
                 querySB = SELECT_TEN_NGRAMS_GENERAL_PART + QUERY_ORDER_BY_CLAUSE + SELECT_TEN_NGRAMS_QUERY_DATA_FIELD_NAME_PART + "DESC" + QUERY_LIMITATION_PART;
-                return extractListOfTenItems(querySB, amountOfItems, offsetPage);
+                return extractListOfItems(querySB, amountOfItems, offsetPage);
             } else { // DESC by count
                 querySB = SELECT_TEN_NGRAMS_GENERAL_PART + QUERY_ORDER_BY_CLAUSE + SELECT_TEN_NGRAMS_QUERY_COUNTER_FIELD_NAME_PART + "DESC" + QUERY_LIMITATION_PART;
-                return extractListOfTenItems(querySB, amountOfItems, offsetPage);
+                return extractListOfItems(querySB, amountOfItems, offsetPage);
             }
         }
     }

libngram-database/src/main/java/dao/jdbcDao/JdbcWordsDao.java

         }
     }
 
-    private List<WordForNgramWithCount> extractListOfTenItems(String query, int ngramId, String partOfWord, int amountOfItems, int offsetPage) {
+    private List<WordForNgramWithCount> extractListOfItems(String query, int ngramId, String partOfWord, int amountOfItems, int offsetPage) {
         if (partOfWord == null || ngramId < 0 || offsetPage < 0)
             return null;
         try {
 
         if (wordsListDaoQueryObject.getOrder() == null && wordsListDaoQueryObject.getOrderByFieldName() == null) {
             querySB = SELECT_TEN_WORDS_WITH_COUNTERS_FOR_NGRAM_BY_PARTIAL_WORD_DATA_GENERAL_PART + QUERY_LIMITATION_PART;
-            return extractListOfTenItems(querySB, ngramId, partOfWord, amountOfItems, offsetPage);
+            return extractListOfItems(querySB, ngramId, partOfWord, amountOfItems, offsetPage);
         }
 
         if (wordsListDaoQueryObject.getOrder() == Order.asc) {
             if (wordsListDaoQueryObject.getOrderByFieldName() == WordsSortFieldName.word) { // ASC by word
                 querySB = SELECT_TEN_WORDS_WITH_COUNTERS_FOR_NGRAM_BY_PARTIAL_WORD_DATA_GENERAL_PART +
                         QUERY_ORDER_BY_CLAUSE + SELECT_TEN_WORDS_QUERY_DATA_FIELD_NAME_PART + "ASC" + QUERY_LIMITATION_PART;
-                return extractListOfTenItems(querySB, ngramId, partOfWord, amountOfItems, offsetPage);
+                return extractListOfItems(querySB, ngramId, partOfWord, amountOfItems, offsetPage);
             } else { // ASC by count
                 querySB = SELECT_TEN_WORDS_WITH_COUNTERS_FOR_NGRAM_BY_PARTIAL_WORD_DATA_GENERAL_PART +
                         QUERY_ORDER_BY_CLAUSE + SELECT_TEN_WORDS_QUERY_COUNTER_FIELD_PART + "ASC" + QUERY_LIMITATION_PART;
-                return extractListOfTenItems(querySB, ngramId, partOfWord, amountOfItems, offsetPage);
+                return extractListOfItems(querySB, ngramId, partOfWord, amountOfItems, offsetPage);
             }
         } else {
             if (wordsListDaoQueryObject.getOrderByFieldName() == WordsSortFieldName.word) { // DESC by word
                 querySB = SELECT_TEN_WORDS_WITH_COUNTERS_FOR_NGRAM_BY_PARTIAL_WORD_DATA_GENERAL_PART +
                         QUERY_ORDER_BY_CLAUSE + SELECT_TEN_WORDS_QUERY_DATA_FIELD_NAME_PART + "DESC" + QUERY_LIMITATION_PART;
-                return extractListOfTenItems(querySB, ngramId, partOfWord, amountOfItems, offsetPage);
+                return extractListOfItems(querySB, ngramId, partOfWord, amountOfItems, offsetPage);
             } else { // DESC by count
                 querySB = SELECT_TEN_WORDS_WITH_COUNTERS_FOR_NGRAM_BY_PARTIAL_WORD_DATA_GENERAL_PART +
                         QUERY_ORDER_BY_CLAUSE + SELECT_TEN_WORDS_QUERY_COUNTER_FIELD_PART + "DESC" + QUERY_LIMITATION_PART;
-                return extractListOfTenItems(querySB, ngramId, partOfWord, amountOfItems, offsetPage);
+                return extractListOfItems(querySB, ngramId, partOfWord, amountOfItems, offsetPage);
             }
         }
     }

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

 package servlets.ngram;
 
-import dao.entities.logical.WordForNgramWithCount;
 import dao.queryOptions.Order;
-import dao.queryOptions.WordsSortFieldName;
+import dtObjects.WordForNgramWithCount;
 import exceptions.WrongURIException;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import queryObjects.QueryObjectBuilderValidationError;
 import queryObjects.WordsListQueryObject;
+import queryOptions.WordsSortFieldName;
 import services.WordsDatabaseReaderInterface;
 import services.WordsJdbcReader;
 import servlets.rest.ViewNgramRest;
 
     private static final String SEARCH_PLACEHOLDER = "Search among all words for this ngram (partial/regex)";
 
-    private void GetItemsTable(HttpServletRequest request, final String patternToFilter, final ViewNgramRest restParser, final boolean isSearchResult) {
+    private void GetItemsTable(HttpServletRequest request, final String patternToFilter, final ViewNgramRest restParser, final boolean isSearchResult) throws WrongQueryExecutionException {
         int pageId;
         WordsDatabaseReaderInterface dbreader = new WordsJdbcReader();
         List<WordForNgramWithCount> listOfWordsForNgram;
                             .order(order).build()
             );
 
-        } catch (IllegalArgumentException e) {
-            listOfWordsForNgram = dbreader.getListOfWordsWithCounters(
-                    new WordsListQueryObject.WordsListQueryBuilder(ngramData, AMOUNT_OF_ELEMENTS_PER_PAGE, 0, patternToFilter)
-                            .orderByField(orderFieldName)
-                            .order(order).build()
-            );
+        } catch (QueryObjectBuilderValidationError e) {
+            try {
+                listOfWordsForNgram = dbreader.getListOfWordsWithCounters(
+                        new WordsListQueryObject.WordsListQueryBuilder(ngramData, AMOUNT_OF_ELEMENTS_PER_PAGE, 0, REGEX_PATTERN_TO_EXTRACT_ALL_WORDS).build()
+                );
+            } catch (QueryObjectBuilderValidationError queryObjectBuilderValidationError) {
+                throw new WrongQueryExecutionException("Even with simplified parameters, execution of words list extraction operation is impossible.");
+            }
         }
 
         final int finalPageId = pageId;
         request.setAttribute("ngramDataString", ngramData);
     }
 
-    private RequestDispatcher returnSearchOfWordsForThisNgramResultPage(HttpServletRequest request, final String wordSearchData, final ViewNgramRest restParser) {
+    private RequestDispatcher returnSearchOfWordsForThisNgramResultPage(HttpServletRequest request, final String wordSearchData, final ViewNgramRest restParser) throws WrongQueryExecutionException {
         logger.debug("Showing a search result page.");
         GetItemsTable(request, wordSearchData, restParser, true);
 
         return request.getRequestDispatcher(JSP_VIEW_SEARCH_RESULT);
     }
 
-    private RequestDispatcher returnListOfWordsForThisNgramPage(HttpServletRequest request, final String patternToFilter, final ViewNgramRest restParser) {
+    private RequestDispatcher returnListOfWordsForThisNgramPage(HttpServletRequest request, final String patternToFilter, final ViewNgramRest restParser) throws WrongQueryExecutionException {
         logger.debug("Showing an an ngram page (words for ngram and their amount).");
         GetItemsTable(request, patternToFilter, restParser, false);
 

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

+package servlets.ngram;
+
+public class WrongQueryExecutionException extends Exception {
+    public WrongQueryExecutionException(String message) {
+        super(message);
+    }
+}

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

 package servlets.ngrams;
 
-import dao.entities.dbTables.Ngram;
-import dao.queryOptions.NgramsSortFieldName;
-import dao.queryOptions.Order;
+import dtObjects.Ngram;
 import exceptions.WrongURIException;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import queryObjects.NgramsListQueryObject;
+import queryObjects.QueryObjectBuilderValidationError;
+import queryOptions.NgramsSortFieldName;
+import queryOptions.Order;
 import services.NgramsDatabaseReaderInterface;
 import services.NgramsJdbcReader;
+import servlets.ngram.WrongQueryExecutionException;
 import servlets.rest.NgramsViewRest;
 
 import javax.servlet.RequestDispatcher;
         return request.getRequestDispatcher(JSP_VIEW_SEARCH_RESULT);
     }
 
-    private RequestDispatcher returnListOfNgramsPage(HttpServletRequest request) throws WrongURIException {
+    private RequestDispatcher returnListOfNgramsPage(HttpServletRequest request) throws WrongURIException, WrongQueryExecutionException {
         logger.debug("Show a list of ngrams page.");
         NgramsDatabaseReaderInterface dbreader = new NgramsJdbcReader();
         int pageId; // = 1 is minimum index at current stage. It's important, that database extracts pages from 0-index
                             .orderByField(orderFieldName)
                             .order(order).build()
             );
-        } catch (IllegalArgumentException e) {
-            listOfNgrams = dbreader.getListOfNgrams(
-                    new NgramsListQueryObject.NgramsListQueryBuilder(AMOUNT_OF_ELEMENTS_PER_PAGE, pageId - 1)
-                            .orderByField(orderFieldName)
-                            .order(order).build()
-            );
+        } catch (QueryObjectBuilderValidationError e) {
+            try {
+                listOfNgrams = dbreader.getListOfNgrams(
+                        new NgramsListQueryObject.NgramsListQueryBuilder(AMOUNT_OF_ELEMENTS_PER_PAGE, 0).build()
+                );
+            } catch (QueryObjectBuilderValidationError queryObjectBuilderValidationError) {
+                throw new WrongQueryExecutionException("Even with simplified parameters, execution of ngrams list extraction operation is impossible.");
+            }
         }
 
         final int finalPageId = pageId;
      * This doGet handles search queries to full match of ngram (that is determined as 'query' parameter: ngrams/view?query= ), and REST GET URLs:
      * ngrams/view/[[pageNumber]/[name|count/[asc|desc]]]
      *
-     * @param request
-     * @param response
      * @throws ServletException
      * @throws IOException
      */