/*
 * $Id: NewLdapServerAction.java,v 1.9 2006/10/04 03:15:08 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: NewLdapServerAction.java,v $
 * Revision 1.9  2006/10/04 03:15:08  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.8  2005/02/14 14:01:55  bigmac
 * Applikationsweites CSS konfigurierbar gemacht + Refactorings
 *
 * Revision 1.7  2004/11/10 15:02:50  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.6  2004/11/09 16:08:01  marbobo
 * - Unnötige Anweisungen entfernt
 *
 * Revision 1.5  2004/11/01 15:52:18  vchris
 * + Anpassungen an neues DB-Schema
 * + Behandlung von LastChangerId und LastChanged
 *
 * Revision 1.4  2004/10/26 15:00:15  jonyp
 * added support of mapping_language
 *
 * Revision 1.3  2004/10/21 13:24:07  vchris
 * Update für neue LDAP Funktionalitäten
 *
 * Revision 1.2  2004/10/18 13:18:37  vchris
 * *** empty log message ***
 *
 * Revision 1.1  2004/10/18 00:27:30  vchris
 * Initiale Version
 *
 *
 */

package vkc.ldap.administration.action;

import java.io.IOException;
import java.sql.Timestamp;

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.NewLdapServerForm;
import vkc.ldap.db.entities.VKCLdapServer;
import vkc.ldap.db.serializer.VKCLdapServerSerializer;
import vkc.misc.VKCLogManager;
import vkc.view.WebShare;

public class NewLdapServerAction extends Action {
    private ActionForward DEFAULT_FORWARD = new ActionForward( "/templates/${template.name}/ldap/administration/newLdapServer.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 IOException, ServletException {
        try {
            NewLdapServerForm form = (NewLdapServerForm) f;
            VKCUser user = form.getUser();
            
            if ( !user.hasAdminPrivileges() ) {
                request.setAttribute( WebShare.INFO_KEY, "vkc.info.access_denied" );
                
                return INFO_FORWARD;
            }

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

            return handleInitializeAction( form, request );
        } catch ( Throwable e ) {
            VKCLogManager.errorLog(this, "", e.getMessage(),e);
            throw new ServletException( e );
        }
    }


	private ActionForward handleInitializeAction( NewLdapServerForm form, HttpServletRequest request ) throws ServletException {
        try {
            return DEFAULT_FORWARD;
        } catch ( Exception e ) {
            throw new ServletException( e );
        }
    }
	
	private ActionForward handleBackAction() {
		return new ActionForward( "/ldapServerList.do", false );
	}
	
	private ActionForward handleCreateAction( NewLdapServerForm form, HttpServletRequest request, VKCUser user ) {
		// Server anlegen.
		VKCLdapServer ldapServer = new VKCLdapServer();
		ldapServer.setLdapServerURL( form.getServerUrl() );
		ldapServer.setDescription( form.getDescription() );
		ldapServer.setBaseDN( form.getBaseDN() );
		ldapServer.setAuthMethod( form.getAuthmethod() );
		ldapServer.setUsersBaseRDN( form.getUsersBaseRDN() );
		ldapServer.setGroupsBaseRDN( form.getGroupsBaseRDN() );
		ldapServer.setMapping_loginattr( form.getMappingLoginAttr() );
		ldapServer.setMapping_userid( form.getMappingUserId() );
		ldapServer.setMapping_fullname( form.getMappingFullName() );
		ldapServer.setMapping_email( form.getMappingEmail() );
		ldapServer.setMapping_language( form.getMappingLanguage() );
		ldapServer.setCreatorId( user.getUserId() );
		ldapServer.setLastChangerId( user.getUserId() );
		ldapServer.setEnabled( request.getParameter( "enabled" ) != null );
		ldapServer.setLastChanged( new Timestamp( System.currentTimeMillis() ) );
		
		try {

			VKCLdapServerSerializer.saveLdapServer(ldapServer);
		}
		catch (Exception e) {
			VKCLogManager.errorLog(this,"","Unable to save new LDAP Server '" + ldapServer.getLdapServerURL() + "' to DB.",e);

			ActionErrors errors = new ActionErrors();
			errors.add( "newServerFailed", new ActionError( "vkc.administration.ldap.newServerFailed" ) );
			request.setAttribute( Globals.ERROR_KEY, errors );
			
			return DEFAULT_FORWARD;
		}
		
		return handleBackAction();	
	}
}
