Mercurial > hg > LGServices
view src/main/java/de/mpiwg/web/jsp/FullTextSearchPage.java @ 43:9dbbbfd474f4
new: 1.existing topic in search result. 2.difference-set of book for topic
| author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
|---|---|
| date | Wed, 23 Dec 2015 16:03:32 +0100 |
| parents | 35ed4e650a53 |
| children | 13555aff1f88 |
line wrap: on
line source
package de.mpiwg.web.jsp; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import de.mpiwg.gazetteer.bo.LGBranch; import de.mpiwg.gazetteer.bo.LGFile; import de.mpiwg.gazetteer.bo.LGFullTextSearchFile; import de.mpiwg.gazetteer.dataverse.bo.VDCUser; import de.mpiwg.gazetteer.db.DBContents; import de.mpiwg.gazetteer.db.DBSection; import de.mpiwg.gazetteer.utils.DBService; import de.mpiwg.gazetteer.utils.DataProvider; import de.mpiwg.web.fullTextSearch.SortContentByAdminType; import de.mpiwg.web.fullTextSearch.SortContentByBookId; import de.mpiwg.web.fullTextSearch.SortContentByBookName; import de.mpiwg.web.fullTextSearch.SortContentByDynasty; import de.mpiwg.web.fullTextSearch.SortContentById; import de.mpiwg.web.fullTextSearch.SortContentByInx; import de.mpiwg.web.fullTextSearch.SortContentByLevel1; import de.mpiwg.web.fullTextSearch.SortContentByLevel2; import de.mpiwg.web.fullTextSearch.SortContentByPeriod; import de.mpiwg.web.fullTextSearch.SortContentByStartPage; import de.mpiwg.web.search.SortSectionByAdminType; import de.mpiwg.web.search.SortSectionByAuthor; import de.mpiwg.web.search.SortSectionByBookId; import de.mpiwg.web.search.SortSectionByBookName; import de.mpiwg.web.search.SortSectionByDynasty; import de.mpiwg.web.search.SortSectionByEdition; import de.mpiwg.web.search.SortSectionById; import de.mpiwg.web.search.SortSectionByLevel1; import de.mpiwg.web.search.SortSectionByPeriod; import de.mpiwg.web.search.SortSectionByStartPage; import de.mpiwg.web.search.SortSectionByVolume; public class FullTextSearchPage extends AbstractJSPPage{ private static Logger logger = Logger.getLogger(FullTextSearchPage.class); public static String bean = "fullTextSearchBean"; public static String page = "pages/fullTextSearch.jsp"; private List<DBContents> completeList; private List<DBContents> filteredList; private List<DBContents> displayList; private String searchTerm = new String(); private String dynastyFilter = new String(); private String adminTypeFilter = new String(); private String level1Filter = new String(); private DataPaginator paginator = new DataPaginator(); private String searchMessage; private String filteringMessage; private String bookIdFilter = new String(); private String bookNameFilter = new String(); private String level2Filter = new String(); private String periodFilter = new String(); private String sectionNameFilter = new String(); private List<LGFullTextSearchFile> fileList = null; private String fileName = new String(); private String focusedContentId = new String(); @Override public void init(){ super.init(); } public void loadParameters(HttpServletRequest request, HttpServletResponse response){ this.request = request; this.response = response; this.searchTerm = getParameter("searchTerm"); this.bookIdFilter = getParameter("bookIdFilter"); this.bookNameFilter = getParameter("bookNameFilter"); this.dynastyFilter = getParameter("dynastyFilter"); this.adminTypeFilter = getParameter("adminTypeFilter"); this.level1Filter = getParameter("level1Filter"); this.level2Filter = getParameter("level2Filter"); this.periodFilter = getParameter("periodFilter"); this.sectionNameFilter = getParameter("sectionNameFilter"); this.fileName = getParameter("fileName"); this.focusedContentId = getParameter("focusedContentId"); } public void search(){ logger.debug("Searching: " + this.searchTerm); this.dynastyFilter = new String(); this.level1Filter = new String(); this.adminTypeFilter = new String(); if(StringUtils.isNotEmpty(this.searchTerm)){ try { List<String> terms = splitTerms(); System.out.println("Full Text Search: " + terms.toString()); this.completeList = DBService.searchFullText(terms); if (this.completeList != null ){ Collections.sort(this.completeList); filter(); } } catch (Exception e) { internalError(e); } } } public void forceLoadFileList(){ logger.debug("forceLoadFileList"); logger.debug(this.getSearchTerm()); if(getSessionBean().getUser() != null){ //logger.debug("userId="+ getSessionBean().getUser().getId()); // set FileList for the user DataProvider.getInstance().setFullTextSearchFileMap(null); // set fullTextSearchFileMap to null, to force reload this.setFileList(DataProvider.getInstance().getSearchFileList4User(getSessionBean().getUser().getId())); } } public void save() { logger.debug("saving table..."); logger.debug(this.getFileName() + ", " + this.getSearchTerm() + ", userId= "+ getSessionBean().getUser().getId()); //logger.debug(this.getFilteredList()); if (StringUtils.equals(this.getFileName(), "") ) { addMsg("Save failed. Table name cannot be empty."); return; } /* Update db table `LGFullTextSearchFile`: new row with userId, file name, ...*/ Long userId = getSessionBean().getUser().getId(); LGFullTextSearchFile file; try { // check if record with (userId, fileName) already existed, update it; otherwise, create one LGFullTextSearchFile searchFile = new LGFullTextSearchFile(); for (LGFullTextSearchFile aFile: this.getFileList()) { if ( StringUtils.equals(aFile.getFileName(), fileName) && aFile.getUserId().equals(userId)) { searchFile = aFile; break; } } if (searchFile.isEmpty() ) { searchFile.setFileName(fileName); searchFile.setUserId(userId); } searchFile.setSearchTerms(this.searchTerm); file = DataProvider.getInstance().saveLGFullTextSearchFile(this.getFilteredList(), userId, searchFile); addMsg("The table has been saved!"); logger.debug(file.getInfo()); } catch (Exception e) { addMsg("Saving fails!"); e.printStackTrace(); internalError(e); } // update FileList DataProvider.getInstance().setFullTextSearchFileMap(null); // set fullTextSearchFileMap to null, to force reload this.setFileList(DataProvider.getInstance().getSearchFileList4User(getSessionBean().getUser().getId())); } public void filter(){ this.filteredList = new ArrayList<DBContents>(); for(DBContents content : this.completeList){ if(!this.filteredList.contains(content)){ if( (StringUtils.isEmpty(dynastyFilter) || StringUtils.startsWith(content.getSection().getBook().getDynasty(), dynastyFilter)) && (StringUtils.isEmpty(level1Filter) || StringUtils.startsWith(content.getSection().getBook().getLevel1(), level1Filter)) && (StringUtils.isEmpty(level2Filter) || StringUtils.startsWith(content.getSection().getBook().getLevel2(), level2Filter)) && (StringUtils.isEmpty(adminTypeFilter) || StringUtils.startsWith(content.getSection().getBook().getAdmin_type(), adminTypeFilter)) && (StringUtils.isEmpty(bookIdFilter) || StringUtils.startsWith(content.getBookId(), bookIdFilter)) && (StringUtils.isEmpty(bookNameFilter) || StringUtils.startsWith(content.getSection().getBook().getName(), bookNameFilter)) && (StringUtils.isEmpty(sectionNameFilter) || StringUtils.startsWith(content.getSection().getName(), sectionNameFilter)) ){ this.filteredList.add(content); } } } if(completeList.size() > 0){ this.searchMessage = completeList.size() + " section(s) found for the term(s): " + this.searchTerm; this.filteringMessage = this.filteredList.size() + " section(s) listed after the filtering"; this.paginator.setCurrentPage(0); this.paginator.resetNumberOfPages(filteredList.size()); }else{ this.searchMessage = "No sections found for the term(s): " + this.searchTerm; this.filteredList = null; this.filteringMessage = ""; this.paginator.setCurrentPage(0); this.paginator.resetNumberOfPages(0); } this.updateCurrentSections(); } private void updateCurrentSections() { this.paginator.initCount(); int startRecord = this.paginator.getCurrentPage() * this.paginator.getItemsPerPage(); if(this.paginator.getNumberOfPages() == 0){ this.displayList = new ArrayList<DBContents>(); }else if((this.paginator.getCurrentPage() + 1) == this.paginator.getNumberOfPages()){ int mod = this.filteredList.size() % paginator.getItemsPerPage(); if(mod == 0){ this.displayList = filteredList.subList(startRecord, startRecord + this.paginator.getItemsPerPage()); }else{ this.displayList = filteredList.subList(startRecord, startRecord + mod); } }else{ this.displayList = filteredList.subList(startRecord, startRecord + this.paginator.getItemsPerPage()); } } private List<String> splitTerms(){ List<String> rs = new ArrayList<String>(); String[] array = this.searchTerm.split(","); for(String tmp : array){ tmp = tmp.replace(" ", ""); if(StringUtils.isNotEmpty(tmp)){ rs.add(tmp); } } return rs; } public List<String> suggestDynasty(String term, int limit){ List<String> list = new ArrayList<String>(); for(DBContents content : this.completeList){ String dynasty = content.getSection().getBook().getDynasty(); if(!list.contains(dynasty) && dynasty.startsWith(term)){ list.add(dynasty); } if(limit == list.size()){ break; } } return list; } public List<String> suggestLevel1(String term, int limit){ List<String> list = new ArrayList<String>(); for(DBContents content : this.completeList){ String level1 = content.getSection().getBook().getLevel1(); if(!list.contains(level1) && level1.startsWith(term)){ list.add(level1); } if(limit == list.size()){ break; } } return list; } public List<String> suggestAdminType(String term, int limit){ List<String> list = new ArrayList<String>(); for(DBContents content : this.completeList){ String adminType = content.getSection().getBook().getAdmin_type(); if(!list.contains(adminType) && adminType.startsWith(term)){ list.add(adminType); } if(limit == list.size()){ break; } } return list; } public List<LGFullTextSearchFile> getFileList() { return fileList; } public void setFileList(List<LGFullTextSearchFile> fileList) { this.fileList = fileList; } public List<DBContents> getCompleteList() { return completeList; } public void setCompleteList(List<DBContents> completeList) { this.completeList = completeList; } public String getSearchTerm() { return searchTerm; } public void setSearchTerm(String searchTerm) { this.searchTerm = searchTerm; } public List<DBContents> getFilteredList() { return filteredList; } public void setFilteredList(List<DBContents> filteredList) { this.filteredList = filteredList; } public List<DBContents> getDisplayList() { return displayList; } public void setDisplayList(List<DBContents> displayList) { this.displayList = displayList; } public DataPaginator getPaginator() { return paginator; } public void setPaginator(DataPaginator paginator) { this.paginator = paginator; } public void firstPage() { this.paginator.first(); this.updateCurrentSections(); } public void lastPage() { this.paginator.last(); this.updateCurrentSections(); } public void fastForward() { this.paginator.fastForward(); this.updateCurrentSections(); } public void fastRewind() { this.paginator.fastRewind(); this.updateCurrentSections(); } public void previousPage() { this.paginator.previous(); this.updateCurrentSections(); } public void nextPage() { this.paginator.next(); this.updateCurrentSections(); } public String getSearchMessage() { return searchMessage; } public void setSearchMessage(String searchMessage) { this.searchMessage = searchMessage; } public String getFilteringMessage() { return filteringMessage; } public void setFilteringMessage(String filteringMessage) { this.filteringMessage = filteringMessage; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getDynastyFilter() { return dynastyFilter; } public void setDynastyFilter(String dynastyFilter) { this.dynastyFilter = dynastyFilter; } public String getAdminTypeFilter() { return adminTypeFilter; } public void setAdminTypeFilter(String adminTypeFilter) { this.adminTypeFilter = adminTypeFilter; } public String getLevel1Filter() { return level1Filter; } public void setLevel1Filter(String level1Filter) { this.level1Filter = level1Filter; } /////// Sorting public String getFocusedContentId() { return focusedContentId; } public void setFocusedContentId(String focusedContentId) { this.focusedContentId = focusedContentId; } public String getBookIdFilter() { return bookIdFilter; } public void setBookIdFilter(String bookIdFilter) { this.bookIdFilter = bookIdFilter; } public String getBookNameFilter() { return bookNameFilter; } public void setBookNameFilter(String bookNameFilter) { this.bookNameFilter = bookNameFilter; } public String getLevel2Filter() { return level2Filter; } public void setLevel2Filter(String level2Filter) { this.level2Filter = level2Filter; } public String getPeriodFilter() { return periodFilter; } public void setPeriodFilter(String periodFilter) { this.periodFilter = periodFilter; } public String getSectionNameFilter() { return sectionNameFilter; } public void setSectionNameFilter(String sectionNameFilter) { this.sectionNameFilter = sectionNameFilter; } public void sortByBookNameUp(){ Collections.sort(this.completeList, new SortContentByBookName()); filter(); } public void sortByBookNameDown(){ Collections.sort(this.completeList, new SortContentByBookName()); Collections.reverse(this.completeList); filter(); } public void sortBySectionNameUp(){ Collections.sort(this.completeList); filter(); } public void sortBySectionNameDown(){ Collections.sort(this.completeList); Collections.reverse(this.completeList); filter(); } public void sortByPeriodUp(){ Collections.sort(this.completeList, new SortContentByPeriod()); filter(); } public void sortByPeriodDown(){ Collections.sort(this.completeList, new SortContentByPeriod()); Collections.reverse(this.completeList); filter(); } public void sortByIdUp(){ Collections.sort(this.completeList, new SortContentById()); this.filter(); } public void sortByIdDown(){ Collections.sort(this.completeList, new SortContentById()); Collections.reverse(completeList); this.filter(); } public void sortByDynastyUp(){ Collections.sort(this.completeList, new SortContentByDynasty()); filter(); } public void sortByDynastyDown(){ Collections.sort(this.completeList, new SortContentByDynasty()); Collections.reverse(completeList); filter(); } public void sortByBookIdUp(){ Collections.sort(this.completeList, new SortContentByBookId()); filter(); } public void sortByBookIdDown(){ Collections.sort(this.completeList, new SortContentByBookId()); Collections.reverse(completeList); filter(); } public void sortByLevel1Up(){ Collections.sort(this.completeList, new SortContentByLevel1()); filter(); } public void sortByLevel1Down(){ Collections.sort(this.completeList, new SortContentByLevel1()); Collections.reverse(completeList); filter(); } public void sortByLevel2Up(){ Collections.sort(this.completeList, new SortContentByLevel2()); filter(); } public void sortByLevel2Down(){ Collections.sort(this.completeList, new SortContentByLevel2()); Collections.reverse(completeList); filter(); } public void sortByAdminTypeUp(){ Collections.sort(this.completeList, new SortContentByAdminType()); filter(); } public void sortByAdminTypeDown(){ Collections.sort(this.completeList, new SortContentByAdminType()); Collections.reverse(completeList); filter(); } public void sortByStartPageUp(){ Collections.sort(this.completeList, new SortContentByStartPage()); filter(); } public void sortByStartPageDown(){ Collections.sort(this.completeList, new SortContentByStartPage()); Collections.reverse(completeList); filter(); } public void sortByInxUp(){ Collections.sort(this.completeList, new SortContentByInx()); filter(); } public void sortByInxDown(){ Collections.sort(this.completeList, new SortContentByInx()); Collections.reverse(completeList); filter(); } public void removeFocusedContent(boolean status) { // set isRemove for the content with id=this.focusedContentId // status is true: remove; // status is false: recover (unremove) for (DBContents content: this.completeList) { if (StringUtils.equals(content.getId().toString(), this.focusedContentId)) { content.setRemoved(status); logger.debug("set remove content id=" + content.getId().toString()); break; } } int currentPage = this.getPaginator().getCurrentPage(); Collections.sort(this.completeList); filter(); this.getPaginator().setCurrentPage(currentPage); this.updateCurrentSections(); } }
