view src/main/java/de/mpiwg/web/SearchBean.java @ 0:7682c04c63a8

First commit of the source code!
author "jurzua <jurzua@mpiwg-berlin.mpg.de>"
date Tue, 10 Mar 2015 14:50:41 +0100
parents
children 5316e79f9a27
line wrap: on
line source

package de.mpiwg.web;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.icefaces.ace.event.TextChangeEvent;

import com.icesoft.faces.component.jseventlistener.JSEventListener;

import de.mpiwg.gazetteer.bo.LGBranch;
import de.mpiwg.gazetteer.db.DBSection;
import de.mpiwg.gazetteer.utils.DBService;
import de.mpiwg.gazetteer.utils.DataProvider;

public class SearchBean  extends AbstractBean{

	private static Logger logger = Logger.getLogger(SearchBean.class);
	
	private String term;
	private List<SelectItem> sectionSuggestion;
	private List<DBSection> sectionList;
	private String message;
	private Map<Long, List<LGBranch>> branchesMap;
	
	public void changeSectionName(TextChangeEvent event){
		logger.debug("changeSectionName");
		logger.debug("key" + event.getKeyCode());
		
		String term = event.getNewValue().toString();
		
		this.sectionSuggestion = new ArrayList<SelectItem>();
		
		if(!term.contains(",")){
			
			try {
				List<String> list = DBService.suggestSectionName(term);
				for(String s : list){
					this.sectionSuggestion.add(new SelectItem(s));
				}
			} catch (SQLException e) {
				internalError(e);
			}	
		}
	}
	
	public void changeSectionSearch(ValueChangeEvent event){
		this.term = (String)event.getNewValue();
		this.search();
	}
	
	public void listenerSearch(ActionEvent event){
		this.search();
	}
	
	private void search(){
		this.message = null;
		if(StringUtils.isNotEmpty(this.term)){
			this.loadBranches();
			try {
				List<String> terms = splitTerms();
				this.sectionList = DBService.searchSection(terms);
				
				for(DBSection section : this.sectionList){
					section.setBranches(this.branchesMap.get(section.getId()));
				}
				
				if(sectionList.size() > 0){
					this.message = sectionList.size() + " item(s) found for the term(s): " + this.term;
				}else{
					this.message = "No items found for the term(s): " + this.term;
				}
				
			} catch (Exception e) {
				internalError(e);
			}			
		}

	}
	
	private void loadBranches(){
		this.branchesMap = new HashMap<Long, List<LGBranch>>();
		List<LGBranch> list = DataProvider.getInstance().getBranches(getSessionBean().getUser().getId());
		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 List<String> splitTerms(){
		List<String> rs = new ArrayList<String>();
		String[] array = this.term.split(",");
		
		for(String tmp : array){
			tmp = tmp.replace(" ", "");
			if(StringUtils.isNotEmpty(tmp)){
				rs.add(tmp);	
			}
		}
		return rs;
	}

	public String getTerm() {
		return term;
	}

	public void setTerm(String term) {
		this.term = term;
	}

	public List<SelectItem> getSectionSuggestion() {
		return sectionSuggestion;
	}

	public void setSectionSuggestion(List<SelectItem> sectionSuggestion) {
		this.sectionSuggestion = sectionSuggestion;
	}

	public List<DBSection> getSectionList() {
		return sectionList;
	}

	public void setSectionList(List<DBSection> sectionList) {
		this.sectionList = sectionList;
	}

	public String getMessage() {
		return message;
	}
}