Mercurial > hg > LGServer
view src/main/java/de/mpiwg/web/SearchBean.java @ 5:5316e79f9a27
Implementation of search pagination and lazy loading to display the result set of a search.
author | "jurzua <jurzua@mpiwg-berlin.mpg.de>" |
---|---|
date | Mon, 16 Mar 2015 11:25:36 +0100 |
parents | 7682c04c63a8 |
children | 5610250d021a |
line wrap: on
line source
package de.mpiwg.web; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.faces.event.ActionEvent; import javax.faces.event.ValueChangeEvent; import javax.faces.model.SelectItem; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.icefaces.ace.event.TextChangeEvent; 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.jsf.DataPaginator; public class SearchBean extends AbstractBean{ private static String GOTO_SEARCH_PAGE = "searchPage"; private static Logger logger = Logger.getLogger(SearchBean.class); private String term; private List<SelectItem> sectionSuggestion; private List<DBSection> completeSectionList; private List<DBSection> currentSectionList; private String message; private Map<Long, List<LGBranch>> branchesMap; private transient DataPaginator advancedPaginator = new DataPaginator(); public void changeSectionName(TextChangeEvent event){ //logger.debug("changeSectionName"); //logger.debug("key" + event.getKeyCode()); String term = event.getNewValue().toString(); this.sectionSuggestion = new ArrayList<SelectItem>(); if(!term.contains(",")){ try { List<String> list = DBService.suggestSectionName(term); for(String s : list){ this.sectionSuggestion.add(new SelectItem(s)); } } catch (SQLException e) { internalError(e); } } } public void changeSectionSearch(ValueChangeEvent event){ this.term = (String)event.getNewValue(); this.search(); } public void listenerSearch(ActionEvent event){ this.search(); } private void search(){ if(StringUtils.isNotEmpty(this.term)){ this.loadBranches(); try { List<String> terms = splitTerms(); this.completeSectionList = DBService.searchSection(terms); if(completeSectionList.size() > 0){ this.message = completeSectionList.size() + " item(s) found for the term(s): " + this.term; this.advancedPaginator.setCurrentPage(0); this.advancedPaginator.resetNumberOfPages(completeSectionList.size()); this.updateCurrentSections(); }else{ this.message = "No items found for the term(s): " + this.term; } } catch (Exception e) { internalError(e); } } } 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 void updateCurrentSections() { this.advancedPaginator.initCount(); int startRecord = this.advancedPaginator.getCurrentPage() * this.advancedPaginator.getItemsPerPage(); if((this.advancedPaginator.getCurrentPage() + 1) == this.advancedPaginator.getNumberOfPages()){ int mod = this.completeSectionList.size() % advancedPaginator.getItemsPerPage(); if(mod == 0){ this.currentSectionList = completeSectionList.subList(startRecord, startRecord + this.advancedPaginator.getItemsPerPage()); }else{ this.currentSectionList = completeSectionList.subList(startRecord, startRecord + mod); } }else{ this.currentSectionList = completeSectionList.subList(startRecord, startRecord + this.advancedPaginator.getItemsPerPage()); } for(DBSection section : this.currentSectionList){ try { section.setBook(DBService.getBook(section.getBookId())); } catch (SQLException e) { e.printStackTrace(); } section.setBranches(this.branchesMap.get(section.getId())); } } public String advancedFirst() { this.advancedPaginator.first(); this.updateCurrentSections(); return GOTO_SEARCH_PAGE; } public String advancedLast() { this.advancedPaginator.last(); this.updateCurrentSections(); return GOTO_SEARCH_PAGE; } public String advancedFastForward() { this.advancedPaginator.fastForward(); this.updateCurrentSections(); return GOTO_SEARCH_PAGE; } public String advancedFastRewind() { this.advancedPaginator.fastRewind(); this.updateCurrentSections(); return GOTO_SEARCH_PAGE; } public String advancedPrevious() { this.advancedPaginator.previous(); this.updateCurrentSections(); return GOTO_SEARCH_PAGE; } public String advancedNext() { this.advancedPaginator.next(); this.updateCurrentSections(); return GOTO_SEARCH_PAGE; } /* public void reset(){ this.completeSectionList = new ArrayList<DBSection>(); this.currentSectionList = new ArrayList<DBSection>(); this.message = new String(); }*/ private List<String> splitTerms(){ List<String> rs = new ArrayList<String>(); String[] array = this.term.split(","); for(String tmp : array){ tmp = tmp.replace(" ", ""); if(StringUtils.isNotEmpty(tmp)){ rs.add(tmp); } } return rs; } public String getTerm() { return term; } public void setTerm(String term) { this.term = term; } public List<SelectItem> getSectionSuggestion() { return sectionSuggestion; } public void setSectionSuggestion(List<SelectItem> sectionSuggestion) { this.sectionSuggestion = sectionSuggestion; } public List<DBSection> getCompleteSectionList() { return completeSectionList; } public void setCompleteSectionList(List<DBSection> completeSectionList) { this.completeSectionList = completeSectionList; } public List<DBSection> getCurrentSectionList() { return currentSectionList; } public void setCurrentSectionList(List<DBSection> currentSectionList) { this.currentSectionList = currentSectionList; } public DataPaginator getAdvancedPaginator() { return advancedPaginator; } public void setAdvancedPaginator(DataPaginator advancedPaginator) { this.advancedPaginator = advancedPaginator; } public String getMessage() { return message; } }