/*
 * $Id: UserExpiredDocumentsForm.java,v 1.6 2006/09/17 21:19:02 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: UserExpiredDocumentsForm.java,v $
 * Revision 1.6  2006/09/17 21:19:02  marbobo
 * + virtowebView-Einträge und Methoden wegen Migration entfernt (nicht mehr nötig)
 *
 * Revision 1.5  2005/08/15 13:56:39  marbobo
 * + Alternative-Sicht implementiert
 *
 * Revision 1.4  2004/08/16 13:38:58  marbobo
 * + Action aif Reques-parameter umgestellt
 *
 * Revision 1.3  2004/04/27 11:00:25  marbobo
 * + Dokumentsicht erweitert (mit/ohne Details)
 *
 * Revision 1.2  2004/04/26 10:44:41  marbobo
 * + Unterstützung für Virtoweb-Sicht eingebaut
 *
 * Revision 1.1  2004/03/25 01:56:36  marbobo
 * ModifyUserExpiredDocumentsForm und Action umbenannt 
 * und Komplett neu überarbeitet
 *
 * Revision 1.7  2003/12/24 10:12:10  bigmac
 * Projektweite Import-Optimierung
 *
 * Revision 1.1  2003/08/07 10:38:24  marbobo
 * Initial Version
 *
 */
package vkc.profile.form;

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

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

import vkc.db.serializer.VKCDocumentSerializer;
import vkc.exception.DBException;
import vkc.util.DateUtils;
import vkc.view.form.DynaForm;


/**
 * Form Bean für die Verwaltung von abgelaufenen Dokumenten eines Benutzers
 *
 * @author marbobo
 * @version $Revision: 1.6 $
 */
public final class UserExpiredDocumentsForm extends DynaForm {
	private Integer deleteDocumentId		= new Integer(-1);
	private Integer recoverDocumentId		= new Integer(-1);
	private Integer selectedId 				= null;
	private Integer[] selectedIds 			= new Integer[0];
	private Integer[] showDocumentDetails 	= new Integer[0];
	private Integer[] showDocuments         = new Integer[0];
	
	private List expiredDocuments = null;

	private String selectedCmdAction  		= "noaction";
	
	/**
	 * Reset der Eingabefelder auf Defaultwerte
	 *
	 * @param mapping The mapping used to select this instance
	 * @param request The servlet request we are processing
	 */
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		super.reset(mapping, request);

		deleteDocumentId		= new Integer(-1);
		recoverDocumentId		= new Integer(-1);
		selectedId 				= null;
		selectedIds 			= new Integer[0];
		showDocumentDetails 	= new Integer[0];
		showDocuments 		    = new Integer[0];

		expiredDocuments = null;

		selectedCmdAction  		= "noaction";
	}

	/**
	 * Validiert die Eingabefelder und gibt gegebenfalls die Fehler zurück
	 *
	 * @param mapping The mapping used to select this instance
	 * @param request The servlet request we are processing
	 */
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();

		if (request.getParameter("action.recover.documents") != null) {
			Integer[] ids = getSelectedIds();
			
			for (int i = 0; i < ids.length; i++) {
				String dateValue = (String) getDynaValue("expiredId_" + ids[i]);
				
				if (!isEmpty(dateValue)) {
					if (DateUtils.parseDate(dateValue) == null) {
						errors.add("invalid_date[" + ids[i] + "]", new ActionError("vkc.profile.error.invalid_date"));
					} else {
						Date today = new Date(System.currentTimeMillis());
						Date d = DateUtils.parseDate(dateValue);
						
						if (today.after(d)) {
							errors.add("invalid_date[" + ids[i] + "]", new ActionError("vkc.profile.error.invalid_date"));
						}
					}
				}
			}
		}
		
		if (isRecoverButtonPressed(request)) {
			Integer dateId = getRecoverDocumentId();
			String dateValue = (String)getDynaValue("expiredId_" + dateId);
			
			if (!isEmpty(dateValue)) {
				if (DateUtils.parseDate(dateValue) == null) {
					errors.add("invalid_date[" + dateId + "]", new ActionError("vkc.profile.error.invalid_date"));
				} else {
					Date today = new Date(System.currentTimeMillis());
					Date d = DateUtils.parseDate(dateValue);
					
					if (today.after(d)) {
						errors.add("invalid_date[" + dateId + "]", new ActionError("vkc.profile.error.invalid_date"));
					}
				}
			}
		}

		return errors;
	}

	public String getSelectedCmdAction() {
		return selectedCmdAction;
	}

	public void setSelectedCmdAction(String s) {
		selectedCmdAction = s;
	}

	public Integer[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(Integer[] integers) {
		selectedIds = integers;
	}

	public List getExpiredDocuments() throws DBException {
		if (expiredDocuments == null) {
			expiredDocuments = new ArrayList(VKCDocumentSerializer.getExpiredDocuments(getUser())); 
		}
		
		return expiredDocuments;
	}
	
	public Integer getRecoverDocumentId() {
		return recoverDocumentId;
	}

	public void setRecoverDocumentId(Integer id) {
		recoverDocumentId = id;
	}
	
	public boolean isRecoverButtonPressed(HttpServletRequest request) {
		String x = request.getParameter("btn_recover.x");
		String y = request.getParameter("btn_recover.y");
		
		return x != null || y != null;
	}

	public Integer getDeleteDocumentId() {
		return deleteDocumentId;
	}

	public void setDeleteDocumentId(Integer id) {
		deleteDocumentId = id;
	}

	public boolean isDeleteButtonPressed(HttpServletRequest request) {
		String x = request.getParameter("btn_delete.x");
		String y = request.getParameter("btn_delete.y");
		
		return x != null || y != null;
	}

	public Integer[] getShowDocuments() {
		return showDocuments;
	}

	public void setShowDocuments(Integer[] list) {
		showDocuments = list;
	}

	public boolean getShowDocs(int i) {
		List ids = Arrays.asList(getShowDocuments());

		return ids.contains(new Integer(i));
	}

	public Integer[] getShowDocumentDetails() {
		return showDocumentDetails;
	}

	public void setShowDocumentDetails(Integer[] list) {
		showDocumentDetails = list;
	}

	public boolean getShowDocDetails(int i) {
		List ids = Arrays.asList(getShowDocumentDetails());
		
		return ids.contains(new Integer(i));
	}

	public Integer getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(Integer i) {
		selectedId = i;
	}
}