/*
 * $Id: UserExpiredDocumentsAction.java,v 1.15 2006/10/04 03:14:29 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: UserExpiredDocumentsAction.java,v $
 * Revision 1.15  2006/10/04 03:14:29  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.14  2006/07/12 12:21:51  marbobo
 * + Bugfix
 *
 * Revision 1.13  2005/09/22 13:18:19  marbobo
 * + Bugfix beim löschen von Kategorien und Dokumenten
 *
 * Revision 1.12  2005/09/01 16:31:57  jonyp
 * changes due to new signature of VKCCategory.deleteCategory/VKCDocument.deleteDocument
 *
 * Revision 1.11  2005/08/30 14:33:44  jonyp
 * fixes due to changes of STATUS_.../FLAG_... from int to Integer
 *
 * Revision 1.10  2005/08/26 14:55:59  jonyp
 * changes due to new paramter(user) for deleteDocument
 *
 * Revision 1.9  2005/08/15 13:56:38  marbobo
 * + Alternative-Sicht implementiert
 *
 * Revision 1.8  2005/02/14 14:01:49  bigmac
 * Applikationsweites CSS konfigurierbar gemacht + Refactorings
 *
 * Revision 1.7  2004/11/02 21:42:28  marbobo
 * + Einige Funktionen für Gast-Benutzer gesperrt
 * + WebShare.LOGIN_PAGE mit WebShare.getLoginPage() ersetzt
 *
 * Revision 1.6  2004/08/16 13:38:58  marbobo
 * + Action aif Reques-parameter umgestellt
 *
 * Revision 1.5  2004/08/04 23:19:18  marbobo
 * + Bugfix
 *
 * Revision 1.4  2004/06/27 17:23:40  marbobo
 * + Quellcode maintenance
 *
 * Revision 1.3  2004/04/27 11:00:25  marbobo
 * + Dokumentsicht erweitert (mit/ohne Details)
 *
 * Revision 1.2  2004/04/22 13:41:12  jonyp
 * - deleted statements to initialize lastAccessor/lastAccessed/accesscounter during recovery of document(s)
 *
 * Revision 1.1  2004/03/25 01:56:37  marbobo
 * ModifyUserExpiredDocumentsForm und Action umbenannt 
 * und Komplett neu überarbeitet
 *
 * Revision 1.13  2004/03/23 15:38:47  bigmac
 * Bestimmung der abgelaufenen Dokumente aktualisiert
 *
 * Revision 1.1  2003/08/07 10:38:23  marbobo
 * Initial Version
 *
 */
package vkc.profile.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

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.entities.VKCDocument;
import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCDocumentSerializer;
import vkc.exception.DBException;
import vkc.profile.form.UserExpiredDocumentsForm;
import vkc.util.DateUtils;
import vkc.view.WebShare;

/**
 * Action-Klasse zum Verwalten von abgelaufenen Benutzerdokumenten
 *
 * @author marbobo
 * @version $Revision: 1.15 $
 */
public final class UserExpiredDocumentsAction extends Action {
	private ActionForward DEFAULT_FORWARD = new ActionForward("/templates/${template.name}/userProfile/userExpiredDocuments.jsp");

    private ActionForward INFO_FORWARD = new ActionForward("/templates/${template.name}/common/InfoPage.jsp");

	/**
	 * Hier werden die entsprechenden Aktionen eingeleitet
	 */
	public synchronized ActionForward execute( ActionMapping mapping, 
	        								   ActionForm f, 
	        								   HttpServletRequest request, 
	        								   HttpServletResponse response )
		throws Exception {
		UserExpiredDocumentsForm form = (UserExpiredDocumentsForm)f;

		if (form.getUser().isGuestUser()) {
			request.setAttribute(WebShare.INFO_KEY, "vkc.info.access_denied");
			
			return INFO_FORWARD;
		}

		if (request.getParameter("action.back") != null) {
			return new ActionForward("/userProfile.do?initialize=true");
		} else 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.documents") != null) {
			return handleDeleteDocumentsAction(form, request);
		} else if (request.getParameter("action.recover.documents") != null) {
			return handleRecoverDocumentsAction(form, request);
		} else if (form.isDeleteButtonPressed(request)) {
			return handleDeleteDocumentAction(form, request);
		} else if (form.isRecoverButtonPressed(request)) {
			return handleRecoverDocumentAction(form, request);
		} else {
			return handleInitializeAction(form, request);
		}
	}

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

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

	private ActionForward handleHideDocumentsAction(UserExpiredDocumentsForm 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/userExpiredDocuments.jsp?anchor=doc" + form.getSelectedId(), false);
	}

	private ActionForward handleShowDocumentsAction(UserExpiredDocumentsForm 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/userExpiredDocuments.jsp?anchor=doc" + form.getSelectedId(), false);
	}

	private ActionForward handleDeleteDocumentAction(UserExpiredDocumentsForm form, HttpServletRequest request) throws DBException {
		List documents = form.getExpiredDocuments();
		Integer documentId = form.getDeleteDocumentId();
		VKCUser user = form.getUser();

		for (int i = 0; i < documents.size(); i++) {
			VKCDocument document = (VKCDocument) documents.get(i);
			
			if (document.getDocumentId().equals(documentId)) {
				document.setFlag(VKCDocument.FLAG_DELETED);
                VKCDocumentSerializer.deleteDocument(user, documentId, !WebShare.isTrashEnabled(), null);
				documents.remove(i);
				break;
			}
		}

		return DEFAULT_FORWARD;
	}

	private ActionForward handleDeleteDocumentsAction(UserExpiredDocumentsForm form, HttpServletRequest request) throws DBException {
		List documents = form.getExpiredDocuments();
		Integer[] documentIds = form.getSelectedIds();
		VKCUser user = form.getUser();

		for (int i = 0; i < documentIds.length; i++) {
			for (int j = 0; j < documents.size(); j++) {
				VKCDocument document = (VKCDocument) documents.get(j);
				
				if (document.getDocumentId().equals(documentIds[i])) {
					document.setFlag(VKCDocument.FLAG_DELETED);
                    VKCDocumentSerializer.deleteDocument(user, documentIds[i], !WebShare.isTrashEnabled(), null);
					documents.remove(j);
					break;
				}
			}
		}

		return DEFAULT_FORWARD;
	}

	private ActionForward handleRecoverDocumentAction(UserExpiredDocumentsForm form, HttpServletRequest request) throws DBException {
		VKCUser user = form.getUser();
		List documents = form.getExpiredDocuments();
		Integer documentId = form.getRecoverDocumentId();

		for (int i = 0; i < documents.size(); i++) {
			VKCDocument document = (VKCDocument) documents.get(i);
			
			if (document.getDocumentId().equals(documentId)) {
				String dateValue = (String)form.getDynaValue("expiredId_" + documentId);
				Date date = DateUtils.parseDate(dateValue);
				document.setExpires(date);
				document.setLastChanged(new Timestamp(System.currentTimeMillis()));
				document.setLastChangerId(user.getUserId());
				document.setFlag(VKCDocument.FLAG_PUBLISHED);
                VKCDocumentSerializer.saveDocument(document, true, false);
				documents.remove(i);
				break;
			}
		}

		return DEFAULT_FORWARD;
	}

	private ActionForward handleRecoverDocumentsAction(UserExpiredDocumentsForm form, HttpServletRequest request) throws DBException {
		VKCUser user = form.getUser();
		List  documents = form.getExpiredDocuments();
		Integer[] documentIds = form.getSelectedIds(); 

		for (int i = 0; i < documentIds.length; i++) {
			for (int j = 0; j < documents.size(); j++) {
				VKCDocument document = (VKCDocument) documents.get(j);
				
				if (document.getDocumentId().equals(documentIds[i])) {
					String dateValue = (String)form.getDynaValue("expiredId_" + documentIds[i]);
					Date date = DateUtils.parseDate(dateValue);
					document.setExpires(date);
					document.setLastChanged(new Timestamp(System.currentTimeMillis()));
					document.setLastChangerId(user.getUserId());
					document.setFlag(VKCDocument.FLAG_PUBLISHED);
                    VKCDocumentSerializer.saveDocument(document, true, false);
					documents.remove(j);
					break;
				}
			}
		}

		return DEFAULT_FORWARD;
	}

	private ActionForward handleInitializeAction(UserExpiredDocumentsForm form, HttpServletRequest request) {
		return DEFAULT_FORWARD;
	}
}