diff 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 diff
--- a/src/main/java/de/mpiwg/web/SearchBean.java	Wed Mar 11 16:32:06 2015 +0100
+++ b/src/main/java/de/mpiwg/web/SearchBean.java	Mon Mar 16 11:25:36 2015 +0100
@@ -14,33 +14,34 @@
 import org.apache.log4j.Logger;
 import org.icefaces.ace.event.TextChangeEvent;
 
-import com.icesoft.faces.component.jseventlistener.JSEventListener;
-
 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> sectionList;
+	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());
+		//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){
@@ -61,20 +62,18 @@
 		this.search();
 	}
 	
+	
 	private void search(){
-		this.message = null;
 		if(StringUtils.isNotEmpty(this.term)){
 			this.loadBranches();
 			try {
 				List<String> terms = splitTerms();
-				this.sectionList = DBService.searchSection(terms);
-				
-				for(DBSection section : this.sectionList){
-					section.setBranches(this.branchesMap.get(section.getId()));
-				}
-				
-				if(sectionList.size() > 0){
-					this.message = sectionList.size() + " item(s) found for the term(s): " + this.term;
+				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;
 				}
@@ -83,7 +82,6 @@
 				internalError(e);
 			}			
 		}
-
 	}
 	
 	private void loadBranches(){
@@ -98,6 +96,76 @@
 		}
 	}
 	
+	
+	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(",");
@@ -126,13 +194,31 @@
 	public void setSectionSuggestion(List<SelectItem> sectionSuggestion) {
 		this.sectionSuggestion = sectionSuggestion;
 	}
+	
+	
 
-	public List<DBSection> getSectionList() {
-		return sectionList;
+	public List<DBSection> getCompleteSectionList() {
+		return completeSectionList;
+	}
+
+	public void setCompleteSectionList(List<DBSection> completeSectionList) {
+		this.completeSectionList = completeSectionList;
 	}
 
-	public void setSectionList(List<DBSection> sectionList) {
-		this.sectionList = sectionList;
+	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() {