view src/main/java/de/mpiwg/web/jsp/HomePage.java @ 62:824b808a7481

improvements and bug fixed
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Mon, 18 Jul 2016 17:35:32 +0200
parents b8ad346e39a0
children fc5116de601f
line wrap: on
line source

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.bo.LGTopicSectionRelation;
import de.mpiwg.gazetteer.db.DBBook;
import de.mpiwg.gazetteer.db.DBSection;
import de.mpiwg.gazetteer.utils.DBService;
import de.mpiwg.gazetteer.utils.DataProvider;
import de.mpiwg.web.books.SortBooksByBookId;
import de.mpiwg.web.branch.SortBranchByAdminType;
import de.mpiwg.web.branch.SortBranchByBookName;
import de.mpiwg.web.branch.SortBranchByBranchId;
import de.mpiwg.web.branch.SortBranchByDynasty;
import de.mpiwg.web.branch.SortBranchByLabel;
import de.mpiwg.web.branch.SortBranchByLastModified;
import de.mpiwg.web.branch.SortBranchByLevel1;
import de.mpiwg.web.branch.SortBranchByLevel2;
import de.mpiwg.web.branch.SortBranchByPeriod;
import de.mpiwg.web.branch.SortBranchByPublishedInDataverse;
import de.mpiwg.web.branch.SortBranchBySectionName;
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 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");
		this.level1Filter = getParameter("level1Filter");
		this.level2Filter = getParameter("level2Filter");
		this.periodFilter = getParameter("periodFilter");
		this.sectionNameFilter = getParameter("sectionNameFilter");
		this.labelFilter = getParameter("labelFilter");
		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())){
				branch.loadTransientData();
				// section of this branch may be deleted. In this case, we don't add the branch to branches list
				// TODO might let user to delete the branch?
				if (branch.isEmpty()) {
					logger.debug("section of the branch doesn't exist anymore.");
				} else {
					this.completeBranchList.add(branch);
				}
			}
		}
		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){
				this.topicSectionRelationMap.put(relation.getSectionId(), new ArrayList<LGTopicSectionRelation>());
			}
			this.topicSectionRelationMap.get(relation.getSectionId()).add(relation);
		}
	}

	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;
	}

	public void setBranchId(Long branchId) {
		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;
	}

	public String getBookIdFilter() {
		return bookIdFilter;
	}


	public void setBookIdFilter(String bookIdFilter) {
		this.bookIdFilter = bookIdFilter;
	}


	private void updateCurrentBranches() {
		this.displayBranchList = this.filteredBranchList;
		
	}

	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)) &&
					(StringUtils.isEmpty(dynastyFilter) || StringUtils.startsWith(branch.getBook().getDynasty(), dynastyFilter)) &&
					(StringUtils.isEmpty(adminTypeFilter) || StringUtils.startsWith(branch.getBook().getAdmin_type(), adminTypeFilter)) &&
					(StringUtils.isEmpty(bookIdFilter) || StringUtils.startsWith(branch.getBook().getId(), bookIdFilter)) &&
					(StringUtils.isEmpty(periodFilter) || StringUtils.startsWith(branch.getBook().getPeriod(), periodFilter)) &&
					(StringUtils.isEmpty(sectionNameFilter) || StringUtils.startsWith(branch.getSection().getName(), sectionNameFilter)) &&
					(StringUtils.isEmpty(labelFilter) || StringUtils.startsWith(branch.getLabel(), labelFilter))
					){

					this.filteredBranchList.add(branch);
					
				}
			}
		}
		
		if(completeBranchList.size() > 0){
			this.filteringMessage = this.filteredBranchList.size() + " tasks listed after filtering";
			this.paginator.setCurrentPage(0);
			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){
			String bookName = branch.getBook().getName();
			if(!list.contains(bookName) && bookName.startsWith(term)){
				list.add(bookName);
			}
			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){
			String level1 = branch.getBook().getLevel1();
			if(!list.contains(level1) && level1.startsWith(term)){
				list.add(level1);
			}
			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){
			String level2 = branch.getBook().getLevel2();
			if(!list.contains(level2) && level2.startsWith(term)){
				list.add(level2);
			}
			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){
			String dynasty = branch.getBook().getDynasty();
			if(!list.contains(dynasty) && dynasty.startsWith(term)){
				list.add(dynasty);
			}
			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){
			String period = branch.getBook().getPeriod();
			if(!list.contains(period) && period.startsWith(term)){
				list.add(period);
			}
			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){
			String adminType = branch.getBook().getAdmin_type();
			if(!list.contains(adminType) && adminType.startsWith(term)){
				list.add(adminType);
			}
			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){
			String sectionName = branch.getSection().getName();
			if(!list.contains(sectionName) && sectionName.startsWith(term)){
				list.add(sectionName);
			}
			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){
			String label = branch.getLabel();
			if(!list.contains(label) && label.startsWith(term)){
				list.add(label);
			}
			if(limit == list.size()){
				break;
			}	
		}
		return list;
	}
	
	
	
	
	
	public void sortByBranchIdUp() {
		Collections.sort(this.completeBranchList, new SortBranchByBranchId());
		filter();
	}

	public void sortByBranchIdDown() {
		Collections.sort(this.completeBranchList, new SortBranchByBranchId());
		Collections.reverse(this.completeBranchList);
		filter();
	}

	
	public void sortByBookNameUp() {
		Collections.sort(this.completeBranchList, new SortBranchByBookName());
		filter();
	}

	public void sortByBookNameDown() {
		Collections.sort(this.completeBranchList, new SortBranchByBookName());
		Collections.reverse(this.completeBranchList);
		filter();
	}

	public void sortByLevel1Up() {
		Collections.sort(this.completeBranchList, new SortBranchByLevel1());
		filter();
	}

	public void sortByLevel1Down() {
		Collections.sort(this.completeBranchList, new SortBranchByLevel1());
		Collections.reverse(this.completeBranchList);
		filter();
	}
	public void sortByLevel2Up() {
		Collections.sort(this.completeBranchList, new SortBranchByLevel2());
		filter();
	}

	public void sortByLevel2Down() {
		Collections.sort(this.completeBranchList, new SortBranchByLevel2());
		Collections.reverse(this.completeBranchList);
		filter();
	}
	public void sortByDynastyUp() {
		Collections.sort(this.completeBranchList, new SortBranchByDynasty());
		filter();
	}
	public void sortByDynastyDown() {
		Collections.sort(this.completeBranchList, new SortBranchByDynasty());
		Collections.reverse(this.completeBranchList);
		filter();
	}
	public void sortByAdminTypeUp() {
		Collections.sort(this.completeBranchList, new SortBranchByAdminType());
		filter();
	}
	public void sortByAdminTypeDown() {
		Collections.sort(this.completeBranchList, new SortBranchByAdminType());
		Collections.reverse(this.completeBranchList);
		filter();
	}
	public void sortBySectionStartPageUp() {
		Collections.sort(this.completeBranchList, new SortBranchBySectionStartPage());
		filter();
	}
	public void sortBySectionStartPageDown() {
		Collections.sort(this.completeBranchList, new SortBranchBySectionStartPage());
		Collections.reverse(this.completeBranchList);
		filter();
	}
	
	
	public void sortByPeriodUp() {
		Collections.sort(this.completeBranchList, new SortBranchByPeriod());
		filter();
	}

	public void sortByPeriodDown() {
		Collections.sort(this.completeBranchList, new SortBranchByPeriod());
		Collections.reverse(this.completeBranchList);
		filter();
	}
	public void sortBySectionNameUp() {
		Collections.sort(this.completeBranchList, new SortBranchBySectionName());
		filter();
	}

	public void sortBySectionNameDown() {
		Collections.sort(this.completeBranchList, new SortBranchBySectionName());
		Collections.reverse(this.completeBranchList);
		filter();
	}
	public void sortByLabelUp() {
		Collections.sort(this.completeBranchList, new SortBranchByLabel());
		filter();
	}

	public void sortByLabelDown() {
		Collections.sort(this.completeBranchList, new SortBranchByLabel());
		Collections.reverse(this.completeBranchList);
		filter();
	}
	public void sortByLastModifiedUp() {
		Collections.sort(this.completeBranchList, new SortBranchByLastModified());
		filter();
	}

	public void sortByLastModifiedDown() {
		Collections.sort(this.completeBranchList, new SortBranchByLastModified());
		Collections.reverse(this.completeBranchList);
		filter();
	}
	public void sortByPublishedInDataverseUp() {
		Collections.sort(this.completeBranchList, new SortBranchByPublishedInDataverse());
		filter();
	}

	public void sortByPublishedInDataverseDown() {
		Collections.sort(this.completeBranchList, new SortBranchByPublishedInDataverse());
		Collections.reverse(this.completeBranchList);
		filter();
	}

	public int getBranchNumber() {
		return branchNumber;
	}


	public void setBranchNumber(int branchNumber) {
		this.branchNumber = branchNumber;
	}


	public String getFilteringMessage() {
		return filteringMessage;
	}


	public void setFilteringMessage(String filteringMessage) {
		this.filteringMessage = filteringMessage;
	}


	public String getLabelFilter() {
		return labelFilter;
	}


	public void setLabelFilter(String labelFilter) {
		this.labelFilter = labelFilter;
	}


	public DataPaginator getPaginator() {
		return paginator;
	}


	public void setPaginator(DataPaginator paginator) {
		this.paginator = paginator;
	}


	public String getLevel1Filter() {
		return level1Filter;
	}


	public void setLevel1Filter(String level1Filter) {
		this.level1Filter = level1Filter;
	}


	public String getLevel2Filter() {
		return level2Filter;
	}


	public void setLevel2Filter(String level2Filter) {
		this.level2Filter = level2Filter;
	}


	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;
	}


	
	
	
}