/*
 * $Id: LdapServerListAction.java,v 1.6 2006/10/04 03:15:07 marbobo Exp $
 * 
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: LdapServerListAction.java,v $
 * Revision 1.6  2006/10/04 03:15:07  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.5  2005/02/14 14:01:55  bigmac
 * Applikationsweites CSS konfigurierbar gemacht + Refactorings
 *
 * Revision 1.4  2004/11/10 15:02:50  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.3  2004/10/18 13:18:37  vchris
 * *** empty log message ***
 *
 * Revision 1.2  2004/10/18 00:26:55  vchris
 * *** empty log message ***
 *
 * Revision 1.1  2004/10/15 15:42:08  vchris
 * Initiale Version
 *
 *
 */

package vkc.ldap.administration.action;

import javax.servlet.ServletException;
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.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.VKCUser;
import vkc.ldap.administration.form.LdapServerListForm;
import vkc.ldap.db.entities.VKCLdapServer;
import vkc.ldap.db.serializer.VKCLdapServerSerializer;
import vkc.misc.VKCLogManager;
import vkc.view.WebShare;

public class LdapServerListAction extends Action {
    
	private ActionForward DEFAULT_FORWARD = new ActionForward( "/templates/${template.name}/ldap/administration/ldapServerList.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 ServletException {
		try {
			LdapServerListForm form = (LdapServerListForm)f;
			VKCUser user = form.getUser();

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

			if (request.getParameter("action.deleteServers") != null) {
				handleDeleteAction( form, request );
			} else if (request.getParameter("action.disableServers") != null) {
				handleDisableServers(form, request);
			} else if (request.getParameter("action.enableServers") != null) {
				handleEnableServers(form, request);
			}

			return handleInitializeAction( form, request );


		} catch( Exception ex ) {
			throw new ServletException( ex );
		}
		
	}
	
	private ActionForward handleInitializeAction( LdapServerListForm form, HttpServletRequest request ) {
		return DEFAULT_FORWARD;
	}
	
	private ActionForward handleDeleteAction( LdapServerListForm form, HttpServletRequest request ) {
		
		Integer[] selectedIds = form.getSelectedIds();
		for (int i = 0; i < selectedIds.length; i++) {
			try {
				VKCLdapServerSerializer.deleteLdapServer(selectedIds[i]);
			}
			catch (Exception e) {
				VKCLogManager.errorLog(this, "", "An error occoured while trying to delete LDAP Server ID = " + selectedIds[i],e);
				ActionErrors errors = new ActionErrors();
				errors.add( "changeServerFailed", new ActionError( "vkc.administration.ldap.changeServerFailed" ) );
				request.setAttribute( Globals.ERROR_KEY, errors );
			}
		}
		
		return DEFAULT_FORWARD;
	}
	
	private ActionForward handleDisableServers( LdapServerListForm form, HttpServletRequest request ) {
		Integer[] selectedIds = form.getSelectedIds();
		
		for (int i = 0; i < selectedIds.length; i++) {
			try {
				VKCLdapServer ldapServer = VKCLdapServerSerializer.retrieveLdapServer(selectedIds[i]);
				if (ldapServer.isEnabled()) {
					ldapServer.setEnabled(false);
					VKCLdapServerSerializer.saveLdapServer(ldapServer);
				}
			}
			catch (Exception e) {
				VKCLogManager.errorLog(this, "", "An error occoured while trying to disable LDAP Server ID = " + selectedIds[i],e);
				ActionErrors errors = new ActionErrors();
				errors.add( "disableServerFailed", new ActionError( "vkc.administration.ldap.changeServerFailed" ) );
				request.setAttribute( Globals.ERROR_KEY, errors );
			}
		}

		return DEFAULT_FORWARD;
	}

	private ActionForward handleEnableServers( LdapServerListForm form, HttpServletRequest request ) {
		Integer[] selectedIds = form.getSelectedIds();
		
		for (int i = 0; i < selectedIds.length; i++) {
			try {
				VKCLdapServer ldapServer = VKCLdapServerSerializer.retrieveLdapServer(selectedIds[i]);
				if ( ! ldapServer.isEnabled()) {
					ldapServer.setEnabled(true);
					VKCLdapServerSerializer.saveLdapServer(ldapServer);
				}
			}
			catch (Exception e) {
				VKCLogManager.errorLog(this, "", "An error occoured while trying to disable LDAP Server ID = " + selectedIds[i],e);
				ActionErrors errors = new ActionErrors();
				errors.add( "disableServerFailed", new ActionError( "vkc.administration.ldap.changeServerFailed" ) );
				request.setAttribute( Globals.ERROR_KEY, errors );
			}
		}

		return DEFAULT_FORWARD;
	}
}