	/*
 * $Id: ClusterDocumentViewAction.java,v 1.3 2006/10/04 03:14:33 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ClusterDocumentViewAction.java,v $
 * Revision 1.3  2006/10/04 03:14:33  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.2  2005/06/29 21:15:50  marbobo
 * + Clustern ist nun aktivierbar/deaktivierbar
 *
 * Revision 1.1  2005/06/23 00:58:28  aram
 * + Initiale Version
 *
 */
package vkc.view.action;

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import vkc.db.serializer.VKCSysParameterSerializer;
import vkc.view.WebShare;
import vkc.view.form.ClusterDocumentViewForm;

public final class ClusterDocumentViewAction extends Action {
    private ActionForward DEFAULT_FORWARD = new ActionForward( "/templates/${template.name}/admin/ClusterDocumentView.jsp" );
    private ActionForward INFO_FORWARD = new ActionForward("/templates/${template.name}/common/InfoPage.jsp");

    public ActionForward execute( ActionMapping mapping, 
    							  ActionForm f, 
    							  HttpServletRequest request,
							      HttpServletResponse response )
    throws ServletException {
        try {
            ClusterDocumentViewForm form = (ClusterDocumentViewForm) f;

    		if (VKCSysParameterSerializer.retrieveIntValue("CATEGORYVIEW", "EnableClusterCategory", 0) == 0) {
    			request.setAttribute(WebShare.INFO_KEY, "vkc.info.action_not_allowed");
                
    			return INFO_FORWARD;
    		}

            if (form.getUser().isGuestUser()) {
                request.setAttribute( WebShare.INFO_KEY, "category.create.denied" );
                
                return INFO_FORWARD;
            }
            
            if ( request.getParameter( "action.show.document.details" ) != null ) {
				return handleShowDocumentDetailsAction( form, request );
			} else if ( request.getParameter( "action.hide.document.details" ) != null ) {
				return handleHideDocumentDetailsAction( form, request );
			} else {
	            return handleInitializeAction( form, request );
			}
        } catch ( Exception e ) {
            throw new ServletException( e );
        }
    }

	private ActionForward handleHideDocumentDetailsAction( ClusterDocumentViewForm form, HttpServletRequest request ) {
		List ids = new ArrayList( Arrays.asList( form.getShowDocumentDetails() ) );
		ids.remove( form.getSelectedId() );
		form.setShowDocumentDetails( (Integer[])ids.toArray( new Integer[ 0 ] ) );
		
		return new ActionForward( "/templates/${template.name}/admin/ClusterDocumentView.jsp?anchor=doc" + form.getSelectedId(), false );
	}

    private ActionForward handleShowDocumentDetailsAction( ClusterDocumentViewForm form, HttpServletRequest request ) {
		List ids = new ArrayList( Arrays.asList( form.getShowDocumentDetails() ) );
		ids.add( form.getSelectedId() );
		form.setShowDocumentDetails( (Integer[])ids.toArray( new Integer[ 0 ] ) );

		return new ActionForward( "/templates/${template.name}/admin/ClusterDocumentView.jsp?anchor=doc" + form.getSelectedId(), false );
	}

	private ActionForward handleInitializeAction( ClusterDocumentViewForm form, HttpServletRequest request ) {
	    return DEFAULT_FORWARD;
	}
}