Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SearchHistory {
- public SearchHistory(Long userId, Long groupId, String searchString) {
- this.userId = userId;
- this.groupId = groupId;
- this.searchString = searchString;
- }
- ...
- //getters + setters
- ...
- public void validate() throws InvalidParameterException{
- if (searchString == null || searchString.length() < MIN_WORD_SIZE) {
- throw new InvalidParameterException("Wrong search word min size: "+searchString);
- }
- if (searchString.length() > MAX_WORD_SIZE) {
- searchString = searchString.substring(0, MAX_WORD_SIZE - 1);
- }
- }
- }
- @Repository
- public class SearchHistorControllerImpl implements SearchHistoryController {
- @PersistenceContext(unitName = "userPU")
- private EntityManager userEm;
- @Override
- @Transactional
- public List<SearchHistory> getSearchHistories(Long userId) {
- return emptyIfNull(userEm.createNamedQuery("getSearchHistories")
- .setParameter("userId", userId)
- .getResultList());
- }
- @Override
- @Transactional
- public SearchHistory saveSearchHistory(Long userId, Long groupId, String word) throws InvalidParameterException{
- SearchHistory searchHistory = new SearchHistory(userId, groupId, word);
- searchHistory.validate(); //throws InvalidParameterException
- removeSameWord(userId, groupId, word);
- checkWordsSizeLimit(userId, groupId);
- searchHistory = userEm.merge(searchHistory);
- userEm.flush();
- return searchHistory;
- }
- private void removeSameWord(Long userId, Long groupId, String word) {
- userEm.createNamedQuery("removeSameWord")
- .setParameter("userId", userId)
- .setParameter("groupId", groupId)
- .setParameter("searchString", word)
- .executeUpdate();
- }
- private void checkWordsSizeLimit(Long userId, Long groupId){
- List<SearchHistory> searchHistoryIds = userEm.createNamedQuery("findOldSearchWordsIds")
- .setParameter("userId", userId)
- .setParameter("groupId", groupId)
- .setFirstResult(MAX_HISTORY_SIZE - 1)
- .getResultList();
- if (!searchHistoryIds.isEmpty()) {
- userEm.createNamedQuery("removeOldWords")
- .setParameter("searchHistoryIds", searchHistoryIds)
- .executeUpdate();
- }
- }
- }
- ...
- <query name="findOldSearchWordsIds">
- SELECT id
- FROM SearchHistory as searchHistory
- WHERE
- searchHistory.userId = :userId
- AND searchHistory.groupId = :groupId
- ORDER BY searchHistory.searchTime DESC
- </query>
- <query name="removeOldWords">
- DELETE FROM SearchHistory as searchHistory
- WHERE searchHistory.id in ( :searchHistoryIds )
- </query>
- <query name="removeSameWord">
- DELETE FROM SearchHistory as searchHistory
- WHERE
- searchHistory.searchString = :searchString
- AND searchHistory.userId = :userId
- AND searchHistory.groupId = :groupId
- </query>
- ...
- ...
- public void commandExecute() {
- buildSearchResult();
- if (whetherSaveRequestInHistory) {
- final String filteredString = StringUtils.removeEnd(filterString, "*");
- try {
- searchHistoryManager.saveSearchHistory(context.getUserId, groupId, filteredString);
- } catch (InvalidParameterException ipe) {
- log.info("Unable to save Search History to db: "+ ipe.getMessage());
- }
- }
- }
- ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement