view src/main/java/de/mpiwg/web/jsp/TopicPage.java @ 75:e5697f7775b2

Display columns named "source" and "year" and implement filter & sorting on them. Hide the column "Start Year"
author Calvin Yeh <cyeh@mpiwg-berlin.mpg.de>
date Mon, 27 Mar 2017 17:50:58 +0200
parents 92ede90bc611
children
line wrap: on
line source

package de.mpiwg.web.jsp;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
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 cl.maps.duplex.DuplexKey;
import de.mpiwg.gazetteer.bo.LGBranch;
import de.mpiwg.gazetteer.bo.LGTopic;
import de.mpiwg.gazetteer.bo.LGTopicSectionRelation;
import de.mpiwg.gazetteer.dataverse.DataverseUtils;
import de.mpiwg.gazetteer.dataverse.bo.VDCUser;
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.search.SortSectionByAdminType;
import de.mpiwg.web.search.SortSectionByBookId;
import de.mpiwg.web.search.SortSectionByBookName;
import de.mpiwg.web.search.SortSectionByDynasty;
import de.mpiwg.web.search.SortSectionById;
import de.mpiwg.web.search.SortSectionByLevel1;
import de.mpiwg.web.search.SortSectionByLevel2;
import de.mpiwg.web.search.SortSectionByPeriod;
import de.mpiwg.web.search.SortSectionByStartPage;


public class TopicPage extends AbstractJSPPage{

	private static Logger logger = Logger.getLogger(TopicPage.class);
	
	public static String bean = "topicBean";
	public static String page = "pages/topicPage.jsp";
	
	private LGTopic topic;
	private Long topicId;
	
	private Long userId;

	private List<VDCUser> suggestionUserList;
	private List<VDCUser> contributors;
	
	private List<DBSection> completeSectionList;
	private List<DBSection> filteredSectionList;
	private List<DBSection> displaySectionList;
	
	
	private String dynastyFilter = new String();
	private String adminTypeFilter = new String();
	private String level1Filter = new String();
	private String level2Filter = new String();

	private String bookNameFilter = new String();
	private String bookIdFilter = new String();
	private String periodFilter = new String();
	private String sectionNameFilter = new String();

	
	private String dynastyFilterMissingBooks = new String();
	private String level1FilterMissingBooks = new String();
	private String level2FilterMissingBooks = new String();
	private String periodFilterMissingBooks = new String();
	private String adminTypeFilterMissingBooks = new String();
	private String bookNameFilterMissingBooks = new String();

	
	private DataPaginator paginator = new DataPaginator();
	private String filteringMessage;
	
	private Map<Long, List<LGBranch>> branchesMap;
	
	private Map<String, DBBook> booksMap;
	
	private List<DBBook> pendingBookList;

	private List<DBBook> filteredSectionListMissingBooks;

	
	
	public void loadParameters(HttpServletRequest request, HttpServletResponse response){

		this.request = request;
		this.response = response;
		this.userId = getLongParameter("userId");	
		this.topicId = getLongParameter("topicId");

		this.dynastyFilter = getParameter("dynastyFilter");
		this.adminTypeFilter = getParameter("adminTypeFilter");
		this.level1Filter = getParameter("level1Filter");
		this.level2Filter = getParameter("level2Filter");
		this.periodFilter = getParameter("periodFilter");
		this.sectionNameFilter = getParameter("sectionNameFilter");
		this.bookIdFilter = getParameter("bookIdFilter");
		this.bookNameFilter = getParameter("bookNameFilter");


		this.dynastyFilterMissingBooks = getParameter("dynastyFilterMissingBooks");
		this.level1FilterMissingBooks  = getParameter("level1FilterMissingBooks");
		this.level2FilterMissingBooks = getParameter("level2FilterMissingBooks");
		this.periodFilterMissingBooks = getParameter("periodFilterMissingBooks");
		this.adminTypeFilterMissingBooks = getParameter("adminTypeFilterMissingBooks");
		this.bookNameFilterMissingBooks = getParameter("bookNameFilterMissingBooks");
			
		
		
	}
	

	
	public void forceLoadTopicSectionRelation() {
		
		logger.debug("forceLoadTopicSectionRelation");
		DataProvider.getInstance().setTopicSectionRelationMap(null);
	
		this.loadTopic(this.topicId);
		
	}
	
	public void loadTopic(Long topicId0){

		LGTopic topic = DataProvider.getInstance().getTopic(topicId0);
		if(topic != null){
			this.loadTopic(topic);	
		}else{
			addMsg("topic [id=" + topicId + "] no found.");
		}
	}
	
	public void loadTopic(String topicId0){
		try {
			
			this.topicId = Long.parseLong(topicId0);
			
			this.loadTopic(this.topicId);
			
			
		} catch (Exception e) {
			internalError(e);
		}
	
	}
	
	public void addContributor(){
		if(userId != null){
			
			VDCUser user = DataverseUtils.getUser(userId);
			System.out.println("Adding user: " + user.getUserName());
			
			this.topic.addContributor(userId);
			this.saveTopic0();
		}
	}
	
	
	
	public void removeContributor(){
		if(userId != null){
			
			VDCUser user = DataverseUtils.getUser(userId);
			System.out.println("Removing user: " + user.getUserName());
			
			this.topic.removeContributor(userId);
			this.saveTopic0();
		}
	}
	
	private void saveTopic0(){
		try {
			DataProvider.getInstance().updateTopic(topic);
			this.loadTopic(topic);
			addMsg("The topic has been updated!");
		} catch (Exception e) {
			internalError(e);
		}
	}

	
	public void loadTopic(LGTopic topic){
		logger.info("Loading topic: " + topic.toString());
			
		if(topic != null && topic.isPersistent()){
			this.loadBranches();
			this.loadBooks();
			
			try {
				this.topic = (LGTopic)topic.clone();
				
				this.contributors = new ArrayList<VDCUser>();
				for(Long userId : this.topic.getContributorsList()){
					VDCUser user = DataverseUtils.getUser(userId);
					if(user != null){
						this.contributors.add(user);
					}
					
				}
				
				this.loadSuggestionUserList();
			
				// all sections in the topic
				this.completeSectionList = DataProvider.getInstance().getAllSectionsInTopic(topic.getId());
				logger.info("completeSectionList.size=" + completeSectionList.size());
				
			} catch (Exception e) {
				internalError(e);
			}
			
			
		}
		
		
		// pendingBookList
		for(DBSection section :this.completeSectionList) {
			String bookId = section.getBookId();
			
			if (this.booksMap.get(bookId) != null) {
				// remove the book with bookId from pendingBookList
				this.booksMap.remove(bookId);
			} 
		}
		
		this.pendingBookList = new ArrayList<DBBook>();
		this.pendingBookList.addAll(this.booksMap.values());
	
		this.filter();
		
	}
	
	

	public void updateDescription() {
		String description = getParameter("description");
		//logger.info("updateLabel: " + branchLabel + ", for branch id=" + this.getBranchId())
		this.getTopic().setDescription(description);
		
		this.saveTopic0();
		
	}




	public void deleteSection(Long sectionId) throws Exception {
		// delete the record with sectionId, topicId in topicSectionRelation database table

		logger.debug("delete sectionId=" + sectionId + " in topicId=" + this.topicId);
		
		DataProvider.getInstance().deleteTopicSectionRelation(this.topicId, sectionId);
		
		
		// update completeSectionList
		this.completeSectionList = DataProvider.getInstance().getAllSectionsInTopic(this.topicId);
		
		this.filter();
	}


	
	private void loadSuggestionUserList() throws Exception{
		this.suggestionUserList = new ArrayList<VDCUser>();
		for(VDCUser user : DataverseUtils.getAllUsers()){
			if(!topic.hasContributor(user.getId())){
				this.suggestionUserList.add(user);
			}
		}
	}
	
	public void reset(){
		this.topic = null;
		this.completeSectionList = null;
	}
	
	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(level2Filter) || StringUtils.startsWith(section.getBook().getLevel2(), level2Filter)) &&
						(StringUtils.isEmpty(bookIdFilter) || StringUtils.startsWith(section.getBookId(), bookIdFilter)) &&
						(StringUtils.isEmpty(bookNameFilter) || StringUtils.startsWith(section.getBook().getName(), bookNameFilter)) &&
						(StringUtils.isEmpty(periodFilter) || StringUtils.startsWith(section.getBook().getPeriod(), periodFilter)) &&
						(StringUtils.isEmpty(sectionNameFilter) || StringUtils.startsWith(section.getName(), sectionNameFilter)) &&
						(StringUtils.isEmpty(adminTypeFilter) || StringUtils.startsWith(section.getBook().getAdmin_type(), adminTypeFilter))
								){
					this.filteredSectionList.add(section);
				}
				
			}
		}
			
		if(completeSectionList.size() > 0){
		
			this.filteringMessage = this.filteredSectionList.size() + " section(s) listed after the filtering";

			this.paginator.setCurrentPage(0);
			this.paginator.resetNumberOfPages(filteredSectionList.size());
			
			this.updateCurrentSections();
			
		}else{
			this.filteredSectionList = null;
			this.filteringMessage = "";
				
			this.paginator.setCurrentPage(0);
			this.paginator.resetNumberOfPages(0);
		}
		
		
		// filter on pendingBookList 
		// complete filters for each field in Missing Books table
		if (this.pendingBookList != null) {
			
			this.filteredSectionListMissingBooks = new ArrayList<DBBook>();
			for(DBBook book : this.pendingBookList){
				if(!this.filteredSectionListMissingBooks.contains(book)){
						
					if( (StringUtils.isEmpty(bookNameFilterMissingBooks) || StringUtils.startsWith(book.getName(), bookNameFilterMissingBooks)) &&
						(StringUtils.isEmpty(dynastyFilterMissingBooks) || StringUtils.startsWith(book.getDynasty(), dynastyFilterMissingBooks)) &&
						(StringUtils.isEmpty(level1FilterMissingBooks) || StringUtils.startsWith(book.getLevel1(), level1FilterMissingBooks)) &&
						(StringUtils.isEmpty(level2FilterMissingBooks) || StringUtils.startsWith(book.getLevel2(), level2FilterMissingBooks)) &&
						(StringUtils.isEmpty(periodFilterMissingBooks) || StringUtils.startsWith(book.getPeriod(), periodFilterMissingBooks)) &&
						(StringUtils.isEmpty(adminTypeFilterMissingBooks) || StringUtils.startsWith(book.getAdmin_type(), adminTypeFilterMissingBooks)) 
							
							){
						
						this.filteredSectionListMissingBooks.add(book);
					}
					
				}
			}
		}
		
			
	}
	
	
	private void updateCurrentSections() {
		/* developing... */
		this.displaySectionList = this.filteredSectionList;
		
		/*
		
		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){// we don't paginate now
			section.setBranches(this.branchesMap.get(section.getId()));		
		}
		
		
	
	}
	
	

	public void addSectionToTopic(Long _sectionId, Long _topicId) throws Exception {
		logger.debug("selectedSectionId:" + _sectionId + ", selectedTopicId: " + _topicId);
		
		
		// create a new record in TopicSectionRelation table 
		String status = DataProvider.getInstance().updateTopicSectionRelation(_sectionId, _topicId);
		
		if (StringUtils.equals(status, "added") ) {
			addMsg("Added section [id=" + _sectionId + "] to the topic [id=" + _topicId + "]");
		} else if (StringUtils.equals(status, "updated") ) {
			addMsg("The section already in the topic");
		} 
	
		this.reset();
	}

	
	
	
	private void loadBranches(){
		this.branchesMap = new HashMap<Long, List<LGBranch>>();
		// List<LGBranch> list = DataProvider.getInstance().getBranches(getSessionBean().getUser().getId());
		List<LGBranch> list = DataProvider.getInstance().getAllExistingBranches();
		
		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 loadBooks(){
	
		List<DBBook> bookList = DBService.getInstance().getBooks();
		
		this.booksMap = new HashMap<String, DBBook>();
		
		for (DBBook book : bookList) {
			
			this.booksMap.put(book.getId(), book);
			
		}
			
	}
	
	

	public void addAllSectionsToTopic(List<DBSection> list, Long selectedTopicId) throws Exception {
		
		if (list == null) {
			return;
		}
		logger.debug("selectedTopicId: " + selectedTopicId + ", total " + list.size() + " sections.");
	
		Integer added = 0;
		Integer existing = 0;
		
		for (DBSection section : list) {
			// create a new record in TopicSectionRelation table 
			String status = DataProvider.getInstance().updateTopicSectionRelation(section.getId(), selectedTopicId);
			
			if (StringUtils.equals(status, "added") ) {
				added++;
			} else if (StringUtils.equals(status, "updated") ) {
				existing++;
			} 
			
		}
		
		addMsg("Added " + added.toString() + " sections. " + existing.toString() + " sections already in the topic.");
		
		this.reset();
	}

	
	
	
	
	public List<VDCUser> getContributors() {
		return contributors;
	}

	public void setContributors(List<VDCUser> contributors) {
		this.contributors = contributors;
	}

	public List<VDCUser> getSuggestionUserList() {
		return suggestionUserList;
	}
	public void setSuggestionUserList(List<VDCUser> suggestionUserList) {
		this.suggestionUserList = suggestionUserList;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}


	public LGTopic getTopic() {
		return topic;
	}



	public void setTopic(LGTopic topic) {
		this.topic = topic;
	}



	public Long getTopicId() {
		return topicId;
	}



	public void setTopicId(Long topicId) {
		this.topicId = topicId;
	}



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



	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 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 String getFilteringMessage() {
		return filteringMessage;
	}



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



	public Map<Long, List<LGBranch>> getBranchesMap() {
		return branchesMap;
	}



	public void setBranchesMap(Map<Long, List<LGBranch>> branchesMap) {
		this.branchesMap = branchesMap;
	}

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



	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 String getDynastyFilterMissingBooks() {
		return dynastyFilterMissingBooks;
	}



	public void setDynastyFilterMissingBooks(String dynastyFilterMissingBooks) {
		this.dynastyFilterMissingBooks = dynastyFilterMissingBooks;
	}


	public List<DBBook> getPendingBookList() {
		return pendingBookList;
	}



	public void setPendingBookList(List<DBBook> pendingBookList) {
		this.pendingBookList = pendingBookList;
	}



	public List<DBSection> getCompleteSectionList() {
		return completeSectionList;
	}

	public void setCompleteSectionList(List<DBSection> completeSectionList) {
		this.completeSectionList = completeSectionList;
	}



	public String getBookNameFilterMissingBooks() {
		return bookNameFilterMissingBooks;
	}



	public void setBookNameFilterMissingBooks(String bookNameFilterMissingBooks) {
		this.bookNameFilterMissingBooks = bookNameFilterMissingBooks;
	}



	public String getLevel1FilterMissingBooks() {
		return level1FilterMissingBooks;
	}



	public void setLevel1FilterMissingBooks(String level1FilterMissingBooks) {
		this.level1FilterMissingBooks = level1FilterMissingBooks;
	}



	public String getLevel2FilterMissingBooks() {
		return level2FilterMissingBooks;
	}



	public void setLevel2FilterMissingBooks(String level2FilterMissingBooks) {
		this.level2FilterMissingBooks = level2FilterMissingBooks;
	}



	public String getPeriodFilterMissingBooks() {
		return periodFilterMissingBooks;
	}



	public void setPeriodFilterMissingBooks(String periodFilterMissingBooks) {
		this.periodFilterMissingBooks = periodFilterMissingBooks;
	}



	public String getAdminTypeFilterMissingBooks() {
		return adminTypeFilterMissingBooks;
	}



	public void setAdminTypeFilterMissingBooks(String adminTypeFilterMissingBooks) {
		this.adminTypeFilterMissingBooks = adminTypeFilterMissingBooks;
	}




	public List<DBBook> getFilteredSectionListMissingBooks() {
		return filteredSectionListMissingBooks;
	}



	public void setFilteredSectionListMissingBooks(
			List<DBBook> filteredSectionListMissingBooks) {
		this.filteredSectionListMissingBooks = filteredSectionListMissingBooks;
	}






	/////// Sorting

	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 sortByPeriodUp(){
		Collections.sort(this.completeSectionList, new SortSectionByPeriod());
		filter();
	}
	
	public void sortByPeriodDown(){
		Collections.sort(this.completeSectionList, new SortSectionByPeriod());
		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 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 sortByLevel2Up(){
		Collections.sort(this.completeSectionList, new SortSectionByLevel2());
		filter();
	}
	
	public void sortByLevel2Down(){
		Collections.sort(this.completeSectionList, new SortSectionByLevel2());
		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 void updateKeywords() {
		String keywords = getParameter("keywords");
		this.getTopic().setKeywords(keywords);
		
		this.saveTopic0();
		
	}




	
	

	
	
}