changeset 82:9649f29eb4b0

Add pagination function for Task page
author Calvin Yeh <cyeh@mpiwg-berlin.mpg.de>
date Thu, 04 May 2017 14:47:20 +0200
parents 881461e4f37d
children 6641376e2a72
files src/main/java/de/mpiwg/web/jsp/HomePage.java src/main/java/de/mpiwg/web/jsp/JSPProxy.java
diffstat 2 files changed, 128 insertions(+), 91 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/web/jsp/HomePage.java	Thu May 04 14:45:32 2017 +0200
+++ b/src/main/java/de/mpiwg/web/jsp/HomePage.java	Thu May 04 14:47:20 2017 +0200
@@ -32,45 +32,42 @@
 import de.mpiwg.web.branch.SortBranchBySectionStartPage;
 
 public class HomePage  extends AbstractJSPPage{
-	
+
 	private static Logger logger = Logger.getLogger(HomePage.class);
-	
+
 	public static String bean = "homeBean";
 	public static String page = "pages/home.jsp";
-	
-	
+
+
 	private List<LGBranch> completeBranchList;	// complete branch list
 	private Long branchId;
-	
+
 	private List<LGBranch> filteredBranchList;
 
 	private List<LGBranch> displayBranchList;
-	
+
 	private String bookNameFilter = new String();
 	private String level1Filter = new String();
-	private String level2Filter = new String();	
-	private String dynastyFilter = new String();	
-	private String adminTypeFilter = new String();	
+	private String level2Filter = new String();
+	private String dynastyFilter = new String();
+	private String adminTypeFilter = new String();
 	private String bookIdFilter = new String();
 	private String periodFilter = new String();
 	private String sectionNameFilter = new String();
 	private String labelFilter = new String();
-	
-	
-	
+
 	private int branchNumber;
-	
+
 	private String filteringMessage;
 
 	private DataPaginator paginator = new DataPaginator();
 
 	private Map<Long, List<LGTopicSectionRelation>> topicSectionRelationMap;
-	
-	
+
 	public void loadParameters(HttpServletRequest request, HttpServletResponse response){
 		this.request = request;
 		this.response = response;
-		
+
 		this.branchId = getLongParameter("branchId");
 		this.bookIdFilter = getParameter("bookIdFilter");
 		this.bookNameFilter = getParameter("bookNameFilter");
@@ -82,33 +79,31 @@
 		this.dynastyFilter = getParameter("dynastyFilter");
 		this.adminTypeFilter = getParameter("adminTypeFilter");
 	}
-	
-	
+
 	public void reloadBranches(){
 		logger.debug("reloadBranches");
-		
+
 		if (this.completeBranchList == null) {
 			this.forceLoadBranches();
-		
+
 			this.filteringMessage = null;
 		} else {
-	
+
 			this.loadTopicSectionRelation();
-							
+
 			for(LGBranch branch : this.completeBranchList){
 				branch.getSection().setTopicSectionRelation(this.topicSectionRelationMap.get(branch.getSection().getId()));
 			}
-			
+
 		}
-	
+
 		return;
 	}
-	
-	
+
 	public void forceLoadBranches(){
 		logger.debug("forceLoadBranches");
 		this.filteringMessage = null;
-		
+
 		this.completeBranchList = new ArrayList<LGBranch>();
 		if(getSessionBean().getUser() != null){
 			for(LGBranch branch : DataProvider.getInstance().getBranches(getSessionBean().getUser().getId())){
@@ -123,22 +118,22 @@
 			}
 		}
 		this.setBranchNumber(this.completeBranchList.size());
-		
+
 		sortByLastModifiedDown();	// default sorting by from lasted modified to oldest
-		
+
 		// loadTopicSectionRelation?
 		this.loadTopicSectionRelation();
-									
+
 		for(LGBranch branch : this.completeBranchList){
 			branch.getSection().setTopicSectionRelation(this.topicSectionRelationMap.get(branch.getSection().getId()));
 		}
-		
+
 	}
-	
+
 	private void loadTopicSectionRelation(){
 		this.topicSectionRelationMap = new HashMap<Long, List<LGTopicSectionRelation>>();
 		List<LGTopicSectionRelation> list = DataProvider.getInstance().getAllExistingTopicSectionRelation();
-		
+
 		for(LGTopicSectionRelation relation : list){
 			relation.loadTransientData();
 			if(this.topicSectionRelationMap.get(relation.getSectionId()) == null){
@@ -150,52 +145,42 @@
 
 	public void deleteBranch(){
 		logger.debug("deleteBranch " + branchId);
-		
+
 		if(branchId != null){
 			LGBranch branch = DataProvider.getInstance().getBranch(branchId);
 			if(branch != null){
 				DataProvider.getInstance().deleteBranch(branch);
 				//this.reloadBranches();
 				this.forceLoadBranches();
-				
-			}	
+
+			}
 		}
 	}
-	
-	
-
-
 
 	public List<LGBranch> getFilteredBranchList() {
 		return filteredBranchList;
 	}
 
-
 	public void setFilteredBranchList(List<LGBranch> filteredBranchList) {
 		this.filteredBranchList = filteredBranchList;
 	}
 
-
 	public List<LGBranch> getDisplayBranchList() {
 		return displayBranchList;
 	}
 
-
 	public void setDisplayBranchList(List<LGBranch> displayBranchList) {
 		this.displayBranchList = displayBranchList;
 	}
 
-
 	public List<LGBranch> getCompleteBranchList() {
 		return completeBranchList;
 	}
 
-
 	public void setCompleteBranchList(List<LGBranch> completeBranchList) {
 		this.completeBranchList = completeBranchList;
 	}
 
-
 	public Long getBranchId() {
 		return branchId;
 	}
@@ -204,42 +189,34 @@
 		this.branchId = branchId;
 	}
 
-
 	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<LGBranch> getFilteredBranches() {
 		return filteredBranchList;
 	}
 
-
 	public void setFilteredBranches(List<LGBranch> filteredBranches) {
 		this.filteredBranchList = filteredBranches;
 	}
 
-
 	public String getBookNameFilter() {
 		return bookNameFilter;
 	}
 
-
 	public void setBookNameFilter(String bookNameFilter) {
 		this.bookNameFilter = bookNameFilter;
 	}
@@ -248,21 +225,48 @@
 		return bookIdFilter;
 	}
 
-
 	public void setBookIdFilter(String bookIdFilter) {
 		this.bookIdFilter = bookIdFilter;
 	}
 
+	private void updateCurrentBranches() {
 
-	private void updateCurrentBranches() {
-		this.displayBranchList = this.filteredBranchList;
-		
+		//this.displayBranchList = this.filteredBranchList;
+
+		this.paginator.initCount();
+
+		int startRecord = this.paginator.getCurrentPage() * this.paginator.getItemsPerPage();
+
+		if(this.paginator.getNumberOfPages() == 0){
+
+			this.displayBranchList = new ArrayList<LGBranch>();
+
+		}
+		else if((this.paginator.getCurrentPage() + 1) == this.paginator.getNumberOfPages()){
+
+			int mod = this.filteredBranchList.size() % paginator.getItemsPerPage();
+
+			if(mod == 0){
+				this.displayBranchList = filteredBranchList.subList(startRecord, startRecord + this.paginator.getItemsPerPage());
+			}
+			else{
+				this.displayBranchList = filteredBranchList.subList(startRecord, startRecord + mod);
+			}
+		}
+		else{
+			this.displayBranchList = filteredBranchList.subList(startRecord, startRecord + this.paginator.getItemsPerPage());
+		}
+
 	}
 
 	public void filter(){
+
 		this.filteredBranchList = new ArrayList<LGBranch>();
+
 		for (LGBranch branch : this.completeBranchList) {
+
 			if(!this.filteredBranchList.contains(branch)){
+
 				if( (StringUtils.isEmpty(bookNameFilter) || StringUtils.startsWith(branch.getBook().getName(), bookNameFilter)) &&
 					(StringUtils.isEmpty(level1Filter) || StringUtils.startsWith(branch.getBook().getLevel1(), level1Filter)) &&
 					(StringUtils.isEmpty(level2Filter) || StringUtils.startsWith(branch.getBook().getLevel2(), level2Filter)) &&
@@ -275,24 +279,25 @@
 					){
 
 					this.filteredBranchList.add(branch);
-					
+
 				}
 			}
 		}
-		
+
 		if(completeBranchList.size() > 0){
+
 			this.filteringMessage = this.filteredBranchList.size() + " tasks listed after filtering";
 			this.paginator.setCurrentPage(0);
+			this.paginator.setItemsPerPage(25);
 			this.paginator.resetNumberOfPages(filteredBranchList.size());
 			this.updateCurrentBranches();
-			
+
 		}else{
 			this.filteredBranchList = null;
 		}
-	
+
 	}
-	
-	
+
 	public List<String> suggestBookName(String term, int limit){
 		List<String> list = new ArrayList<String>();
 		for(LGBranch branch : this.completeBranchList){
@@ -302,11 +307,11 @@
 			}
 			if(limit == list.size()){
 				break;
-			}	
+			}
 		}
 		return list;
 	}
-	
+
 	public List<String> suggestLevel1(String term, int limit){
 		List<String> list = new ArrayList<String>();
 		for(LGBranch branch : this.completeBranchList){
@@ -316,11 +321,11 @@
 			}
 			if(limit == list.size()){
 				break;
-			}	
+			}
 		}
 		return list;
 	}
-	
+
 	public List<String> suggestLevel2(String term, int limit){
 		List<String> list = new ArrayList<String>();
 		for(LGBranch branch : this.completeBranchList){
@@ -330,11 +335,11 @@
 			}
 			if(limit == list.size()){
 				break;
-			}	
+			}
 		}
 		return list;
 	}
-	
+
 	public List<String> suggestDynasty(String term, int limit){
 		List<String> list = new ArrayList<String>();
 		for(LGBranch branch : this.completeBranchList){
@@ -344,11 +349,11 @@
 			}
 			if(limit == list.size()){
 				break;
-			}	
+			}
 		}
 		return list;
 	}
-	
+
 	public List<String> suggestPeriod(String term, int limit){
 		List<String> list = new ArrayList<String>();
 		for(LGBranch branch : this.completeBranchList){
@@ -358,11 +363,11 @@
 			}
 			if(limit == list.size()){
 				break;
-			}	
+			}
 		}
 		return list;
 	}
-	
+
 	public List<String> suggestAdminType(String term, int limit){
 		List<String> list = new ArrayList<String>();
 		for(LGBranch branch : this.completeBranchList){
@@ -372,11 +377,11 @@
 			}
 			if(limit == list.size()){
 				break;
-			}	
+			}
 		}
 		return list;
 	}
-	
+
 	public List<String> suggestSectionName(String term, int limit){
 		List<String> list = new ArrayList<String>();
 		for(LGBranch branch : this.completeBranchList){
@@ -386,11 +391,11 @@
 			}
 			if(limit == list.size()){
 				break;
-			}	
+			}
 		}
 		return list;
 	}
-	
+
 	public List<String> suggestLabel(String term, int limit){
 		List<String> list = new ArrayList<String>();
 		for(LGBranch branch : this.completeBranchList){
@@ -400,15 +405,11 @@
 			}
 			if(limit == list.size()){
 				break;
-			}	
+			}
 		}
 		return list;
 	}
-	
-	
-	
-	
-	
+
 	public void sortByBranchIdUp() {
 		Collections.sort(this.completeBranchList, new SortBranchByBranchId());
 		filter();
@@ -431,7 +432,6 @@
 		filter();
 	}
 
-	
 	public void sortByBookNameUp() {
 		Collections.sort(this.completeBranchList, new SortBranchByBookName());
 		filter();
@@ -490,8 +490,7 @@
 		Collections.reverse(this.completeBranchList);
 		filter();
 	}
-	
-	
+
 	public void sortByPeriodUp() {
 		Collections.sort(this.completeBranchList, new SortBranchByPeriod());
 		filter();
@@ -622,8 +621,33 @@
 		this.adminTypeFilter = adminTypeFilter;
 	}
 
+	public void firstPage() {
+		this.paginator.first();
+		this.updateCurrentBranches();
+	}
 
-	
-	
-	
+	public void lastPage() {
+		this.paginator.last();
+		this.updateCurrentBranches();
+	}
+
+	public void fastForward() {
+		this.paginator.fastForward();
+		this.updateCurrentBranches();
+	}
+
+	public void fastRewind() {
+		this.paginator.fastRewind();
+		this.updateCurrentBranches();
+	}
+
+	public void previousPage() {
+		this.paginator.previous();
+		this.updateCurrentBranches();
+	}
+
+	public void nextPage() {
+		this.paginator.next();
+		this.updateCurrentBranches();
+	}
 }
--- a/src/main/java/de/mpiwg/web/jsp/JSPProxy.java	Thu May 04 14:45:32 2017 +0200
+++ b/src/main/java/de/mpiwg/web/jsp/JSPProxy.java	Thu May 04 14:47:20 2017 +0200
@@ -158,8 +158,21 @@
 					getSessionBean().getHomePage().sortByPublishedInDataverseUp();
 				} else if(StringUtils.equals(action, "sortByPublishedInDataverseDown")) {
 					getSessionBean().getHomePage().sortByPublishedInDataverseDown();
-				}
 
+				//PAGINATOR
+				} else if(StringUtils.equals(action, "firstPage")){
+					getSessionBean().getHomePage().firstPage();
+				} else if(StringUtils.equals(action, "fastRewind")){
+					getSessionBean().getHomePage().fastRewind();
+				} else if(StringUtils.equals(action, "previousPage")){
+					getSessionBean().getHomePage().previousPage();
+				} else if(StringUtils.equals(action, "nextPage")){
+					getSessionBean().getHomePage().nextPage();
+				} else if(StringUtils.equals(action, "fastForward")){
+					getSessionBean().getHomePage().fastForward();
+				} else if(StringUtils.equals(action, "lastPage")){
+					getSessionBean().getHomePage().lastPage();
+        }
 				return HomePage.page;