/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.util;

import org.bodington.util.*;
import java.util.Properties;
import java.util.Enumeration;
import java.util.Vector;
import java.util.StringTokenizer;

public class DobPasswordGenerator implements org.bodington.util.PasswordGenerator
	{
	
	public DobPasswordGenerator()
		{
		}
	
	public void setAllowedCharacters( String str )
		throws PasswordGeneratorException
		{
		}
	
    public void setGlobalProperties(Properties props) throws PasswordGeneratorException
    	{
    	}
    	
    public Enumeration getGlobalPropertyNames()
    	{
    	Vector l = new Vector();
        return l.elements();
    	}

    public Enumeration getUserPropertyNames()
    	{
    	Vector l = new Vector();
    	l.addElement( "student_id" );
    	l.addElement( "date_of_birth" );
    	l.addElement( "academic_year" );
        return l.elements();
    	}

    public String generate(Properties props) throws PasswordGeneratorException
    	{
    	String date_of_birth;
		StringTokenizer tok;
		int day, month, year;
    	
    	if ( props == null )
    		throw new PasswordGeneratorException( "Null properties not allowed when generating a password." );
    		
    	date_of_birth = props.getProperty( "date_of_birth" );
    	
    	if ( date_of_birth==null )
    		throw new PasswordGeneratorException( "Missing user property when attempting to generate a password." );
    		
    	tok = new StringTokenizer( date_of_birth, "/:,\\;:*#." );
    	if ( tok.countTokens()!=3 )
    		throw new PasswordGeneratorException( "Missing component of date of birth user property when attempting to generate a password." );
    		
    	try
    		{
    		day    = Integer.parseInt( tok.nextToken() );
    		month  = Integer.parseInt( tok.nextToken() );
    		year   = Integer.parseInt( tok.nextToken() );
    		}
    	catch ( NumberFormatException nfex )
    		{
    		throw new PasswordGeneratorException( "An invalid number was found in a user property." );
    		}
    			
    	return "" + day + "/" + month + "/" + year;
    	}


	
	public boolean isOffLine()
		{
		return true;  // password IS stored off line - student uses date of birth.
		}

	public String offLineAdvice()
		{
		return "The generated password is the student's date of birth in the format day/month/year.  There are no leading zeros and the year is four digits";
		}
	}
