/*
 * $Id: ShowHistoryAction.java,v 1.5 2005/08/09 22:30:26 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ShowHistoryAction.java,v $
 * Revision 1.5  2005/08/09 22:30:26  marbobo
 * + Alternative-Sicht implementiert
 *
 * Revision 1.4  2005/02/14 14:01:50  bigmac
 * Applikationsweites CSS konfigurierbar gemacht + Refactorings
 *
 * Revision 1.3  2004/11/10 15:02:49  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.2  2004/11/02 21:42:29  marbobo
 * + Einige Funktionen für Gast-Benutzer gesperrt
 * + WebShare.LOGIN_PAGE mit WebShare.getLoginPage() ersetzt
 *
 * Revision 1.1  2004/05/11 09:28:18  jonyp
 * first strike
 *
 * Revision 1.1  2004/05/05 13:07:47  jonyp
 * first strike
 *
 */
package vkc.profile.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.VKCUserDocHistorySerializer;
import vkc.view.WebShare;
import vkc.misc.VKCLogManager;
import vkc.profile.form.ShowHistoryForm;

/**
 * Action für die Ausgabe der DokumentHistorie
 *
 * @author jonyP
 */
public final class ShowHistoryAction extends Action {
    private ActionForward DEFAULT_FORWARD = new ActionForward("/templates/${template.name}/userProfile/showHistory.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 {
			ShowHistoryForm form = (ShowHistoryForm)f;

			if (form.getUser().isGuestUser()) {
				request.setAttribute(WebShare.INFO_KEY, "vkc.info.access_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 if (request.getParameter("action.show.documents") != null) {
				return handleShowDocumentsAction(form, request);
			} else if (request.getParameter("action.hide.documents") != null) {
				return handleHideDocumentsAction(form, request);
			} else if (request.getParameter("action.delete.history") != null) {
				return handleDeleteAction(form, request);
			} else {
				return handleInitializeAction(form, request);
			}
		} catch (Exception ex) {
		    VKCLogManager.errorLog(this,"", ex.getMessage(),ex);
			throw new ServletException(ex);
		}
    }

	private ActionForward handleHideDocumentDetailsAction(ShowHistoryForm 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}/userProfile/showHistory.jsp?anchor=doc" + form.getSelectedId(), false);
	}

	private ActionForward handleShowDocumentDetailsAction(ShowHistoryForm 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}/userProfile/showHistory.jsp?anchor=doc" + form.getSelectedId(), false);
	}

	private ActionForward handleHideDocumentsAction(ShowHistoryForm form, HttpServletRequest request) {
		List ids = new ArrayList(Arrays.asList(form.getShowDocuments()));
		ids.remove(form.getSelectedId());
		form.setShowDocuments((Integer[])ids.toArray(new Integer[0]));
		
		return new ActionForward("/templates/${template.name}/userProfile/showHistory.jsp?anchor=doc" + form.getSelectedId(), false);
	}

	private ActionForward handleShowDocumentsAction(ShowHistoryForm form, HttpServletRequest request) {
		List ids = new ArrayList(Arrays.asList(form.getShowDocuments()));
		ids.add(form.getSelectedId());
		form.setShowDocuments((Integer[])ids.toArray(new Integer[0]));

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

	private ActionForward handleInitializeAction(ShowHistoryForm form, HttpServletRequest request) throws Exception {
		return DEFAULT_FORWARD;
	}
	
	private ActionForward handleDeleteAction(ShowHistoryForm form, HttpServletRequest request) throws Exception {
		VKCUserDocHistorySerializer.deleteDocumentHistory(form.getUser().getUserId(), null);

		return DEFAULT_FORWARD;
	}
}