/*
 * $Id: TrashViewForm.java,v 1.5 2005/09/01 20:02:41 jonyp Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: TrashViewForm.java,v $
 * Revision 1.5  2005/09/01 20:02:41  jonyp
 * *** empty log message ***
 *
 * Revision 1.4  2005/08/26 14:52:04  jonyp
 * working things
 *
 * Revision 1.3  2005/08/19 15:09:52  jonyp
 * added comments
 *
 * Revision 1.2  2005/08/09 22:43:27  marbobo
 * - Unnötige Import-Anweisungen entfernt
 *
 * Revision 1.1  2005/06/14 14:51:13  jonyp
 * *** empty log message ***
 *
 * 
 */
 
package vkc.administration.form;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionMapping;

import vkc.db.entities.VKCCategory;
import vkc.db.entities.VKCEntity;
import vkc.db.serializer.VKCCategorySerializer;
import vkc.exception.DBException;
import vkc.util.StringUtils;
import vkc.view.WebShare;
import vkc.view.bean.CategorySettings;
import vkc.view.form.BasicActionForm;

public class TrashViewForm extends BasicActionForm {

	protected CategorySettings categoryInfo = null;

	protected HttpSession session = null;

	protected Integer categoryId = VKCEntity.ID_UNDEFINED;
	protected Integer documentId = VKCEntity.ID_UNDEFINED;
	protected Integer selectedId = VKCEntity.ID_UNDEFINED;
	protected Integer[] showDocumentDetails = new Integer[ 0 ];
	
	protected List accessibleSubCategories = null;
	protected List documentStati = null;
	protected List documents = null;
	
	protected Integer[] selectedCategoryIds = {};
	protected Integer[] selectedDocumentIds = {};
	
	protected String trashAction = "";

	protected VKCCategory category = null;

	/**
	 * Reset all properties to their default values.
	 *
	 * @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 );

		category = null;
		categoryId = null;
		documentId = null;
		selectedId = null;
		selectedCategoryIds = new Integer[0];
		selectedDocumentIds = new Integer[0];
		accessibleSubCategories = null;
		documentStati = null;
		categoryInfo = (CategorySettings) request.getAttribute( "categorySettings" );
		showDocumentDetails = new Integer[ 0 ];
		session = request.getSession();
		trashAction = "";
	}	

	public CategorySettings getCategorySettings() {
		if ( categoryInfo == null ) {
			categoryInfo = WebShare.getCategorySettings();
		}
		
		return categoryInfo;
	}

	public VKCCategory getCategory() throws DBException {
		if ( category == null ) {
			category = VKCCategorySerializer.retrieveCategory( getCategoryId() );
		}

		return category;
	}

	public void reloadCategory() throws DBException {
		category = VKCCategorySerializer.retrieveCategory(getCategoryId());
	}
	
	public Integer getCategoryId() {
		return categoryId;
	}

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

	public Integer[] getSelectedCategoryIds() {
		return selectedCategoryIds;
	}

	public Integer[] getSelectedDocumentIds() {
		return selectedDocumentIds;
	}

	public void setSelectedCategoryIds( Integer[] cats ) {
		selectedCategoryIds = cats;
	}

	public void setSelectedDocumentIds( Integer[] docs ) {
		selectedDocumentIds = docs;
	}

    public List getAccessibleSubcategories() throws DBException {

        if ( accessibleSubCategories == null ) {
	        if ( getUser() != null && getCategory() != null ) {
	            accessibleSubCategories = getCategory().getAccessibleSubCategories( getUser() );
	        } else {
	            accessibleSubCategories = new ArrayList();
	        }
        }
        
        return accessibleSubCategories;
    }
	
	
	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 i ) {
		selectedId = i;
	}

	public Integer getCategorySortMode() {
		return (Integer)session.getAttribute("categoryViewCategorySortMode");
	}

	public void setCategorySortMode( Integer i ) {
	    session.setAttribute("categoryViewCategorySortMode", i);
	}
	
	public Integer getDocumentSortMode() {
	    return (Integer)session.getAttribute("categoryViewDocumentSortMode");
	}

	public void setDocumentSortMode( Integer i ) {
	    session.setAttribute("categoryViewDocumentSortMode", i);
	}
	
	public boolean getHasDocuments() throws DBException {
		return !getCategory().getDocuments().isEmpty();
	}
	
	public List getDocuments() throws DBException {
		List documents = getCategory().getDocuments();
		
		return documents;
	}
	
	public String getTrashAction() {
		return trashAction;
	}

	public void setTrashAction(String s) {
		trashAction = StringUtils.normalize(s);
	}

	public boolean isEmpty() throws DBException{
	    
	    if (getAccessibleSubcategories().isEmpty() && getDocuments().isEmpty()) {
	        return true;
	    }
	    
	    return false;
	}
}