#region Apache Notice
/*****************************************************************************
* $Header: $
* $Revision: $
* $Date: $
*
* iBATIS.NET Data Mapper
* Copyright (C) 2004 - Gilles Bayon
*
*
* 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.
*
********************************************************************************/
#endregion
#region Using
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Xml;
using IBatisNet.Common;
using IBatisNet.Common.Utilities;
using IBatisNet.Common.Utilities.Objects;
using IBatisNet.DataMapper.TypeHandlers;
#endregion
namespace IBatisNet.DataMapper.Scope
{
///
/// The ConfigurationScope maintains the state of the build process.
///
public class ConfigurationScope
{
#region Fields
private ErrorContext _errorContext = null;
private HybridDictionary _providers = new HybridDictionary();
private NameValueCollection _properties = new NameValueCollection();
private XmlDocument _sqlMapConfigDocument = null;
private XmlDocument _sqlMapDocument = null;
private XmlNode _nodeContext = null;
private bool _useConfigFileWatcher = false;
private bool _useStatementNamespaces = false;
private bool _isCacheModelsEnabled = false;
private bool _useEmbedStatementParams = false;
private bool _validateSqlMap = false;
private bool _validateSqlMapConfig = true;
private bool _isCallFromDao = false;
private SqlMapper _sqlMapper = null;
private string _sqlMapNamespace = null;
private DataSource _dataSource = null;
private bool _isXmlValid = true;
private XmlNamespaceManager _nsmgr = null;
private HybridDictionary _cacheModelFlushOnExecuteStatements = new HybridDictionary();
#endregion
#region Constructors
///
/// Default constructor
///
public ConfigurationScope()
{
_errorContext = new ErrorContext();
_providers.Clear();
}
#endregion
#region Properties
///
/// XmlNamespaceManager
///
public XmlNamespaceManager XmlNamespaceManager
{
set { _nsmgr = value; }
get { return _nsmgr; }
}
///
/// Indicates whether or not to validate the configuration document
///
public bool ValidateSqlMapConfig
{
set { _validateSqlMapConfig = value; }
get { return _validateSqlMapConfig; }
}
///
/// Set if theparser should validate the sqlMap documents
///
public bool ValidateSqlMap
{
set { _validateSqlMap = value; }
get { return _validateSqlMap; }
}
///
/// Tells us if the xml configuration file validate the schema
///
public bool IsXmlValid
{
set { _isXmlValid = value; }
get { return _isXmlValid; }
}
///
/// The current SqlMap namespace.
///
public string SqlMapNamespace
{
set { _sqlMapNamespace = value; }
get { return _sqlMapNamespace; }
}
///
/// The SqlMapper we are building.
///
public SqlMapper SqlMapper
{
set { _sqlMapper = value; }
get { return _sqlMapper; }
}
///
/// The current TypeHandlerFactory
///
internal TypeHandlerFactory TypeHandlerFactory
{
get { return _sqlMapper.TypeHandlerFactory; }
}
///
/// Tell us if we are in a DataAccess context.
///
public bool IsCallFromDao
{
set { _isCallFromDao = value; }
get { return _isCallFromDao; }
}
///
/// Tell us if we cache model is enabled.
///
public bool IsCacheModelsEnabled
{
set { _isCacheModelsEnabled = value; }
get { return _isCacheModelsEnabled; }
}
///
/// External data source
///
public DataSource DataSource
{
set { _dataSource = value; }
get { return _dataSource; }
}
///
/// The current context node we are analizing
///
public XmlNode NodeContext
{
set { _nodeContext = value; }
get { return _nodeContext; }
}
///
/// The XML SqlMap config file
///
public XmlDocument SqlMapConfigDocument
{
set { _sqlMapConfigDocument = value; }
get { return _sqlMapConfigDocument; }
}
///
/// A XML SqlMap file
///
public XmlDocument SqlMapDocument
{
set { _sqlMapDocument = value; }
get { return _sqlMapDocument; }
}
///
/// Tell us if we use Configuration File Watcher
///
public bool UseConfigFileWatcher
{
set { _useConfigFileWatcher = value; }
get { return _useConfigFileWatcher; }
}
///
/// Tell us if we use statements namespaces
///
public bool UseStatementNamespaces
{
set { _useStatementNamespaces = value; }
get { return _useStatementNamespaces; }
}
///
/// Get the request's error context
///
public ErrorContext ErrorContext
{
get { return _errorContext; }
}
///
/// List of providers
///
public HybridDictionary Providers
{
get { return _providers; }
}
///
/// List of global properties
///
public NameValueCollection Properties
{
get { return _properties; }
}
///
/// Indicates if parameters should be embedded in the sql statement.
///
public bool UseEmbedStatementParams
{
get { return _useEmbedStatementParams; }
set { _useEmbedStatementParams = value; }
}
///
/// Temporary storage for mapping cache model ids (key is System.String) to statements (value is IList which contains IMappedStatements).
///
public HybridDictionary CacheModelFlushOnExecuteStatements
{
get { return _cacheModelFlushOnExecuteStatements; }
set { _cacheModelFlushOnExecuteStatements = value; }
}
#endregion
///
///
///
/// Type of the ResultMap
/// Property name to map
///
///
///
public ITypeHandler ResolveTypeHandler(Type clazz, string propertyName, string clrType, string dbType)
{
ITypeHandler handler = null;
if (clazz==null)
{
handler = this.TypeHandlerFactory.GetUnkownTypeHandler();
}
else if (typeof(IDictionary).IsAssignableFrom(clazz))
{
// IDictionary
if (clrType ==null ||clrType.Length == 0)
{
handler = this.TypeHandlerFactory.GetUnkownTypeHandler();
}
else
{
try
{
Type type = Resources.TypeForName(clrType);
handler = this.TypeHandlerFactory.GetTypeHandler(type, dbType);
}
catch (Exception e)
{
throw new ConfigurationException("Error. Could not set TypeHandler. Cause: " + e.Message, e);
}
}
}
else if (this.TypeHandlerFactory.GetTypeHandler(clazz, dbType) != null)
{
// Primitive
handler = this.TypeHandlerFactory.GetTypeHandler(clazz, dbType);
}
else
{
// .NET object
if (clrType ==null || clrType.Length == 0)
{
Type type = ObjectProbe.GetPropertyTypeForGetter(clazz, propertyName);
handler = this.TypeHandlerFactory.GetTypeHandler(type, dbType);
}
else
{
try
{
Type type = Resources.TypeForName(clrType);
handler = this.TypeHandlerFactory.GetTypeHandler(type, dbType);
}
catch (Exception e)
{
throw new ConfigurationException("Error. Could not set TypeHandler. Cause: " + e.Message, e);
}
}
}
return handler;
}
}
}