view src/main/java/de/mpiwg/web/jsp/SearchPage.java @ 105:16a0796e3871 default tip

remove "console.log" from general.js
author Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
date Fri, 29 Sep 2017 16:18:02 +0200
parents d0dcbe8254f5
children
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 de.mpiwg.web.search.*;
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.DBSection;
import de.mpiwg.gazetteer.utils.DBService;
import de.mpiwg.gazetteer.utils.DataProvider;

public class SearchPage extends AbstractJSPPage{

	private static Logger logger = Logger.getLogger(SearchPage.class);

	public static String bean = "searchBean";
	public static String page = "pages/search.jsp";


	private static Integer SEARCH_IN_SECTION_NAME = 0;
	private static Integer SEARCH_IN_BOOK_NAME = 1;
	//private static Integer SEARCH_FULL_TEXT = 2;


	private Map<Long, List<LGBranch>> branchesMap;
	private List<DBSection> completeSectionList;
	private List<DBSection> filteredSectionList;
	private List<DBSection> displaySectionList;

	private String searchTerm = new String();
	private Integer searchIn = SEARCH_IN_SECTION_NAME;

	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 periodFilter = new String();
	private String sectionNameFilter = new String();
    private String bookYearFilter = new String();
    private String editionYearFilter = new String();
	private String sourceFilter = new String();

	private DataPaginator paginator = new DataPaginator();
	private String searchMessage;
	private String filteringMessage;


	private Map<Long, List<LGTopicSectionRelation>> topicSectionRelationMap;


	@Override
	public void init(){
		super.init();
	}

	public void loadParameters(HttpServletRequest request, HttpServletResponse response){

		this.request = request;
		this.response = response;

		this.searchTerm = getParameter("searchTerm");
		this.dynastyFilter = getParameter("dynastyFilter");
		this.adminTypeFilter = getParameter("adminTypeFilter");
		this.level1Filter = getParameter("level1Filter");
		this.level2Filter = getParameter("level2Filter");
		this.bookNameFilter = getParameter("bookNameFilter");
		this.periodFilter = getParameter("periodFilter");
		this.sectionNameFilter = getParameter("sectionNameFilter");
        this.bookYearFilter = getParameter("bookYearFilter");
		this.editionYearFilter = getParameter("editionYearFilter");
		this.sourceFilter = getParameter("sourceFilter");

		this.searchIn = getIntParameter("searchIn");

	}

	public void updateTopicSectionRelation() {
		logger.debug("updateTopicSectionRelation");
		this.loadTopicSectionRelation();
		this.filter();

	}

	public void search(){
		logger.debug("Searching: " + this.searchTerm);

		this.dynastyFilter = new String();
		this.level1Filter = new String();
		this.adminTypeFilter = new String();

		if(StringUtils.isNotEmpty(this.searchTerm)){
			this.loadBranches();

			this.loadTopicSectionRelation();

			// TODO load all books for the difference set??


			try {
				List<String> terms = splitTerms();

				if(SEARCH_IN_SECTION_NAME.equals(this.searchIn)){
					System.out.println("Section Search in Section Name");
					this.completeSectionList = DBService.searchSection(terms);

				}else if(SEARCH_IN_BOOK_NAME.equals(this.searchIn)){
					System.out.println("Section Search in Book Name");
					this.completeSectionList = DBService.searchBook(terms, "name");

				} /*else if (SEARCH_FULL_TEXT.equals(this.searchIn)) {
					System.out.println("Full Text Search");
					DBService.searchFullText(terms);
				}
				*/

				Collections.sort(this.completeSectionList);
				filter();

			} catch (Exception e) {
				internalError(e);
			}
		}
	}

	public void filter(){
		this.filteredSectionList = new ArrayList<DBSection>();
		if (this.completeSectionList != null) {

			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(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(bookYearFilter) || section.getBook().getBookYear() == Integer.parseInt(bookYearFilter)) &&
                            (StringUtils.isEmpty(editionYearFilter) || section.getBook().getEditionYear() == Integer.parseInt(editionYearFilter)) &&
                            (StringUtils.isEmpty(sourceFilter) || StringUtils.contains(section.getBook().getSource(), sourceFilter)) &&
							(StringUtils.isEmpty(adminTypeFilter) || StringUtils.startsWith(section.getBook().getAdmin_type(), adminTypeFilter))
									){
						this.filteredSectionList.add(section);
					}
				}
			}



			if(completeSectionList.size() > 0){
				this.searchMessage = completeSectionList.size() + " section(s) found for the term(s): " + this.searchTerm;
				this.filteringMessage = this.filteredSectionList.size() + " section(s) listed after the filtering";

				this.paginator.setCurrentPage(0);
				this.paginator.resetNumberOfPages(filteredSectionList.size());

			}else{
				this.searchMessage = "No sections found for the term(s): " + this.searchTerm;
				this.filteredSectionList = null;
				this.filteringMessage = "";

				this.paginator.setCurrentPage(0);
				this.paginator.resetNumberOfPages(0);
			}

			this.updateCurrentSections();
		}
	}

	private void updateCurrentSections() {
		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()));
			section.setTopicSectionRelation(this.topicSectionRelationMap.get(section.getId()));
		}
	}

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


	private List<String> splitTerms(){
		List<String> rs = new ArrayList<String>();
		String[] array = this.searchTerm.split(",");

		for(String tmp : array){
			tmp = tmp.replace(" ", "");
			if(StringUtils.isNotEmpty(tmp)){
				rs.add(tmp);
			}
		}
		return rs;
	}

	public List<String> suggestDynasty(String term, int limit){
		List<String> list = new ArrayList<String>();
		for(DBSection section : this.completeSectionList){
			String dynasty = section.getBook().getDynasty();
			if(!list.contains(dynasty) && dynasty.startsWith(term)){
				list.add(dynasty);
			}
			if(limit == list.size()){
				break;
			}
		}
		return list;
	}

	public List<String> suggestLevel1(String term, int limit){
		List<String> list = new ArrayList<String>();
		for(DBSection section : this.completeSectionList){
			String level1 = section.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(DBSection section : this.completeSectionList){
			String level2 = section.getBook().getLevel2();
			if(!list.contains(level2) && level2.startsWith(term)){
				list.add(level2);
			}
			if(limit == list.size()){
				break;
			}
		}
		return list;
	}


	public List<String> suggestBookName(String term, int limit){
		List<String> list = new ArrayList<String>();
		for(DBSection section : this.completeSectionList){
			String bookName = section.getBook().getName();
			if(!list.contains(bookName) && bookName.startsWith(term)){
				list.add(bookName);
			}
			if(limit == list.size()){
				break;
			}
		}
		return list;
	}

	public List<String> suggestPeriod(String term, int limit){
		List<String> list = new ArrayList<String>();
		for(DBSection section : this.completeSectionList){
			String period = section.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(DBSection section : this.completeSectionList){
			String adminType = section.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(DBSection section : this.completeSectionList){
			String sectionName = section.getName();
			if(!list.contains(sectionName) && sectionName.startsWith(term)){
				list.add(sectionName);
			}
			if(limit == list.size()){
				break;
			}
		}
		return list;
	}


	public static Integer getSEARCH_IN_SECTION_NAME() {
		return SEARCH_IN_SECTION_NAME;
	}


	public static void setSEARCH_IN_SECTION_NAME(Integer sEARCH_IN_SECTION_NAME) {
		SEARCH_IN_SECTION_NAME = sEARCH_IN_SECTION_NAME;
	}


	public Map<Long, List<LGTopicSectionRelation>> getTopicSectionRelationMap() {
		return topicSectionRelationMap;
	}

	public void setTopicSectionRelationMap(
			Map<Long, List<LGTopicSectionRelation>> topicSectionRelationMap) {
		this.topicSectionRelationMap = topicSectionRelationMap;
	}

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


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


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


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


	public String getSearchTerm() {
		return searchTerm;
	}


	public void setSearchTerm(String searchTerm) {
		this.searchTerm = searchTerm;
	}


	public Integer getSearchIn() {
		return searchIn;
	}


	public void setSearchIn(Integer searchIn) {
		this.searchIn = searchIn;
	}

	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 void firstPage() {
		this.paginator.first();
		this.updateCurrentSections();
	}

	public void lastPage() {
		this.paginator.last();
		this.updateCurrentSections();
	}

	public void fastForward() {
		this.paginator.fastForward();
		this.updateCurrentSections();
	}

	public void fastRewind() {
		this.paginator.fastRewind();
		this.updateCurrentSections();
	}

	public void previousPage() {
		this.paginator.previous();
		this.updateCurrentSections();
	}

	public void nextPage() {
		this.paginator.next();
		this.updateCurrentSections();
	}

	public String getSearchMessage() {
		return searchMessage;
	}

	public void setSearchMessage(String searchMessage) {
		this.searchMessage = searchMessage;
	}

	public String getFilteringMessage() {
		return filteringMessage;
	}

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

	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 getDynastyFilter() {
		return dynastyFilter;
	}

	public void setDynastyFilter(String dynastyFilter) {
		this.dynastyFilter = dynastyFilter;
	}

	public String getLevel2Filter() {
		return level2Filter;
	}

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

	public String getBookNameFilter() {
		return bookNameFilter;
	}

	public void setBookNameFilter(String bookNameFilter) {
		this.bookNameFilter = bookNameFilter;
	}

	public String getSectionNameFilter() {
		return sectionNameFilter;
	}

	public void setSectionNameFilter(String sectionNameFilter) {
		this.sectionNameFilter = sectionNameFilter;
	}

	public String getPeriodFilter() {
		return periodFilter;
	}

	public void setPeriodFilter(String periodFilter) {
		this.periodFilter = periodFilter;
	}

    public String getBookYearFilter() {
        return bookYearFilter;
    }


    public void setBookYearFilter(String bookYearFilter) {
        this.bookYearFilter = bookYearFilter;
    }

    public String getEditionYearFilter() {
        return editionYearFilter;
    }


    public void setEditionYearFilter(String editionYearFilter) {
        this.editionYearFilter = editionYearFilter;
    }

	public String getSourceFilter() {
		return sourceFilter;
	}


	public void setSourceFilter(String sourceFilter) {
		this.sourceFilter = sourceFilter;
	}


	/////// 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 sortByAuthorUp(){
		Collections.sort(this.completeSectionList, new SortSectionByAuthor());
		filter();
	}

	public void sortByAuthorDown(){
		Collections.sort(this.completeSectionList, new SortSectionByAuthor());
		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 sortByVolumeUp(){
		Collections.sort(this.completeSectionList, new SortSectionByVolume());
		filter();
	}

	public void sortByVolumeDown(){
		Collections.sort(this.completeSectionList, new SortSectionByVolume());
		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 sortByEditionUp(){
		Collections.sort(this.completeSectionList, new SortSectionByEdition());
		filter();
	}

	public void sortByEditionDown(){
		Collections.sort(this.completeSectionList, new SortSectionByEdition());
		Collections.reverse(completeSectionList);
		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 sortByBookYearUp(){
        Collections.sort(this.completeSectionList, new SortSectionByBookYear());
        filter();
    }

    public void sortByBookYearDown(){
        Collections.sort(this.completeSectionList, new SortSectionByBookYear());
        Collections.reverse(completeSectionList);
        filter();
    }

    public void sortByEditionYearUp(){
        Collections.sort(this.completeSectionList, new SortSectionByEditionYear());
        filter();
    }

    public void sortByEditionYearDown(){
        Collections.sort(this.completeSectionList, new SortSectionByEditionYear());
        Collections.reverse(completeSectionList);
        filter();
    }

	public void sortBySourceUp(){
		Collections.sort(this.completeSectionList, new SortSectionBySource());
		filter();
	}

	public void sortBySourceDown(){
		Collections.sort(this.completeSectionList, new SortSectionBySource());
		Collections.reverse(completeSectionList);
		filter();
	}




}