// Persist library : Persistence layer
// Copyright (C) 2003 Vincent Daron
//
// 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; either
// version 2.1 of the License, or (at your option) any later version.
//
// 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
// 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, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Specialized;
using Persist.Attributes;
using Persist.Config;
using Persist.Criteria;
using Persist.Converters;
using Persist.IdGenerators;
using Persist.Maps;
using Persist.Sql;
namespace Persist.Config{
/// <summary>
/// This class store the current configuration
/// Typically filled in by a ConfigurationStore
/// </summary>
public class Configuration
{
private IConverter theTrivialConverter = new TrivialConverter();
private Hashtable theIdGenerators = new Hashtable();
private Hashtable theClassMaps = new Hashtable();
private Hashtable theDatabases = new Hashtable();
private Hashtable theConverters = new Hashtable();
private bool IsInitialized = false;
private string theAssemblyName;
/// <summary>
/// This method is called once after the configuration is filled and initialize all the configuration parts
/// </summary>
internal void Init()
{
lock(this)
{
foreach(RelationalDatabaseHolder holder in theDatabases.Values)
{
holder.Init();
}
foreach(IdGeneratorHolder holder in theIdGenerators.Values)
{
holder.Init();
}
foreach(ConverterHolder holder in theConverters.Values)
{
holder.Init(this);
}
foreach(ClassMap cm in theClassMaps.Values)
{
cm.Init(this);
}
IsInitialized = true;
}
}
/// <summary>
/// AssemblyName where the mapped objects are stored
/// </summary>
public string AssemblyName
{
get{return theAssemblyName;}
set{theAssemblyName = value;}
}
/// <summary>
/// Return a ClassMap from that Name
/// </summary>
/// <param name="classMapName">ClassMap Name</param>
/// <returns>the ClassMap</returns>
public ClassMap GetClassMap(string classMapName)
{
return theClassMaps[classMapName] as ClassMap;
}
/// <summary>
/// Return the ClassMap associated to the given persistent oject
/// </summary>
/// <param name="obj">the persistent object</param>
/// <returns>the ClassMap</returns>
public ClassMap GetClassMap(PersistentObject obj)
{
ClassMap classMap = GetClassMap(obj.GetType().FullName);
if(classMap == null)
throw new Exception("No ClassMap for " + obj.GetType().Name + " class");
return classMap;
}
/// <summary>
/// Return the ClassMap associated to the given Type
/// </summary>
/// <param name="type">the Type</param>
/// <returns>the ClassMap</returns>
public ClassMap GetClassMap(Type type)
{
return GetClassMap(type.FullName);
}
/// <summary>
/// Return the RelationalDatabase from that name
/// </summary>
/// <param name="name">the Relationaldatabase Name</param>
/// <returns>theRelational Database</returns>
public RelationalDatabase GetRelationalDatabase(string name)
{
RelationalDatabaseHolder dbHolder = (RelationalDatabaseHolder)theDatabases[name];
if(dbHolder == null)
{
throw new Exception("RelationalDatabase "+name+" not found in configuration");
}
return dbHolder.Instance;
}
/// <summary>
/// Return the Converter from that name
/// </summary>
/// <param name="name">the Converter Name</param>
/// <returns>the Converter</returns>
public IConverter GetConverter(string name)
{
ConverterHolder ch = (ConverterHolder)theConverters[name];
if(ch == null)
throw new Exception("Converter "+name+" not found !");
return ch.Instance;
}
/// <summary>
/// Return the TrivialConverter, a converter that did'nt do anything
/// </summary>
/// <returns>theTrivial Converter</returns>
public IConverter TrivialConverter
{
get
{
return theTrivialConverter;
}
}
/// <summary>
/// Return the IIdGenerator from that name
/// </summary>
/// <param name="name">the IIdGenerator Name</param>
/// <returns>the IIdGenerator</returns>
public IIdGenerator GetIdGenerator(string name)
{
IdGeneratorHolder holder = (IdGeneratorHolder)theIdGenerators[name];
if(holder == null)
throw new Exception("IdGenerator "+name+" not found !");
return holder.Instance;
}
/// <summary>
/// Add a ClassMap to the configuration
/// If the configuration is already initialized, throw a ConfigurationException
/// </summary>
/// <param name="classMap">the classMap</param>
public void AddClassMap(ClassMap classMap)
{
if(IsInitialized)
throw new ConfigurationException("Can't add ClassMap once initialized");
// Remove the assembly name from the Type to retrieve the FullName of the Type
string typeName = classMap.TypeName.Substring(0,classMap.TypeName.IndexOf(','));
Debug.Assert(typeName.Length > 0,"ClassMap Type format incorrect",classMap.TypeName);
theClassMaps.Add(typeName, classMap);
}
/// <summary>
/// Remove a ClassMap from configuration
/// if the Configuration is initialized, throw a ConfigurationException
/// </summary>
/// <param name="classMapName">the name of the class mapto remove</param>
public void RemoveClassMap(string classMapName)
{
if(IsInitialized)
throw new ConfigurationException("Can't remove ClassMap once initialized");
theClassMaps.Remove(classMapName);
}
/// <summary>
/// Add a Relational Database to the configuration
/// If the configuration is already initialized, throw a ConfigurationException
/// </summary>
/// <param name="relationalDatabaseName">the RelationalDatabase Name</param>
/// <param name="relationalDatabaseTypeName">the RelationalDatabase TypeName</param>
/// <param name="parameters">The relationalm database parameters</param>
public void AddRelationalDatabase(string relationalDatabaseName,string relationalDatabaseTypeName,NameValueCollection parameters)
{
if(IsInitialized)
throw new ConfigurationException("Can't add RelationalDatabase once initialized");
theDatabases.Add(relationalDatabaseName,new RelationalDatabaseHolder(relationalDatabaseName,relationalDatabaseTypeName,parameters));
}
/// <summary>
/// Remove a RelationalDatabase from configuration
/// </summary>
/// <param name="relationalDatabaseName">the relational database name</param>
public void RemoveRelationalDatabase(string relationalDatabaseName)
{
if(IsInitialized)
throw new ConfigurationException("Can't remove RelationalDatabase once initialized");
theDatabases.Remove(relationalDatabaseName);
}
/// <summary>
/// Add a Converter to the configuration
/// If the configuration is already initialized, throw a ConfigurationException
/// </summary>
/// <param name="converterName">the converter name</param>
/// <param name="converter">the converter</param>
public void AddConverter(string converterName, ConverterHolder converter)
{
if(IsInitialized)
throw new ConfigurationException("Can't add Converter once initialized");
theConverters.Add(converterName, converter);
}
/// <summary>
/// Remove a converter from configuration
/// </summary>
/// <param name="converterName">the converter name</param>
public void RemoveConverter(string converterName)
{
if(IsInitialized)
throw new ConfigurationException("Can't remove Converter once initialized");
theConverters.Remove(converterName);
}
/// <summary>
/// Add an IdGenerator to the configuration
/// If the configuration is already initialized, throw a ConfigurationException
/// </summary>
/// <param name="idGeneratorName">Id Generator name</param>
/// <param name="typeName">IdGenerator TypeName</param>
/// <param name="singleInstance">Indicate iof multiple iostance of this IdGeenrator are allowed</param>
/// <param name="parameters">IdGenerator Parameters</param>
public void AddIdGenerator(string idGeneratorName,string typeName, bool singleInstance, NameValueCollection parameters)
{
if(IsInitialized)
throw new ConfigurationException("Can't add IdGenerator once initialized");
theIdGenerators.Add(idGeneratorName,new IdGeneratorHolder(idGeneratorName,typeName,parameters,singleInstance));
}
/// <summary>
/// Remove an IdGenerator from configuration
/// </summary>
/// <param name="idGeneratorName">the IdConfigfurati</param>
public void RemoveIdGenerator(string idGeneratorName)
{
if(IsInitialized)
throw new ConfigurationException("Can't remove IdGenerator once initialized");
theIdGenerators.Remove(idGeneratorName);
}
/// <summary>
/// Return the Configured ClassMaps
/// </summary>
public ICollection ClassMaps
{
get{return theClassMaps.Values;}
}
/// <summary>
/// Return the configured Databases
/// </summary>
public ICollection Databases
{
get{return theDatabases.Values;}
}
/// <summary>
/// Return the configured parameters
/// </summary>
public ICollection Converters
{
get{return theConverters.Values;}
}
/// <summary>
/// return the IdGenerators
/// </summary>
public ICollection IdGenerators
{
get{return theIdGenerators.Values;}
}
}
}
|