/*
 * $Id: SearchRelatedDocumentsForm.java,v 1.5 2006/12/18 15:50:23 lawern Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: SearchRelatedDocumentsForm.java,v $
 * Revision 1.5  2006/12/18 15:50:23  lawern
 * Korowas Änderungen überarbeitet.
 *
 * Revision 1.3  2006/09/29 21:10:06  marbobo
 * + Sucheoptimiert
 *
 * Revision 1.2  2005/08/14 18:13:59  marbobo
 * + Alternative-Sicht implementiert
 *
 * Revision 1.1  2004/04/28 11:18:46  marbobo
 * Initiale Version
 *
 */
package vkc.search.form;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.lucene.search.Hits;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

import vkc.db.entities.VKCDocument;
import vkc.db.serializer.VKCDocumentSerializer;
import vkc.exception.DBException;
import vkc.search.Search;
import vkc.view.form.DynaForm;


/**
 * Form Bean für die Darstellung von Suchergebnissen
 *
 * @author marbobo
 * @version $Revision: 1.5 $
 */
public final class SearchRelatedDocumentsForm extends DynaForm {
	public final static int DOCUMENT_SORT_PUBLISHING_DATE_UP   	= 1;
	public final static int DOCUMENT_SORT_PUBLISHING_DATE_DOWN 	= 2;

	public final static int DOCUMENT_SORT_TITLE_UP  			= 3;
	public final static int DOCUMENT_SORT_TITLE_DOWN			= 4;

	public final static int DOCUMENT_SORT_AUTHOR_UP 			= 5;
	public final static int DOCUMENT_SORT_AUTHOR_DOWN			= 6;
	
	public final static int DOCUMENT_SORT_SIZE_UP  				= 7;
	public final static int DOCUMENT_SORT_SIZE_DOWN 			= 8;

	public final static int DOCUMENT_SORT_TYPE_UP  				= 9;
	public final static int DOCUMENT_SORT_TYPE_DOWN 			= 10;

	public final static int DOCUMENT_SORT_PUBLISHER_UP 			= 13;
	public final static int DOCUMENT_SORT_PUBLISHER_DOWN		= 14;

	public final static int DOCUMENT_SORT_CREATION_DATE_UP   	= 15;
	public final static int DOCUMENT_SORT_CREATION_DATE_DOWN 	= 16;

	public final static int DOCUMENT_SORT_SCORE_UP   			= 17;
	public final static int DOCUMENT_SORT_SCORE_DOWN 			= 18;

	private Integer currentResultPageId	= new Integer(1);
	private Integer documentSortMode = new Integer(DOCUMENT_SORT_SCORE_DOWN);
	private Integer hitsCount = new Integer(0);
	private Integer pageCount = new Integer(1);
	private Integer startIndex = new Integer(0);
	private Integer	categoryId = null;
	private Integer	documentId = null;
	private Integer	selectedId = null;
	
	private Integer[] showDocumentDetails = new Integer[0];
	private Integer[] showDocuments = new Integer[0];

    private List results = new ArrayList(); 

    private String selectedNumberOfResultsPerPage = "10";
	//~ Methods ----------------------------------------------------------------

	/**
	 * Reset der Eingabefelder auf Defaultwerte
	 *
	 * @param mapping The mapping used to select this instance
	 * @param request The servlet request we are processing
	 */
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		super.reset(mapping, request);

		currentResultPageId	= new Integer(1);
		documentSortMode = new Integer(DOCUMENT_SORT_SCORE_DOWN);
		hitsCount = new Integer(0);
		pageCount = new Integer(1);
		startIndex = new Integer(0);
		categoryId = null;
		documentId = null;
		selectedId = null;
		
		showDocumentDetails = new Integer[0];
		showDocuments = new Integer[0];

	    results = new ArrayList(); 

	    selectedNumberOfResultsPerPage = "10";
	}

	/**
	 * Validiert die Eingabefelder und gibt gegebenfalls die Fehler zurück
	 *
	 * @param mapping The mapping used to select this instance
	 * @param request The servlet request we are processing
	 */
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		
		return errors;
	}

	public Integer[] getShowDocuments() {
		return showDocuments;
	}

	public void setShowDocuments(Integer[] list) {
		showDocuments = list;
	}

	public boolean getShowDocs(int i) {
		List ids = Arrays.asList(getShowDocuments());

		return ids.contains(new Integer(i));
	}

	public Integer[] getShowDocumentDetails() {
		return showDocumentDetails;
	}

	public void setShowDocumentDetails(Integer[] list) {
		showDocumentDetails = list;
	}

	public boolean getShowDocDetails(int i) {
		List ids = Arrays.asList(getShowDocumentDetails());
		return ids.contains(new Integer(i));
	}

	public Integer getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(Integer integer) {
		selectedId = integer;
	}

	public Integer getCategoryId() {
		return categoryId;
	}

	public Integer getDocumentId() {
		return documentId;
	}

	public List getRelatedDocuments() throws DBException {
		List relatedDocuments = new ArrayList();
		VKCDocument doc = VKCDocumentSerializer.retrieveDocument(getDocumentId());

		Collection relatedDocs = Search.whatsRelated(doc, null, null, "*", getUser(), null);

		if (relatedDocs != null) {
			relatedDocuments.addAll(relatedDocs);
		}
		
		return relatedDocuments;
	}

	public void setCategoryId(Integer integer) {
		categoryId = integer;
	}

	public void setDocumentId(Integer integer) {
		documentId = integer;
	}

	public Integer getCurrentResultPageId() {
		return currentResultPageId;
	}

	public void setCurrentResultPageId(Integer currentResultPageId) {
		this.currentResultPageId = currentResultPageId;
	}

	public Integer getDocumentSortMode() {
		return documentSortMode;
	}

	public void setDocumentSortMode(Integer documentSortMode) {
		this.documentSortMode = documentSortMode;
	}

	public Integer getHitsCount() {
		return hitsCount;
	}

	public void setHitsCount(Integer hitsCount) {
		this.hitsCount = hitsCount;
	}

	public Integer getPageCount() {
		return pageCount;
	}

	public void setPageCount(Integer pageCount) {
		this.pageCount = pageCount;
	}

	public Integer getStartIndex() {
		return startIndex;
	}

	public void setStartIndex(Integer startIndex) {
		this.startIndex = startIndex;
	}

	public String getSelectedNumberOfResultsPerPage() {
		return selectedNumberOfResultsPerPage;
	}

	public void setSelectedNumberOfResultsPerPage(
			String selectedNumberOfResultsPerPage) {
		this.selectedNumberOfResultsPerPage = selectedNumberOfResultsPerPage;
	}

	public List getResults() {
		return results;
	}

	public void setResults(List results) {
		this.results = results;
	}

	public Integer[] getPagesIndex() {
		Integer resultsPerPage = new Integer(getSelectedNumberOfResultsPerPage());
		Integer pagesCount = new Integer(((getHitsCount().intValue() % resultsPerPage.intValue()) == 0)
										? (getHitsCount().intValue() / resultsPerPage.intValue())
										: ((getHitsCount().intValue() / resultsPerPage.intValue()) + 1));
		Integer[] pagesIndex = new Integer[pagesCount.intValue()];

		for (int i = 0; i < pagesIndex.length; i++) {
			pagesIndex[i] = new Integer(i + 1);
		}
		
		return pagesIndex;
	}
	
	public Integer getNextPageId() {
		return new Integer(getCurrentResultPageId().intValue() + 1);
	}

	public Integer getPreviousPageId() {
		return new Integer(getCurrentResultPageId().intValue() - 1);
	}

	public Hits getHits() throws DBException {
		Hits hits = null;
		Sort sort = null;
		int mode = getDocumentSortMode().intValue();

		switch (mode) {
			case DOCUMENT_SORT_SCORE_UP: 
				sort = new Sort(new SortField(null, SortField.SCORE, true));
				break;
			case DOCUMENT_SORT_SCORE_DOWN: 
				sort = new Sort(new SortField(null, SortField.SCORE));
				break;
			case DOCUMENT_SORT_PUBLISHING_DATE_UP: 
				sort = new Sort(new SortField("published", SortField.STRING));
				break;
			case DOCUMENT_SORT_PUBLISHING_DATE_DOWN: 
				sort = new Sort(new SortField("published", SortField.STRING, true));
				break;
			case DOCUMENT_SORT_CREATION_DATE_UP: 
				sort = new Sort(new SortField("creationDate", SortField.STRING));
				break;
			case DOCUMENT_SORT_CREATION_DATE_DOWN: 
				sort = new Sort(new SortField("creationDate", SortField.STRING, true));
				break;
			case DOCUMENT_SORT_TITLE_UP: 
				sort = new Sort(new SortField("title_sort", getUser().getLanguage().getLocale()));
				break;
			case DOCUMENT_SORT_TITLE_DOWN: 
				sort = new Sort(new SortField("title_sort", getUser().getLanguage().getLocale(), true));
				break;
			case DOCUMENT_SORT_AUTHOR_UP: 
				sort = new Sort(new SortField("authorname_sort", getUser().getLanguage().getLocale()));
				break;
			case DOCUMENT_SORT_AUTHOR_DOWN: 
				sort = new Sort(new SortField("authorname_sort", getUser().getLanguage().getLocale(), true));
				break;
			case DOCUMENT_SORT_PUBLISHER_UP: 
				sort = new Sort(new SortField("publishername_sort", getUser().getLanguage().getLocale()));
				break;
			case DOCUMENT_SORT_PUBLISHER_DOWN: 
				sort = new Sort(new SortField("publishername_sort", getUser().getLanguage().getLocale(), true));
				break;
			case DOCUMENT_SORT_SIZE_UP: 
				sort = new Sort(new SortField("docSize", SortField.STRING));
				break;
			case DOCUMENT_SORT_SIZE_DOWN: 
				sort = new Sort(new SortField("docSize", SortField.STRING, true));
				break;
			case DOCUMENT_SORT_TYPE_UP: 
				sort = new Sort(new SortField("docTypeName", getUser().getLanguage().getLocale()));
				break;
			case DOCUMENT_SORT_TYPE_DOWN: 
				sort = new Sort(new SortField("docTypeName", getUser().getLanguage().getLocale(), true));
				break;
		}
		
		VKCDocument doc = VKCDocumentSerializer.retrieveDocument(getDocumentId());

		hits = Search.whatsRelated(doc, null, null, "*", getUser(), null, null, null, sort);

		return hits;
	}
}