Mercurial > hg > LGServices
view src/main/java/de/mpiwg/web/jsp/TopicListPage.java @ 86:53ca65aad5f7
Add getDefaultTopicId()
author | Calvin Yeh <cyeh@mpipw-berlin.mpg.com> |
---|---|
date | Fri, 19 May 2017 20:09:47 +0200 |
parents | db737aead627 |
children |
line wrap: on
line source
package de.mpiwg.web.jsp; import java.util.ArrayList; import java.util.Collections; import java.util.List; 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.LGTopic; import de.mpiwg.gazetteer.utils.DataProvider; import de.mpiwg.web.topicList.SortByDescription; import de.mpiwg.web.topicList.SortByLastModified; import de.mpiwg.web.topicList.SortByNameCh; import de.mpiwg.web.topicList.SortByNameEn; import de.mpiwg.web.topicList.SortByNamePinyin; import de.mpiwg.web.topicList.SortByTopicId; public class TopicListPage extends AbstractJSPPage{ private static Logger logger = Logger.getLogger(TopicListPage.class); public static String bean = "topicListBean"; public static String page = "pages/topicList.jsp"; private List<LGTopic> completeTopicList; // complete topic list private List<LGTopic> filteredTopicList; private List<LGTopic> displayTopicList; private Long topicId; private int topicNumber; private String filteringMessage; private DataPaginator paginator = new DataPaginator(); public void loadParameters(HttpServletRequest request, HttpServletResponse response){ this.request = request; this.response = response; this.topicId = getLongParameter("topicId"); } public void reloadTopics(){ logger.debug("reloadTopics"); if (this.completeTopicList == null) { this.forceLoadTopics(); this.filteringMessage = null; } return; } public void forceLoadTopics(){ logger.debug("forceLoadTopics"); DataProvider.getInstance().setTopicMap(null); DataProvider.getInstance().setTopicSectionRelationMap(null); this.completeTopicList = new ArrayList<LGTopic>(); // sometimes this.getSessionBean() will cause NullPointerException. Fixed. if(getSessionBean().getUser() != null){ for(LGTopic topic : DataProvider.getInstance().getTopics(this.getSessionBean().getUser().getId())){ if (topic.isEmpty()) { logger.debug("topic doesn't exist anymore."); } else { this.completeTopicList.add(topic); } } this.setTopicNumber(this.completeTopicList.size()); sortByLastModifiedDown(); // default sorting by from lasted modified to oldest } } public void createTopic(Long userId){ logger.debug("createTopic"); String nameEn = getParameter("nameEn"); String nameCh = getParameter("nameCh"); String namePinyin = getParameter("namePinyin"); String description = getParameter("description"); logger.debug("nameEn: " + nameEn + "nameCh: " + nameCh + "namePinyin: " + namePinyin + "description: " + description); if (StringUtils.isEmpty(nameEn)) { addMsg("Creating topic Failed. Name(Eng) cannot be empty."); } else if (StringUtils.isEmpty(nameCh)) { addMsg("Creating topic Failed. Name(中文) cannot be empty."); } else if (StringUtils.isEmpty(namePinyin)) { addMsg("Creating topic Failed. Name(Pinyin) cannot be empty."); } else { DataProvider.getInstance().createTopic(nameEn, nameCh, namePinyin, description, userId); addMsg("New topic has been created."); this.forceLoadTopics(); } } public void deleteTopic(){ logger.debug("deleteTopic " + topicId); if(topicId != null){ LGTopic topic = DataProvider.getInstance().getTopic(topicId); if(topic != null){ DataProvider.getInstance().deleteTopic(topic); this.forceLoadTopics(); } } } public void filter(){ this.filteredTopicList = new ArrayList<LGTopic>(); for (LGTopic topic : this.completeTopicList) { if(!this.filteredTopicList.contains(topic)){ /* 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.filteredTopicList.add(branch); } */ this.filteredTopicList.add(topic); } } if(completeTopicList.size() > 0){ //this.filteringMessage = this.filteredTopicList.size() + " topics listed after filtering"; this.paginator.setCurrentPage(0); this.paginator.resetNumberOfPages(filteredTopicList.size()); }else{ this.filteredTopicList = null; } this.updateCurrentTopics(); } private void updateCurrentTopics() { this.paginator.initCount(); int startRecord = this.paginator.getCurrentPage() * this.paginator.getItemsPerPage(); if(this.paginator.getNumberOfPages() == 0){ this.displayTopicList = new ArrayList<LGTopic>(); }else if((this.paginator.getCurrentPage() + 1) == this.paginator.getNumberOfPages()){ int mod = this.filteredTopicList.size() % paginator.getItemsPerPage(); if(mod == 0){ this.displayTopicList = filteredTopicList.subList(startRecord, startRecord + this.paginator.getItemsPerPage()); }else{ this.displayTopicList = filteredTopicList.subList(startRecord, startRecord + mod); } }else{ this.displayTopicList = filteredTopicList.subList(startRecord, startRecord + this.paginator.getItemsPerPage()); } /* for(LGTopic topic : this.displayTopicList){ topic.setTopic(this.topicMap.get(topic.getId())); } */ } // TODO sort public void sortByTopicIdUp() { Collections.sort(this.completeTopicList, new SortByTopicId()); filter(); } public void sortByTopicIdDown() { Collections.sort(this.completeTopicList, new SortByTopicId()); Collections.reverse(this.completeTopicList); filter(); } public void sortByLastModifiedUp() { Collections.sort(this.completeTopicList, new SortByLastModified()); filter(); } public void sortByLastModifiedDown() { Collections.sort(this.completeTopicList, new SortByLastModified()); Collections.reverse(this.completeTopicList); filter(); } public void sortByNameChUp() { Collections.sort(this.completeTopicList, new SortByNameCh()); filter(); } public void sortByNameChDown() { Collections.sort(this.completeTopicList, new SortByNameCh()); Collections.reverse(this.completeTopicList); filter(); } public void sortByNameEnUp() { Collections.sort(this.completeTopicList, new SortByNameEn()); filter(); } public void sortByNameEnDown() { Collections.sort(this.completeTopicList, new SortByNameEn()); Collections.reverse(this.completeTopicList); filter(); } public void sortByNamePinyinUp() { Collections.sort(this.completeTopicList, new SortByNamePinyin()); filter(); } public void sortByNamePinyinDown() { Collections.sort(this.completeTopicList, new SortByNamePinyin()); Collections.reverse(this.completeTopicList); filter(); } public void sortByDescriptionUp() { Collections.sort(this.completeTopicList, new SortByDescription()); filter(); } public void sortByDescriptionDown() { Collections.sort(this.completeTopicList, new SortByDescription()); Collections.reverse(this.completeTopicList); filter(); } // ---- public List<LGTopic> getCompleteTopicList() { return completeTopicList; } public void setCompleteTopicList(List<LGTopic> completeTopicList) { this.completeTopicList = completeTopicList; } public long getDefaultTopicId(long userId){ long result = 0; for(LGTopic aTopic : this.completeTopicList){ if(aTopic.getUserId().longValue() == userId){ result = aTopic.getId().longValue(); break; } } if(result == 0) result = this.completeTopicList.get(0).getId().longValue(); return result; } public List<LGTopic> getFilteredTopicList() { return filteredTopicList; } public void setFilteredTopicList(List<LGTopic> filteredTopicList) { this.filteredTopicList = filteredTopicList; } public List<LGTopic> getDisplayTopicList() { return displayTopicList; } public void setDisplayTopicList(List<LGTopic> displayTopicList) { this.displayTopicList = displayTopicList; } public Long getTopicId() { return topicId; } public void setTopicId(Long topicId) { this.topicId = topicId; } public String getFilteringMessage() { return filteringMessage; } public void setFilteringMessage(String filteringMessage) { this.filteringMessage = filteringMessage; } public DataPaginator getPaginator() { return paginator; } public void setPaginator(DataPaginator paginator) { this.paginator = paginator; } public int getTopicNumber() { return topicNumber; } public void setTopicNumber(int topicNumber) { this.topicNumber = topicNumber; } }