Mercurial > hg > LGServices
view src/main/java/de/mpiwg/web/jsp/FullTextSearchPage.java @ 58:b8ad346e39a0
new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
---|---|
date | Mon, 02 May 2016 12:03:30 +0200 |
parents | 95bf4ac726e6 |
children | bc0219c2600b |
line wrap: on
line source
package de.mpiwg.web.jsp; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import de.mpiwg.gazetteer.bo.LGBranch; import de.mpiwg.gazetteer.bo.LGFile; import de.mpiwg.gazetteer.bo.LGFullTextSearchFile; import de.mpiwg.gazetteer.db.DBContents; import de.mpiwg.gazetteer.utils.DBService; import de.mpiwg.gazetteer.utils.DataProvider; import de.mpiwg.gazetteer.utils.FileManager; 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; 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 batchSearchTerm = 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 selectedContentMessage; 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 String contentFilter = new String(); private List<LGFullTextSearchFile> fileList = null; private List<LGFullTextSearchFile> weekFileList = null; // files modified within the past week private List<LGFullTextSearchFile> monthFileList = null; // files modified within the past month private List<LGFullTextSearchFile> olderFileList = null; // files modified before one month ago private String fileName = new String(); private String focusedContentId = new String(); private Integer selectedNumOfContent = 0; private String mouseX; private String mouseY; private LGFullTextSearchFile file = null; @Override public void init(){ super.init(); } public void loadParameters(HttpServletRequest request, HttpServletResponse response){ this.request = request; this.response = response; this.searchTerm = getParameter("searchTerm"); this.batchSearchTerm = getParameter("batchSearchTerm"); 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.contentFilter = getParameter("contentFilter"); this.fileName = getParameter("fileName"); this.focusedContentId = getParameter("focusedContentId"); this.mouseX = getParameter("mouseX"); this.mouseY = getParameter("mouseY"); this.paginator.setItemsPerPage(20); } public void searchBatch() { logger.debug("Batch Searching: " + this.batchSearchTerm); this.dynastyFilter = new String(); this.level1Filter = new String(); this.adminTypeFilter = new String(); if(StringUtils.isNotEmpty(this.batchSearchTerm)){ try { // parse different keyword set by delimiter ";" List<String> batchSearchKeywords = splitBatchSearchTerm(); logger.debug("batchSearchKeywords: " + batchSearchKeywords); String countingMessage = new String(); for (String aKeywordSet: batchSearchKeywords) { this.setSearchTerm(aKeywordSet); List<String> terms = splitTerms(); System.out.println("Full Text Search: " + terms.toString()); this.completeList = DBService.searchFullText(terms); if (this.completeList.size() != 0 ){ // save none zero result Collections.sort(this.completeList); this.filter(); this.calSelectedContentInCompleteList(); this.setFileName(this.getSearchTerm()); this.save(); } countingMessage += this.completeList.size() + "\t" + terms + "<br>"; } this.setSearchMessage(countingMessage); } catch (Exception e) { internalError(e); } } } public void search(){ logger.debug("Searching: " + this.searchTerm); this.dynastyFilter = new String(); this.level1Filter = new String(); this.adminTypeFilter = new String(); this.setFile(null); 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(); this.calSelectedContentInCompleteList(); } } catch (Exception e) { internalError(e); } } } private void calSelectedContentInCompleteList() { // calculate selectedContent this.selectedNumOfContent = 0; for (DBContents content: this.completeList) { if (!content.isRemoved()) { this.selectedNumOfContent += 1; } } this.setSelectedContentMessage(this.selectedNumOfContent.toString() + " section(s) selected"); } private void dispatchFileListByTime() { // set this.weekFileList, this.monthFileList, this.olderFileList from this.fileList this.weekFileList = new ArrayList<LGFullTextSearchFile>(); this.monthFileList = new ArrayList<LGFullTextSearchFile>(); this.olderFileList = new ArrayList<LGFullTextSearchFile>(); // TODO for (LGFullTextSearchFile aFile: this.getFileList()) { Date todayDate = new Date(); long DAY_IN_MS = 1000 * 60 * 60 * 24; Date oneWeekAgo = new Date(todayDate.getTime() - (7 * DAY_IN_MS)); Date oneMonthAgo = new Date(todayDate.getTime() - (30 * DAY_IN_MS)); if ( aFile.getLastChangeDate().after(oneWeekAgo)) { this.weekFileList.add(aFile); } else if (aFile.getLastChangeDate().after(oneMonthAgo)) { this.monthFileList.add(aFile); } else { this.olderFileList.add(aFile); } } } 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 this.forceSetFileLists(); } } private void forceSetFileLists() { // set FileList for the user DataProvider.getInstance().setFullTextSearchFileMap(null); // set fullTextSearchFileMap to null, to force reload this.setFileList(DataProvider.getInstance().getSearchFileList4User(getSessionBean().getUser().getId())); this.dispatchFileListByTime(); } public void deleteFile() throws IOException { Long fileId = getLongParameter("fileId"); logger.debug("deleting fileId=" + fileId); if(fileId != null){ LGFullTextSearchFile file = DataProvider.getInstance().getFullTextSearchFile(fileId); if(file != null){ DataProvider.getInstance().deleteLGFullTextSearchFile(file); // update FileList this.forceSetFileLists(); addMsg("The file " + file.getFileName() + " has been deleted."); } } } public void loadFile() { Long fileId = getLongParameter("fileId"); logger.debug("loading fileId=" + fileId); if(fileId != null){ // load from html file into searching result table LGFullTextSearchFile file = DataProvider.getInstance().getFullTextSearchFile(fileId); if(file != null) { this.setFile(file); String html; try { html = FileManager.getFullTextSearchHtmlFileText(file); Document doc = Jsoup.parse(html); Element body = doc.body(); this.setSearchTerm(body.getElementById("searchTerm").text()); this.setFileName(file.getFileName()); Element pageTableBody = body.getElementsByTag("tbody").first(); Elements rows = pageTableBody.children(); // set completeList by parsing html file List<DBContents> completListFromParsedFile = new ArrayList<DBContents>(); for (Element row : rows) { // make complteListFromParsedFile... Elements cols = row.children(); //logger.debug(cols.size()); if (cols.size() < 13) { } else if (StringUtils.equals("#", cols.get(0).text())) { // the table header } else { DBContents content = new DBContents(); String bookId = cols.get(1).text(); Integer contentPage = Integer.parseInt(cols.get(9).text()); String contentText = cols.get(10).text(); Long contentId = Long.parseLong(cols.get(11).text()); boolean isRemoved = true; if (StringUtils.equals(cols.get(12).text(), "false")) { isRemoved = false; } content.setInx(Integer.parseInt(cols.get(0).text())); content.setId(contentId); content.setBookId(cols.get(1).text()); // set this.section by bookId and page content.setSection(DBService.getInstance().getSectionByBookIdAndPage(bookId, contentPage)); // set this.coordinatesBook by bookId content.setCoordinatesBook(DBService.getInstance().getCoordinatesBook(bookId)); content.setContent(contentText); content.setPage(contentPage); content.setRemoved(isRemoved); completListFromParsedFile.add(content); } } this.setCompleteList(completListFromParsedFile); if (this.completeList != null ){ Collections.sort(this.completeList); filter(); this.calSelectedContentInCompleteList(); } } catch (Exception e) { logger.debug("getFullTextSearchHtmlFileText failed."); e.printStackTrace(); } } else { this.setFile(null); } } } 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); file = DataProvider.getInstance().saveLGFullTextSearchFile(this.getCompleteList(), userId, searchFile); logger.debug("file: " + file.getInfo()); this.setFile(file); addMsg("The table has been saved!"); } catch (Exception e) { addMsg("Saving fails!"); e.printStackTrace(); internalError(e); } this.forceSetFileLists(); } 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)) && (StringUtils.isEmpty(contentFilter) || StringUtils.contains(content.getContent(), contentFilter)) ){ 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()); } } 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(); // update the selectedNumOfContent to selectedMessage if (status) { this.selectedNumOfContent -= 1; } else { this.selectedNumOfContent += 1; } selectedContentMessage = this.selectedNumOfContent.toString() + " section(s) selected"; this.setSelectedContentMessage(selectedContentMessage); this.getPaginator().setCurrentPage(currentPage); this.updateCurrentSections(); } private List<String> splitBatchSearchTerm() { List<String> rs = new ArrayList<String>(); String[] array = this.batchSearchTerm.split(";"); for(String tmp : array){ tmp = tmp.replace(" ", ""); if(StringUtils.isNotEmpty(tmp)){ rs.add(tmp); } } return rs; } 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 getBatchSearchTerm() { return batchSearchTerm; } public void setBatchSearchTerm(String batchSearchTerm) { this.batchSearchTerm = batchSearchTerm; } 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 getSelectedContentMessage() { return selectedContentMessage; } public void setSelectedContentMessage(String selectedContentMessage) { this.selectedContentMessage = selectedContentMessage; } 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; } public String getContentFilter() { return contentFilter; } public void setContentFilter(String contentFilter) { this.contentFilter = contentFilter; } 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 List<LGFullTextSearchFile> getWeekFileList() { return weekFileList; } public void setWeekFileList(List<LGFullTextSearchFile> weekFileList) { this.weekFileList = weekFileList; } public List<LGFullTextSearchFile> getMonthFileList() { return monthFileList; } public void setMonthFileList(List<LGFullTextSearchFile> monthFileList) { this.monthFileList = monthFileList; } public List<LGFullTextSearchFile> getOlderFileList() { return olderFileList; } public void setOlderFileList(List<LGFullTextSearchFile> olderFileList) { this.olderFileList = olderFileList; } public String getMouseX() { return mouseX; } public void setMouseX(String mouseX) { this.mouseX = mouseX; } public String getMouseY() { return mouseY; } public void setMouseY(String mouseY) { this.mouseY = mouseY; } public Integer getSelectedNumOfContent() { return selectedNumOfContent; } public void setSelectedNumOfContent(Integer selectedNumOfContent) { this.selectedNumOfContent = selectedNumOfContent; } public LGFullTextSearchFile getFile() { return file; } public void setFile(LGFullTextSearchFile file) { this.file = file; } /////// Sorting 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(); } }