#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.Common.Utilities.Objects.Members; using IBatisNet.DataMapper.Configuration; using IBatisNet.DataMapper.DataExchange; using IBatisNet.DataMapper.TypeHandlers; #endregion namespace IBatisNet.DataMapper.Scope { /// /// The ConfigurationScope maintains the state of the build process. /// public class ConfigurationScope : IScope { /// /// Empty parameter map /// public const string EMPTY_PARAMETER_MAP = "iBATIS.Empty.ParameterMap"; #region Fields private ErrorContext _errorContext = null; private HybridDictionary _providers = new HybridDictionary(); private HybridDictionary _sqlIncludes = 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 _useReflectionOptimizer = true; private bool _validateSqlMap = false; private bool _isCallFromDao = false; private ISqlMapper _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 /// /// The list of sql fragment /// public HybridDictionary SqlIncludes { get { return _sqlIncludes; } } /// /// XmlNamespaceManager /// public XmlNamespaceManager XmlNamespaceManager { set { _nsmgr = value; } get { return _nsmgr; } } /// /// Set if the parser 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 ISqlMapper SqlMapper { set { _sqlMapper = value; } get { return _sqlMapper; } } /// /// A factory for DataExchange objects /// public DataExchangeFactory DataExchangeFactory { get { return _sqlMapper.DataExchangeFactory; } } /// /// 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 we can use reflection optimizer. /// public bool UseReflectionOptimizer { get { return _useReflectionOptimizer; } set { _useReflectionOptimizer = 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 /// /// Register under Statement Name or Fully Qualified Statement Name /// /// An Identity /// The new Identity public string ApplyNamespace(string id) { string newId = id; if (_sqlMapNamespace != null && _sqlMapNamespace.Length > 0 && id != null && id.Length > 0 && id.IndexOf(".") < 0) { newId = _sqlMapNamespace + DomSqlMapBuilder.DOT + id; } return newId; } /// /// Resolves the type handler. /// /// The clazz. /// Name of the member. /// Type of the CLR. /// Type of the db. /// if set to true [for setter]. /// public ITypeHandler ResolveTypeHandler(Type clazz, string memberName, string clrType, string dbType, bool forSetter) { ITypeHandler handler = null; if (clazz==null) { handler = this.DataExchangeFactory.TypeHandlerFactory.GetUnkownTypeHandler(); } else if (typeof(IDictionary).IsAssignableFrom(clazz)) { // IDictionary if (clrType ==null ||clrType.Length == 0) { handler = this.DataExchangeFactory.TypeHandlerFactory.GetUnkownTypeHandler(); } else { try { Type type = TypeUtils.ResolveType(clrType); handler = this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type, dbType); } catch (Exception e) { #if dotnet2 throw new ConfigurationErrorsException("Error. Could not set TypeHandler. Cause: " + e.Message, e); #else throw new ConfigurationException("Error. Could not set TypeHandler. Cause: " + e.Message, e); #endif } } } else if (this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(clazz, dbType) != null) { // Primitive handler = this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(clazz, dbType); } else { // .NET object if (clrType ==null || clrType.Length == 0) { Type type = null; if (forSetter) { type = ObjectProbe.GetMemberTypeForSetter(clazz, memberName); } else { type = ObjectProbe.GetMemberTypeForGetter(clazz, memberName); } handler = this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type, dbType); } else { try { Type type = TypeUtils.ResolveType(clrType); handler = this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type, dbType); } catch (Exception e) { #if dotnet2 throw new ConfigurationErrorsException("Error. Could not set TypeHandler. Cause: " + e.Message, e); #else throw new ConfigurationException("Error. Could not set TypeHandler. Cause: " + e.Message, e); #endif } } } return handler; } } }