/*
 * $Id: TrashViewAction.java,v 1.9 2005/09/13 00:09:11 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: TrashViewAction.java,v $
 * Revision 1.9  2005/09/13 00:09:11  marbobo
 * + Anpassung wegen geändertem Clipboard
 *
 * Revision 1.8  2005/09/01 21:32:09  jonyp
 * *** empty log message ***
 *
 * Revision 1.7  2005/09/01 20:02:50  jonyp
 * *** empty log message ***
 *
 * Revision 1.6  2005/09/01 19:49:55  jonyp
 * *** empty log message ***
 *
 * Revision 1.5  2005/09/01 16:33:59  jonyp
 * *** empty log message ***
 *
 * Revision 1.4  2005/08/26 14:52:04  jonyp
 * working things
 *
 * Revision 1.3  2005/08/22 09:39:23  jonyp
 * added correct log
 *
 */
package vkc.administration.action;

import java.util.Set;

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

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

import vkc.db.entities.VKCCategory;
import vkc.db.entities.VKCDocument;
import vkc.misc.VKCLogManager;
import vkc.util.ArrayUtils;
import vkc.util.CategoryUtils;
import vkc.view.ClipBoard;
import vkc.view.WebShare;
import vkc.administration.form.TrashViewForm;

public final class TrashViewAction extends Action {
	private ActionForward DEFAULT_FORWARD = new ActionForward("/templates/${template.name}/administration/trashView.jsp", false);

	public ActionForward execute( ActionMapping mapping, 
            					  ActionForm f, 
            					  HttpServletRequest request,
            					  HttpServletResponse response )
    throws ServletException {
        try {
            TrashViewForm form = (TrashViewForm)f;
            
			if (request.getParameter("action.trash") != null) {
				return handleTrashAction(form, request);
			}

			return handleInitializeAction(form, request);
        } catch (Exception ex) {
            VKCLogManager.errorLog(this, "1", "Exception while retrieving category!", ex);
            throw new ServletException(ex);
        }
    }

	/**
	 * Method deletes the selected category
	 */
	private ActionForward handleTrashAction(TrashViewForm form, HttpServletRequest request) throws Exception {
	    if (!form.getUser().hasAdminPrivileges()) {
			// User has no privileges
			request.setAttribute(WebShare.INFO_KEY, "vkc.info.access_denied");

			return new ActionForward("/showCategory.do?categoryId=" + VKCCategory.ROOT_CATEGORY_ID);
	    }
	    
	    Set catIdsSet = ArrayUtils.toIntegerSet(form.getSelectedCategoryIds());
		Set docIdsSet = ArrayUtils.toIntegerSet(form.getSelectedDocumentIds());
        
	    if (form.getTrashAction().equals("deleteDefinitely")) {
	        // delete all marked files 
			WebShare.database.delete(form.getCategoryId(), catIdsSet, docIdsSet, form.getUser());
			form.setTrashAction("noaction");

			return new ActionForward("/showTrash.do?categoryId=" + form.getCategoryId(), true);
	    } else if (form.getTrashAction().equals("move2Clipboard")) {	        
			// cut marked files into clipbord
	        ClipBoard clipBoard = new ClipBoard();
			clipBoard.setCategoryIds(catIdsSet);
			clipBoard.setDocumentIds(docIdsSet);
			clipBoard.setSourceCategoryId(form.getCategoryId());
			clipBoard.setUser(form.getUser());
			clipBoard.setCategoryFlag(VKCCategory.FLAG_DELETED);
			clipBoard.setDocumentFlag(VKCDocument.FLAG_DELETED);
			clipBoard.setOperation(ClipBoard.CUT_OPERATION);			

			if (clipBoard.execute()) {
			    HttpSession session = request.getSession();
				session.setAttribute(WebShare.CLIPBOARD_KEY, clipBoard);
			} else {
			    ActionErrors errors = new ActionErrors();
			    errors.add("cutfailed", new ActionError("error.cut.selection"));
			}
			
			form.setTrashAction("noaction");
			
			return DEFAULT_FORWARD;
			//sowas nützt doch nur was, wenn category id im scriptlet abgefragt wird?
			//return new ActionForward( "/templates/${template.name}/administration/trashView.jsp?categoryId=" + form.getCategoryId());
	    } else if (form.getTrashAction().equals("noaction")) {
	        return DEFAULT_FORWARD;
	    }
	    
	    form.setTrashAction("noaction");	    
	    
	    return DEFAULT_FORWARD;	    
	}

	private ActionForward handleInitializeAction(TrashViewForm form, HttpServletRequest request) throws Exception {
	    if (!form.getUser().hasAdminPrivileges()) {
			// User hat nicht die Berechtigung zur Trashbehandlung
			return new ActionForward("/showCategory.do?categoryId=" + VKCCategory.ROOT_CATEGORY_ID, true);			
	    }		
	    
		//Kategorie ist nicht mehr im trash
		if (!CategoryUtils.isInTrash(form.getCategoryId())) {
			return new ActionForward("/showCategory.do?categoryId=" + VKCCategory.ROOT_CATEGORY_ID, true); 
		}
		
	    return DEFAULT_FORWARD;
	}
}