/*
 * $Id: ChangeDocumentPublisherAction.java,v 1.5 2006/10/04 03:15:01 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ChangeDocumentPublisherAction.java,v $
 * Revision 1.5  2006/10/04 03:15:01  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.4  2006/08/01 20:58:52  marbobo
 * + Navigiertes Dokument-Hervorheben für alternative Kategoriesicht, Projektsicht und Funktionssicht implementiert
 *
 * Revision 1.3  2005/09/13 00:08:02  marbobo
 * + Aktuallisierung des Suchindex bei Änderung des Ersteller/Herausgebers implementiert
 *
 * Revision 1.2  2005/08/17 12:58:07  jonyp
 * *** empty log message ***
 *
 * Revision 1.1  2005/08/09 19:00:05  jonyp
 * support for change of publisher
 *
 * Revision 1.2  2005/07/06 13:57:31  jonyp
 * changes due to new serializer function "changePublisherId"
 *
 * Revision 1.1  2005/07/06 13:33:17  jonyp
 * first strike: does not work !
 *
 */

package vkc.administration.action;

import java.util.Date;

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

import org.apache.struts.Globals;
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.administration.form.ChangeDocumentPublisherForm;
import vkc.db.entities.VKCCategory;
import vkc.db.entities.VKCDocument;
import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCDocumentSerializer;
import vkc.exception.DBException;
import vkc.procoba.project.ProjectViewForm;
import vkc.view.PageId;
import vkc.view.WebShare;

public final class ChangeDocumentPublisherAction extends Action {
	private ActionForward DEFAULT_FORWARD = new ActionForward("/templates/${template.name}/administration/changeDocumentPublisher.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 Exception {
		ChangeDocumentPublisherForm form = (ChangeDocumentPublisherForm)f;
    	VKCUser user = form.getUser();		

		if (!user.hasAdminPrivileges()) { 
            request.setAttribute(WebShare.INFO_KEY, "vkc.info.access_denied");
            
            return INFO_FORWARD;
        }

		if (request.getParameter("action.change.publisher") != null) {
			return handleChangeDocumentPublisherAction(form, request);
		}

		if (request.getParameter("action.back") != null) {
			return handleBackAction(form, request);
		}

		// No Action
		return DEFAULT_FORWARD;
	}

	private ActionForward handleChangeDocumentPublisherAction(ChangeDocumentPublisherForm form, HttpServletRequest request) throws Exception {
	    VKCDocument document = form.getDocument();
	    
	    document.setPublisherId(form.getSelectedNewPublisherId());
	    document.setLastChangerId(form.getUser().getUserId());
	    document.setLastChanged(new Date(System.currentTimeMillis()));
	    VKCDocumentSerializer.saveDocument(document, true, false);
	    
        request.setAttribute(Globals.MESSAGE_KEY, "vkc.administration.changepublisher.succeeded");

        return DEFAULT_FORWARD;
	}
	
	private ActionForward handleBackAction(ChangeDocumentPublisherForm form, HttpServletRequest request) throws DBException {
    	VKCCategory category = form.getDocumentCategory();
    	String forwardURL = "";
    	
    	if (form.getSourcePageId().equals(PageId.PROJECT_VIEW)) {
			forwardURL = "/projectView.do?viewMode=" + ProjectViewForm.VIEW_MODE_FOLDER + "&projectId=" + form.getTargetId() + "&subfolderId=" + form.getSubTargetId() + "&anchor=doc" + form.getDocumentId();
			
			return new ActionForward(forwardURL, true);
    	} else if (form.getSourcePageId().equals(PageId.FUNCTIONAL_VIEW)) {
			forwardURL = "/functionalView.do?functionalViewId=" + form.getTargetId() + "&subfolderId=" + form.getSubTargetId() + "&anchor=doc" + form.getDocumentId();

			return new ActionForward(forwardURL, true);
		} else if (form.getSourcePageId().equals(PageId.CATEGORY_VIEW)) {
			if (form.getTargetId() != null) {
				forwardURL = "/showCategory.do?categoryId=" + form.getTargetId() + "&anchor=doc" + form.getDocumentId();
			} else {
				forwardURL = "/showCategory.do?categoryId=" + category.getCategoryId() + "&anchor=doc" + form.getDocumentId();
			}

			return new ActionForward(forwardURL, true);
		} else {
			return new ActionForward("/showCategory.do?categoryId=" + VKCCategory.ROOT_CATEGORY_ID, true);
		}
    }
}