/*
 * $Id: InputFieldConstraints.java,v 1.16 2004/11/09 14:35:42 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: InputFieldConstraints.java,v $
 * Revision 1.16  2004/11/09 14:35:42  marbobo
 * + Umstellung von authorId auf publisherId
 * + Hinzufügen von AuthorName für Dokumente
 * + Nötwendige Änderungen betreffend AuthorName und PublisherId
 *
 * Revision 1.15  2004/10/26 15:00:51  jonyp
 * added support of mapping_language
 *
 * Revision 1.14  2004/10/21 13:24:06  vchris
 * Update für neue LDAP Funktionalitäten
 *
 * Revision 1.13  2004/10/18 00:26:56  vchris
 * *** empty log message ***
 *
 * Revision 1.12  2004/07/20 08:57:44  jonyp
 * added copyright info + cvs log
 *
 *
 */

package vkc.view;

import java.util.HashMap;

public class InputFieldConstraints {
	private final static HashMap maxFieldLength = new HashMap();
	
	static {
		maxFieldLength.put( "user.username",     new Integer( 20 ) );
		maxFieldLength.put( "user.password",     new Integer( 255 ) );
		maxFieldLength.put( "user.firstname",    new Integer( 50 ) );
		maxFieldLength.put( "user.lastname",     new Integer( 50 ) );
		maxFieldLength.put( "user.email",        new Integer( 50 ) );
		maxFieldLength.put( "user.salutation",   new Integer( 50 ) );
		maxFieldLength.put( "user.company",      new Integer( 50 ) );
		maxFieldLength.put( "user.department",   new Integer( 50 ) );
		maxFieldLength.put( "user.zip",          new Integer( 10 ) );
		maxFieldLength.put( "user.city",         new Integer( 50 ) );
		maxFieldLength.put( "user.street",       new Integer( 50 ) );
		maxFieldLength.put( "user.phone",        new Integer( 20 ) );
		maxFieldLength.put( "user.fax",          new Integer( 20 ) );
		maxFieldLength.put( "user.mobile",       new Integer( 20 ) );
		maxFieldLength.put( "user.homepage.url", new Integer( 100 ) );
		maxFieldLength.put( "user.contact.url",  new Integer( 100 ) );
		
		maxFieldLength.put( "group.name",         new Integer( 50 ) );
		maxFieldLength.put( "group.description",  new Integer( 50 ) );

		maxFieldLength.put( "document.authorname",  new Integer( 127 ) );
		maxFieldLength.put( "document.title",       new Integer( 255 ) );
		maxFieldLength.put( "document.description", new Integer( 4000 ) );
		maxFieldLength.put( "document.filename",    new Integer( 255 ) );
		maxFieldLength.put( "document.versiondesc", new Integer( 255 ) );

		maxFieldLength.put( "category.name",        new Integer( 255 ) );
		maxFieldLength.put( "category.description", new Integer( 4000 ) );

        maxFieldLength.put( "project.external.contacts", new Integer( 1024 ) );
		maxFieldLength.put( "project.message",           new Integer( 2048 ) );
		maxFieldLength.put( "project.steps", 			 new Integer( 255 ) );

		maxFieldLength.put( "sysparameter.valuestring", new Integer( 1024 ) );
		
		maxFieldLength.put( "ldap.serverUrl", new Integer( 512 ) );
		maxFieldLength.put( "ldap.description", new Integer( 64 ) );
		maxFieldLength.put( "ldap.authmethod", new Integer( 20 ) );
		maxFieldLength.put( "ldap.basedn", new Integer( 512 ) );
		maxFieldLength.put( "ldap.groupRdn", new Integer( 512 ) );
		maxFieldLength.put( "ldap.groupsBaseRDN", new Integer( 128 ) );
		maxFieldLength.put( "ldap.usersBaseRDN", new Integer( 128 ) );
		maxFieldLength.put( "ldap.mapping.loginAttr", new Integer( 32 ) );
		maxFieldLength.put( "ldap.mapping.email", new Integer( 32 ) );
		maxFieldLength.put( "ldap.mapping.userId", new Integer( 32 ) );
		maxFieldLength.put( "ldap.mapping.fullName", new Integer( 32 ) );
		maxFieldLength.put( "ldap.mapping.language", new Integer( 32 ) );
	}

	private InputFieldConstraints() {
	}
	
	/**
	 * Methode liefert die maximal erlaubte Zeichenlänge für ein
	 * Eingabefeld
	 * <p>
	 * @param fieldname - Name des Feldes dessen maximale Länge bestimmt
	 * werden soll
	 * <p>
	 * @return maximale Feldlänge als String
	 */	
	public final static String getMaxLength( String fieldname ) {
		Integer length = (Integer)maxFieldLength.get( fieldname );

		if ( length == null ) {
			return "";
		} 

		return length.toString();
	}

	public final static int getMaxIntLength( String fieldname ) {
		Integer length = (Integer)maxFieldLength.get( fieldname );
		
		if ( length == null ) {
			return Integer.MAX_VALUE;
		} 

		return length.intValue();
	}

	public final static boolean isLengthValid( String value, String lengthConstraint ) {
		if ( value == null ) {
			return true;			
		}

		Integer len = (Integer)maxFieldLength.get( lengthConstraint );
		
		if ( len == null ) {
			return true;
		} 

		return value.length() <= len.intValue();
	}
}