Advertisement
Guest User

Untitled

a guest
Oct 26th, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. public class SearchHistory {
  2. public SearchHistory(Long userId, Long groupId, String searchString) {
  3. this.userId = userId;
  4. this.groupId = groupId;
  5. this.searchString = searchString;
  6. }
  7. ...
  8. //getters + setters
  9. ...
  10. public void validate() throws InvalidParameterException{
  11. if (searchString == null || searchString.length() < MIN_WORD_SIZE) {
  12. throw new InvalidParameterException("Wrong search word min size: "+searchString);
  13. }
  14.  
  15. if (searchString.length() > MAX_WORD_SIZE) {
  16. searchString = searchString.substring(0, MAX_WORD_SIZE - 1);
  17. }
  18. }
  19. }
  20.  
  21. @Repository
  22. public class SearchHistorControllerImpl implements SearchHistoryController {
  23.  
  24. @PersistenceContext(unitName = "userPU")
  25. private EntityManager userEm;
  26.  
  27. @Override
  28. @Transactional
  29. public List<SearchHistory> getSearchHistories(Long userId) {
  30. return emptyIfNull(userEm.createNamedQuery("getSearchHistories")
  31. .setParameter("userId", userId)
  32. .getResultList());
  33. }
  34.  
  35. @Override
  36. @Transactional
  37. public SearchHistory saveSearchHistory(Long userId, Long groupId, String word) throws InvalidParameterException{
  38. SearchHistory searchHistory = new SearchHistory(userId, groupId, word);
  39. searchHistory.validate(); //throws InvalidParameterException
  40.  
  41. removeSameWord(userId, groupId, word);
  42. checkWordsSizeLimit(userId, groupId);
  43.  
  44. searchHistory = userEm.merge(searchHistory);
  45. userEm.flush();
  46. return searchHistory;
  47. }
  48.  
  49. private void removeSameWord(Long userId, Long groupId, String word) {
  50. userEm.createNamedQuery("removeSameWord")
  51. .setParameter("userId", userId)
  52. .setParameter("groupId", groupId)
  53. .setParameter("searchString", word)
  54. .executeUpdate();
  55. }
  56.  
  57. private void checkWordsSizeLimit(Long userId, Long groupId){
  58. List<SearchHistory> searchHistoryIds = userEm.createNamedQuery("findOldSearchWordsIds")
  59. .setParameter("userId", userId)
  60. .setParameter("groupId", groupId)
  61. .setFirstResult(MAX_HISTORY_SIZE - 1)
  62. .getResultList();
  63.  
  64. if (!searchHistoryIds.isEmpty()) {
  65. userEm.createNamedQuery("removeOldWords")
  66. .setParameter("searchHistoryIds", searchHistoryIds)
  67. .executeUpdate();
  68. }
  69. }
  70. }
  71.  
  72. ...
  73. <query name="findOldSearchWordsIds">
  74. SELECT id
  75. FROM SearchHistory as searchHistory
  76. WHERE
  77. searchHistory.userId = :userId
  78. AND searchHistory.groupId = :groupId
  79. ORDER BY searchHistory.searchTime DESC
  80. </query>
  81.  
  82. <query name="removeOldWords">
  83. DELETE FROM SearchHistory as searchHistory
  84. WHERE searchHistory.id in ( :searchHistoryIds )
  85. </query>
  86.  
  87. <query name="removeSameWord">
  88. DELETE FROM SearchHistory as searchHistory
  89. WHERE
  90. searchHistory.searchString = :searchString
  91. AND searchHistory.userId = :userId
  92. AND searchHistory.groupId = :groupId
  93. </query>
  94. ...
  95.  
  96. ...
  97. public void commandExecute() {
  98. buildSearchResult();
  99.  
  100. if (whetherSaveRequestInHistory) {
  101. final String filteredString = StringUtils.removeEnd(filterString, "*");
  102. try {
  103. searchHistoryManager.saveSearchHistory(context.getUserId, groupId, filteredString);
  104. } catch (InvalidParameterException ipe) {
  105. log.info("Unable to save Search History to db: "+ ipe.getMessage());
  106. }
  107. }
  108. }
  109.  
  110. ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement