/***********************************************************************
 * 
 *  Copyright (C) 2005-2006 Novell, Inc. All Rights Reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; version 2.1
 *  of the License.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, Novell, Inc.
 * 
 *  To contact Novell about this file by physical or electronic mail, 
 *  you may find current contact information at www.novell.com.
 * 
 ***********************************************************************/


using System;
using System.Collections;
using System.Threading;
using sscs.common;

namespace sscs.cache
{

class KeyChain : IKeychain
{
    //We can have a Arraylist in case we dont need to look up via SecretID
    // Hashtable has [SecretID(string), Secret(class)]
    private Hashtable tSecretList = new Hashtable();
    private Hashtable SecretList; // = Hashtable.Synchronized(tSecretList);

    private string            keyChainId;
    
    private DateTime createdTime;
    internal  DateTime CreatedTime
    {
        get
        {
            return createdTime;
        }
        set
        {
            createdTime = value;
        }  
    }
    private DateTime accessedTime;
    internal DateTime AccessedTime
    {
        get
        {
            return accessedTime;
        }
        set
        {
            accessedTime = value;
        }
    }

    private DateTime modifiedTime;
    internal DateTime ModifiedTime
    {
        get
        {
            return modifiedTime;
        }
        set
        {
            modifiedTime = value;
        }
    }

    internal KeyChain()
    {
        SecretList = Hashtable.Synchronized(tSecretList);
    }

    internal KeyChain(string keyChainId)
    {
        SecretList = Hashtable.Synchronized(tSecretList);
        this.keyChainId = keyChainId;
    }
    ~KeyChain()
    {
    }
    
    public void SetKey(string value)
    {
        keyChainId = value;
    }

    public void AddSecret(Secret mySecret)
    {
        try
        {
            mySecret.CreatedTime = DateTime.Now;
            SecretList.Add((mySecret.GetKey()), mySecret);
            this.ModifiedTime = DateTime.Now;
        }
        catch(Exception e)
        {
            CSSSLogger.ExpLog(e.ToString());
            throw e;
        }
    }
    
    public void RemoveSecret(String secretID)
    {   
		// remove all keyvalues first, as to unlink reverse links
		try 
		{
			Secret secret = GetSecret(secretID);
			secret.RemoveAllKeyValuePairs(this);
		}
		catch (Exception e)
		{
            CSSSLogger.ExpLog(e.ToString());
		}

        SecretList.Remove(secretID);
        this.ModifiedTime = DateTime.Now;
    }
    
	public void RemoveAllSecrets()
	{
		SecretList.Clear();
	}

    public Secret GetSecret(string secretID)
    {
        if(SecretList.ContainsKey(secretID))
        {
            Secret secret = (Secret)SecretList[secretID];
            secret.AccessedTime = DateTime.Now;
            return secret;
        }
        else
            throw new SecretNotFoundException(secretID);
    }

    internal string GetKey()
    {
        return keyChainId;
    }
    

    internal IEnumerator GetAllSecrets()
    {
        return SecretList.GetEnumerator();
    }
    internal int GetNumSecrets()
    {
        return SecretList.Count;
    }

    internal bool CheckIfSecretExists(string id)
    {
        if( SecretList.ContainsKey(id) )
            return true;
        else
            return false;
    }
}
}