/*
 * $Id: CompareDocumentsAction.java,v 1.3 2006/08/30 21:08:14 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: CompareDocumentsAction.java,v $
 * Revision 1.3  2006/08/30 21:08:14  marbobo
 * + Rückkehr zur Projekt- und Funktionssicht korrigiert
 *
 * Revision 1.2  2006/01/26 12:43:49  korowa
 * *** empty log message ***
 *
 * Revision 1.1  2006/01/20 12:43:24  korowa
 * Ermöglicht ein Vergleich von verschiedenen Versionen eines Dokumentes
 *
 *
 */

package vkc.view.action;

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.misc.VKCLogManager;
import vkc.view.form.CompareDocumentsForm;

/**
 * Action zum Vergleichen 2 Versionen eines Dokumentes 
 *
 * 
 */
public final class CompareDocumentsAction extends Action {
	private ActionForward DEFAULT_FORWARD = new ActionForward("/templates/${template.name}/common/CompareDocuments.jsp", false);

    /**
     * Wird aufgerufen, falls die Versionen eines Documentes
     * 
     */
    public ActionForward execute(ActionMapping mapping, 
    							 ActionForm f,
                                 HttpServletRequest request,
                                 HttpServletResponse response) 
    throws ServletException {
        try {
            CompareDocumentsForm form = (CompareDocumentsForm)f;

            if (request.getParameter("action.back") !=null) {
				return handleBackAction(form, request);
			}
            
			return handleInitialzeAction(form, request);
        } catch (Exception e) {
            VKCLogManager.errorLog(this, "", e.getMessage(), e);
            throw new ServletException(e);
        }
    }
    
    private ActionForward handleBackAction(CompareDocumentsForm form, HttpServletRequest request) {
        String forwardURL = "/showDocumentVersions.do?documentId=" + form.getDocumentId();
        forwardURL += "&categoryId=" + form.getCategoryId();
        forwardURL += "&sourcePageId=" + form.getSourcePageId();

        if (form.getTargetId()!= null) {
            forwardURL += "&targetId=" + form.getTargetId();
        }
        
        if (form.getSubTargetId()!= null) {
            forwardURL += "&subTargetId=" + form.getSubTargetId();
        }
        
        if (form.getSelectedDocuments() != null && form.getSelectedDocuments().length > 0) {
        	for (int i = 0; i < form.getSelectedDocuments().length; i++) {
                forwardURL += "&selectedDocuments=" + form.getSelectedDocuments()[i];
			}
        }
        
		return new ActionForward(forwardURL, true);
	}

	private ActionForward handleInitialzeAction(CompareDocumentsForm form, HttpServletRequest request) throws Exception {
		return DEFAULT_FORWARD;
	}
}