Mercurial > hg > LGServices
diff src/main/java/de/mpiwg/web/jsp/SearchPage.java @ 0:3e62083dbcbf
First commit. This project comes from LGServer. We removed the framework icefaces. Now, LGServices uses just JSP and jquery.
author | "jurzua <jurzua@mpiwg-berlin.mpg.de>" |
---|---|
date | Thu, 23 Apr 2015 15:46:01 +0200 |
parents | |
children | 1af9d7db348e |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/mpiwg/web/jsp/SearchPage.java Thu Apr 23 15:46:01 2015 +0200 @@ -0,0 +1,502 @@ +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.db.DBSection; +import de.mpiwg.gazetteer.utils.DBService; +import de.mpiwg.gazetteer.utils.DataProvider; +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 SearchPage extends AbstractJSPPage{ + + private static Logger logger = Logger.getLogger(SearchPage.class); + + public static String bean = "searchBean"; + public static String page = "pages/search.jsp"; + + private static Integer SEARCH_IN_SECTION_NAME = 0; + private static Integer SEARCH_IN_BOOK_NAME = 1; + + + + private Map<Long, List<LGBranch>> branchesMap; + private List<DBSection> completeSectionList; + private List<DBSection> filteredSectionList; + private List<DBSection> displaySectionList; + + private String searchTerm = new String(); + private Integer searchIn = SEARCH_IN_SECTION_NAME; + + 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; + + @Override + public void init(){ + super.init(); + } + + public void loadParameters(HttpServletRequest request, HttpServletResponse response){ + this.request = request; + this.response = response; + + this.searchTerm = getParameter("searchTerm"); + this.dynastyFilter = getParameter("dynastyFilter"); + this.adminTypeFilter = getParameter("adminTypeFilter"); + this.level1Filter = getParameter("level1Filter"); + this.searchIn = getIntParameter("searchIn"); + } + + 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)){ + this.loadBranches(); + try { + List<String> terms = splitTerms(); + + if(SEARCH_IN_SECTION_NAME.equals(this.searchIn)){ + System.out.println("Search in Section Name"); + this.completeSectionList = DBService.searchSection(terms); + }else if(SEARCH_IN_BOOK_NAME.equals(this.searchIn)){ + System.out.println("Search in Book Name"); + this.completeSectionList = DBService.searchBook(terms, "name"); + } + + Collections.sort(this.completeSectionList); + + filter(); + + + } catch (Exception e) { + internalError(e); + } + } + } + + public void filter(){ + this.filteredSectionList = new ArrayList<DBSection>(); + for(DBSection section : this.completeSectionList){ + if(!this.filteredSectionList.contains(section)){ + + if( (StringUtils.isEmpty(dynastyFilter) || StringUtils.startsWith(section.getBook().getDynasty(), dynastyFilter)) && + (StringUtils.isEmpty(level1Filter) || StringUtils.startsWith(section.getBook().getLevel1(), level1Filter)) && + (StringUtils.isEmpty(adminTypeFilter) || StringUtils.startsWith(section.getBook().getAdmin_type(), adminTypeFilter)) + ){ + this.filteredSectionList.add(section); + } + } + } + + if(completeSectionList.size() > 0){ + this.searchMessage = completeSectionList.size() + " section(s) found for the term(s): " + this.searchTerm; + this.filteringMessage = this.filteredSectionList.size() + " section(s) listed after the filtering"; + this.paginator.setCurrentPage(0); + this.paginator.resetNumberOfPages(filteredSectionList.size()); + this.updateCurrentSections(); + }else{ + this.searchMessage = "No sections found for the term(s): " + this.searchTerm; + this.filteredSectionList = null; + } + } + + private void updateCurrentSections() { + this.paginator.initCount(); + int startRecord = this.paginator.getCurrentPage() + * this.paginator.getItemsPerPage(); + if(this.paginator.getNumberOfPages() == 0){ + this.displaySectionList = new ArrayList<DBSection>(); + }else if((this.paginator.getCurrentPage() + 1) == this.paginator.getNumberOfPages()){ + int mod = this.filteredSectionList.size() % paginator.getItemsPerPage(); + if(mod == 0){ + this.displaySectionList = filteredSectionList.subList(startRecord, startRecord + this.paginator.getItemsPerPage()); + }else{ + this.displaySectionList = filteredSectionList.subList(startRecord, startRecord + mod); + } + + }else{ + this.displaySectionList = filteredSectionList.subList(startRecord, startRecord + this.paginator.getItemsPerPage()); + } + + for(DBSection section : this.displaySectionList){ + section.setBranches(this.branchesMap.get(section.getId())); + } + } + + private void loadBranches(){ + this.branchesMap = new HashMap<Long, List<LGBranch>>(); + List<LGBranch> list = DataProvider.getInstance().getBranches(getSessionBean().getUser().getId()); + for(LGBranch branch : list){ + branch.loadTransientData(); + if(this.branchesMap.get(branch.getSectionId()) == null){ + this.branchesMap.put(branch.getSectionId(), new ArrayList<LGBranch>()); + } + this.branchesMap.get(branch.getSectionId()).add(branch); + } + } + + 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(DBSection section : this.completeSectionList){ + String dynasty = section.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(DBSection section : this.completeSectionList){ + String level1 = section.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(DBSection section : this.completeSectionList){ + String adminType = section.getBook().getAdmin_type(); + if(!list.contains(adminType) && adminType.startsWith(term)){ + list.add(adminType); + } + if(limit == list.size()){ + break; + } + } + return list; + } + + + public static Integer getSEARCH_IN_SECTION_NAME() { + return SEARCH_IN_SECTION_NAME; + } + + + public static void setSEARCH_IN_SECTION_NAME(Integer sEARCH_IN_SECTION_NAME) { + SEARCH_IN_SECTION_NAME = sEARCH_IN_SECTION_NAME; + } + + + public Map<Long, List<LGBranch>> getBranchesMap() { + return branchesMap; + } + + + public void setBranchesMap(Map<Long, List<LGBranch>> branchesMap) { + this.branchesMap = branchesMap; + } + + + public List<DBSection> getCompleteSectionList() { + return completeSectionList; + } + + + public void setCompleteSectionList(List<DBSection> completeSectionList) { + this.completeSectionList = completeSectionList; + } + + + public String getSearchTerm() { + return searchTerm; + } + + + public void setSearchTerm(String searchTerm) { + this.searchTerm = searchTerm; + } + + + public Integer getSearchIn() { + return searchIn; + } + + + public void setSearchIn(Integer searchIn) { + this.searchIn = searchIn; + } + + public List<DBSection> getFilteredSectionList() { + return filteredSectionList; + } + + public void setFilteredSectionList(List<DBSection> filteredSectionList) { + this.filteredSectionList = filteredSectionList; + } + + public List<DBSection> getDisplaySectionList() { + return displaySectionList; + } + + public void setDisplaySectionList(List<DBSection> displaySectionList) { + this.displaySectionList = displaySectionList; + } + + 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; + } + + /////// Sorting + + public String getDynastyFilter() { + return dynastyFilter; + } + + public void setDynastyFilter(String dynastyFilter) { + this.dynastyFilter = dynastyFilter; + } + + public void sortByBookNameUp(){ + Collections.sort(this.completeSectionList, new SortSectionByBookName()); + filter(); + } + + public void sortByBookNameDown(){ + Collections.sort(this.completeSectionList, new SortSectionByBookName()); + Collections.reverse(this.completeSectionList); + filter(); + } + + public void sortBySectionNameUp(){ + Collections.sort(this.completeSectionList); + filter(); + } + + public void sortBySectionNameDown(){ + Collections.sort(this.completeSectionList); + Collections.reverse(this.completeSectionList); + filter(); + } + + public void sortByAuthorUp(){ + Collections.sort(this.completeSectionList, new SortSectionByAuthor()); + filter(); + } + + public void sortByAuthorDown(){ + Collections.sort(this.completeSectionList, new SortSectionByAuthor()); + Collections.reverse(this.completeSectionList); + filter(); + } + + public void sortByPeriodUp(){ + Collections.sort(this.completeSectionList, new SortSectionByPeriod()); + filter(); + } + + public void sortByPeriodDown(){ + Collections.sort(this.completeSectionList, new SortSectionByPeriod()); + Collections.reverse(this.completeSectionList); + filter(); + } + + public void sortByVolumeUp(){ + Collections.sort(this.completeSectionList, new SortSectionByVolume()); + filter(); + } + + public void sortByVolumeDown(){ + Collections.sort(this.completeSectionList, new SortSectionByVolume()); + Collections.reverse(this.completeSectionList); + filter(); + } + + + public void sortBySectionIdUp(){ + Collections.sort(this.completeSectionList, new SortSectionById()); + this.filter(); + } + + public void sortBySectionIdDown(){ + Collections.sort(this.completeSectionList, new SortSectionById()); + Collections.reverse(completeSectionList); + this.filter(); + } + + public void sortByEditionUp(){ + Collections.sort(this.completeSectionList, new SortSectionByEdition()); + filter(); + } + + public void sortByEditionDown(){ + Collections.sort(this.completeSectionList, new SortSectionByEdition()); + Collections.reverse(completeSectionList); + filter(); + } + + public void sortByDynastyUp(){ + Collections.sort(this.completeSectionList, new SortSectionByDynasty()); + filter(); + } + + public void sortByDynastyDown(){ + Collections.sort(this.completeSectionList, new SortSectionByDynasty()); + Collections.reverse(completeSectionList); + filter(); + } + + public void sortByBookIdUp(){ + Collections.sort(this.completeSectionList, new SortSectionByBookId()); + filter(); + } + + public void sortByBookIdDown(){ + Collections.sort(this.completeSectionList, new SortSectionByBookId()); + Collections.reverse(completeSectionList); + filter(); + } + + public void sortByLevel1Up(){ + Collections.sort(this.completeSectionList, new SortSectionByLevel1()); + filter(); + } + + public void sortByLevel1Down(){ + Collections.sort(this.completeSectionList, new SortSectionByLevel1()); + Collections.reverse(completeSectionList); + filter(); + } + + public void sortByAdminTypeUp(){ + Collections.sort(this.completeSectionList, new SortSectionByAdminType()); + filter(); + } + + public void sortByAdminTypeDown(){ + Collections.sort(this.completeSectionList, new SortSectionByAdminType()); + Collections.reverse(completeSectionList); + filter(); + } + + public void sortByStartPageUp(){ + Collections.sort(this.completeSectionList, new SortSectionByStartPage()); + filter(); + } + + public void sortByStartPageDown(){ + Collections.sort(this.completeSectionList, new SortSectionByStartPage()); + Collections.reverse(completeSectionList); + filter(); + } + + 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; + } + + +}