/*
 * $Id: EditLdapServerForm.java,v 1.8 2004/11/24 17:26:12 jonyp Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: EditLdapServerForm.java,v $
 * Revision 1.8  2004/11/24 17:26:12  jonyp
 * added getAuthenticationMethods
 *
 * Revision 1.7  2004/11/10 15:02:49  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:23  jonyp
 * added support of mapping_language
 *
 * Revision 1.3  2004/10/21 13:24:06  vchris
 * Update für neue LDAP Funktionalitäten
 *
 * Revision 1.2  2004/10/18 13:18:36  vchris
 * *** empty log message ***
 *
 * Revision 1.1  2004/10/18 00:27:30  vchris
 * Initiale Version
 *
 *
 */

package vkc.ldap.administration.form;

import java.sql.Timestamp;
import java.util.ArrayList;
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.VKCUserSerializer;
import vkc.ldap.db.entities.VKCLdapServer;
import vkc.misc.VKCLogManager;
import vkc.view.bean.UserInfo;
import vkc.view.form.DynaForm;

public final class EditLdapServerForm extends DynaForm {
	private String serverUrl 			= "";
	private String description 			= "";
	private String baseDN 				= "";
	private String usersBaseRDN 		= "";
	private String groupsBaseRDN 		= "";
	private String mappingLoginAttr		= "";
	private String mappingUserId		= "";
	private String mappingEmail			= "";
	private String mappingFullName		= "";
	private String mappingLanguage		= "";
	private String lastChanger			= "";
	private Timestamp lastChanged		= null;
	private boolean enabled;
	private String authmethod;

	public void reset( ActionMapping mapping, HttpServletRequest request ) {
		super.reset( mapping, request );
    }   

    public ActionErrors validate( ActionMapping mapping, HttpServletRequest request ) {
        ActionErrors errors = new ActionErrors();

		setEnabled(request.getParameter("enabled") != null);

		if ( request.getParameter( "action.changeServer" ) != null ) {
	        if ( isEmpty( getServerUrl() ) ) {
	            errors.add( "serverUrl", new ActionError( "vkc.administration.ldap.error.serverUrlRequired" ) );
	        }
	
	        if ( isEmpty( getDescription() ) ) {
	        	errors.add( "description", new ActionError( "vkc.administration.ldap.error.descriptionRequired" ) );
	        }
	
	        if ( isEmpty( getAuthmethod() ) ) {
	            errors.add( "authmethod", new ActionError( "vkc.administration.ldap.error.authmethodRequired" ) );
	        }
	        
	        if ( isEmpty( getBaseDN() ) ) {
	            errors.add( "basedn", new ActionError( "vkc.administration.ldap.error.basednRequired" ) );
	        }
	        
			if ( isEmpty( getUsersBaseRDN() ) ) {
				errors.add( "usersBaseRDN", new ActionError ( "vkc.administration.ldap.error.usersBaseRDNRequired" ) );
			}

	        if ( isEmpty( getGroupsBaseRDN() ) ) {
	        	errors.add( "groupsBaseRDN", new ActionError ( "vkc.administration.ldap.error.groupsBaseRDNRequired" ) );
	        }

			if ( isEmpty( getMappingLoginAttr() ) ) {
				errors.add( "mapping.loginAttr", new ActionError ( "vkc.administration.ldap.error.mapping.loginAttrRequired" ) );
			}

			if ( isEmpty( getMappingUserId() ) ) {
				errors.add( "mapping.userId", new ActionError ( "vkc.administration.ldap.error.mapping.userIdRequired" ) );
			}
	
			if ( isEmpty( getMappingFullName() ) ) {
				errors.add( "mapping.fullName", new ActionError ( "vkc.administration.ldap.error.mapping.FullNameRequired" ) );
			}
		}

		return errors;
    }
    
	public void synchronizeForm(VKCLdapServer ldapServer) {
		setServerUrl(ldapServer.getLdapServerURL());
		setDescription(ldapServer.getDescription());
		setAuthmethod(ldapServer.getAuthMethod());
		setBaseDN(ldapServer.getBaseDN());
		setEnabled(ldapServer.isEnabled());
		setUsersBaseRDN(ldapServer.getUsersBaseRDN());
		setGroupsBaseRDN(ldapServer.getGroupsBaseRDN());
		setMappingLoginAttr(ldapServer.getMapping_loginattr());
		setMappingEmail(ldapServer.getMapping_email());
		setMappingUserId(ldapServer.getMapping_userid());
		setMappingFullName(ldapServer.getMapping_fullname());
		setMappingLanguage(ldapServer.getMapping_language());
		setLastChanged(ldapServer.getLastChanged());
		Integer lastChangerId = ldapServer.getLastChangerId();
		if (lastChangerId != null) {
			try {
				UserInfo userInfo = VKCUserSerializer.retrieveUserInfo(lastChangerId);
				setLastChanger(userInfo.getFullName());
			} catch (Exception ex) {
				VKCLogManager.errorLog(this,"","Error retrieving User Info for UserID = " + lastChangerId,ex);
			}
		}
	}
	
	public String getAuthmethod() {
		return authmethod;
	}
	
	public void setAuthmethod(String authmethod) {
		this.authmethod = authmethod;
	}
	
	public String getBaseDN() {
		return baseDN;
	}
	
	public void setBaseDN(String baseDN) {
		this.baseDN = baseDN;
	}
	
	public String getDescription() {
		return description;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}
	
	public boolean isEnabled() {
		return enabled;
	}
	
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	
	public String getServerUrl() {
		return serverUrl;
	}
	
	public void setServerUrl(String serverUrl) {
		this.serverUrl = serverUrl;
	}
	
	public String getGroupsBaseRDN() {
		return groupsBaseRDN;
	}

	public String getMappingEmail() {
		return mappingEmail;
	}

	public String getMappingFullName() {
		return mappingFullName;
	}

	public String getMappingLanguage() {
		return mappingLanguage;
	}
	
	public String getMappingLoginAttr() {
		return mappingLoginAttr;
	}

	public String getMappingUserId() {
		return mappingUserId;
	}

	public String getUsersBaseRDN() {
		return usersBaseRDN;
	}

	public void setGroupsBaseRDN(String string) {
		groupsBaseRDN = string;
	}

	public void setMappingEmail(String string) {
		mappingEmail = string;
	}

	public void setMappingFullName(String string) {
		mappingFullName = string;
	}

	public void setMappingLanguage(String string) {
		mappingLanguage = string;
	}
	
	public void setMappingLoginAttr(String string) {
		mappingLoginAttr = string;
	}

	public void setMappingUserId(String string) {
		mappingUserId = string;
	}

	public void setUsersBaseRDN(String string) {
		usersBaseRDN = string;
	}

	public Timestamp getLastChanged() {
		return lastChanged;
	}
	
	public void setLastChanged(Timestamp lastChanged) {
		this.lastChanged = lastChanged;
	}
	
	public String getLastChanger() {
		return lastChanger;
	}
	
	public void setLastChanger(String lastChanger) {
		this.lastChanger = lastChanger;
	}
	
	public List getAuthenticationMethods() {
	    
	    ArrayList authMethods = new ArrayList();    
	    
	    authMethods.add("simple");
	    
	    return authMethods;
	}
}