diff src/main/java/de/mpiwg/web/jsp/TopicPage.java @ 41:ba9515f22897

new: topic management and adding sections from searching result into topic
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Thu, 17 Dec 2015 13:44:08 +0100
parents
children 815cd86bb9ec
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/mpiwg/web/jsp/TopicPage.java	Thu Dec 17 13:44:08 2015 +0100
@@ -0,0 +1,672 @@
+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 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.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 DataPaginator paginator = new DataPaginator();
+	private String filteringMessage;
+	
+	private Map<Long, List<LGBranch>> branchesMap;
+	
+	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");
+
+	}
+	
+
+	
+	public void forceLoadTopicSectionRelation() {
+		
+		logger.debug("forceLoadTopicSectionRelation");
+		DataProvider.getInstance().setTopicSectionRelationMap(null);
+		
+		this.loadTopic(this.topic);
+	}
+
+	
+	
+	public void loadTopic(String topicId0){
+		try {
+			
+			this.topicId = Long.parseLong(topicId0);
+			
+			LGTopic topic = DataProvider.getInstance().getTopic(topicId);
+			if(topic != null){
+				this.loadTopic(topic);	
+			}else{
+				addMsg("topic [id=" + topicId + "] no found.");
+			}
+			
+			
+		} 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();
+			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());
+				
+			} catch (Exception e) {
+				internalError(e);
+			}
+			
+			logger.info("completeSectionList.size=" + completeSectionList.size());
+		}
+		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=" + topicId);
+		
+		DataProvider.getInstance().deleteTopicSectionRelation(topicId, sectionId);
+		
+		
+		// update completeSectionList
+		this.completeSectionList = DataProvider.getInstance().getAllSectionsInTopic(topic.getId());
+		
+
+	}
+
+
+	
+	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);
+		}
+		
+			
+	}
+	
+	
+	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){
+			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") ) {
+			// TODO responding with names to user
+			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);
+		}
+	}
+	
+	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 List<DBSection> getCompleteSectionList() {
+		return completeSectionList;
+	}
+
+
+
+	public void setCompleteSectionList(List<DBSection> completeSectionList) {
+		this.completeSectionList = completeSectionList;
+	}
+
+
+
+	/////// 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();
+	}
+
+	
+	
+
+	
+	
+}