/*
 * $Id: ChangeCreatorPublishershipAction.java,v 1.5 2006/10/04 03:14:59 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ChangeCreatorPublishershipAction.java,v $
 * Revision 1.5  2006/10/04 03:14:59  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.4  2005/09/13 17:49:40  marbobo
 * + Bugfix beim Redirect
 *
 * Revision 1.3  2005/09/13 00:08:02  marbobo
 * + Aktuallisierung des Suchindex bei Änderung des Ersteller/Herausgebers implementiert
 *
 * Revision 1.2  2005/08/09 22:43:26  marbobo
 * - Unnötige Import-Anweisungen entfernt
 *
 * Revision 1.1  2005/08/09 19:00:05  jonyp
 * support for change of publisher
 *
 */

package vkc.administration.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

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.ChangeCreatorPublishershipForm;
import vkc.db.entities.VKCDocument;
import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCCategorySerializer;
import vkc.db.serializer.VKCDocumentSerializer;
import vkc.view.WebShare;

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

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

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

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

		// No Action
		return DEFAULT_FORWARD;
	}

	private ActionForward handleChangeCreatorPublishershipAction(ChangeCreatorPublishershipForm form, HttpServletRequest request) throws Exception {
	    List documents = new ArrayList(VKCDocumentSerializer.getDocuments(form.getOldPublisherId()));
	    
	    for (Iterator iter = documents.iterator(); iter.hasNext();) {
			VKCDocument document = (VKCDocument) iter.next();
			
		    document.setPublisherId(form.getSelectedNewPublisherId());
		    document.setLastChangerId(form.getUser().getUserId());
		    document.setLastChanged(new Date(System.currentTimeMillis()));
		    VKCDocumentSerializer.saveDocument(document, true, false);
		}
	    
	    VKCCategorySerializer.changeCreatorId(null, form.getOldPublisherId(), form.getSelectedNewPublisherId(), form.getUser().getUserId());
	    
	    request.setAttribute(Globals.MESSAGE_KEY, "vkc.administration.changecreatorpublishership.succeeded");
	    
	    return DEFAULT_FORWARD;
	}
	
	private ActionForward handleBackAction(ChangeCreatorPublishershipForm form, HttpServletRequest request) {
    	return new ActionForward(form.getRedirectPath(), true);
    }
}