#region Apache Notice
/*****************************************************************************
* $Revision: 576082 $
* $LastChangedDate$
* $LastChangedBy$
*
* iBATIS.NET Data Mapper
* Copyright (C) 2008/2005 - The Apache Software Foundation
*
*
* 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.Generic;
using System.Data;
using System.Diagnostics;
using Apache.Ibatis.Common.Contracts;
using Apache.Ibatis.Common.Exceptions;
using Apache.Ibatis.Common.Utilities;
using Apache.Ibatis.Common.Utilities.Objects;
using Apache.Ibatis.DataMapper.DataExchange;
using Apache.Ibatis.DataMapper.TypeHandlers;
#endregion
namespace Apache.Ibatis.DataMapper.Model.ResultMapping
{
///
/// Main implementation of ResultMap interface
///
[Serializable]
[DebuggerDisplay("ResultMap: {Id}-{ClassName}")]
public sealed class ResultMap : ResultMapEventSupport, IResultMap
{
private static IResultMap nullResultMap = null;
#region Fields
[NonSerialized]
private bool isInitalized = true;
[NonSerialized]
private readonly string id = string.Empty;
[NonSerialized]
private readonly string className = string.Empty;
[NonSerialized]
private readonly string extendMap = string.Empty;
[NonSerialized]
private readonly Type type = null;
[NonSerialized]
private readonly List groupByPropertyNames = new List();
[NonSerialized]
private readonly ResultPropertyCollection properties = new ResultPropertyCollection();
[NonSerialized]
private readonly ResultPropertyCollection groupByProperties = new ResultPropertyCollection();
[NonSerialized]
private readonly ArgumentPropertyCollection parameters = new ArgumentPropertyCollection();
[NonSerialized]
private readonly Discriminator discriminator = null;
[NonSerialized]
private readonly IFactory objectFactory = null;
[NonSerialized]
private IDataExchange dataExchange = null;
[NonSerialized]
private readonly bool isSimpleType = false;
[NonSerialized]
private readonly List keyPropertyNames = new List();
[NonSerialized]
private readonly ResultPropertyCollection keysProperties = new ResultPropertyCollection();
#endregion
#region Properties
///
/// The GroupBy Properties.
///
public List GroupByPropertyNames
{
get { return groupByPropertyNames; }
}
///
/// Gets or sets a value indicating whether this instance is initalized.
///
///
/// true if this instance is initalized; otherwise, false.
///
public bool IsInitalized
{
get { return true; }
set { isInitalized = value; }
}
///
/// The discriminator used to choose the good Case
///
public Discriminator Discriminator
{
get { return discriminator; }
}
///
/// The collection of ResultProperty.
///
public ResultPropertyCollection Properties
{
get { return properties; }
}
///
/// The GroupBy Properties.
///
public ResultPropertyCollection GroupByProperties
{
get { return groupByProperties; }
}
///
/// The collection of constructor parameters.
///
public ArgumentPropertyCollection Parameters
{
get { return parameters; }
}
///
/// Identifier used to identify the resultMap amongst the others.
///
/// GetProduct
public string Id
{
get { return id; }
}
///
/// Extend ResultMap attribute
///
public string ExtendMap
{
get { return extendMap; }
}
///
/// The output type class of the resultMap.
///
public Type Class
{
get { return type; }
}
///
/// Gets the name of the class.
///
/// The name of the class.
public string ClassName
{
get { return className; }
}
///
/// Sets the IDataExchange
///
public IDataExchange DataExchange
{
set { dataExchange = value; }
}
#endregion
///
/// Initializes a new instance of the class.
///
/// Identifier used to identify the resultMap amongst the others.
/// The output class name of the resultMap.
/// The extend result map bame.
/// The groupBy properties
/// The key columns.
/// The result type.
/// The data exchange.
/// The object factory.
/// The type handler factory.
/// The properties.
/// The parameters.
/// The discriminator.
public ResultMap(
string id,
string className,
string extendMap,
string groupBy,
string keyColumns,
Type type,
IDataExchange dataExchange,
IFactory objectFactory,
TypeHandlerFactory typeHandlerFactory,
ResultPropertyCollection properties,
ArgumentPropertyCollection parameters,
Discriminator discriminator)
{
Contract.Require.That(id, Is.Not.Null & Is.Not.Empty).When("retrieving argument id in ResultMap constructor");
Contract.Require.That(className, Is.Not.Null & Is.Not.Empty).When("retrieving argument className in ResultMap constructor");
Contract.Require.That(type, Is.Not.Null).When("retrieving argument type in ResultMap constructor");
Contract.Require.That(typeHandlerFactory, Is.Not.Null).When("retrieving argument typeHandlerFactory in ResultMap constructor");
nullResultMap = new NullResultMap();
this.id = id;
this.className = className;
this.extendMap = extendMap;
this.type = type;
this.dataExchange = dataExchange;
this.properties = properties;
this.parameters = parameters;
this.discriminator = discriminator;
this.objectFactory = objectFactory;
isSimpleType = typeHandlerFactory.IsSimpleType(type);
if (!string.IsNullOrEmpty(groupBy))
{
string[] props = groupBy.Split(',');
for (int i = 0; i < props.Length; i++)
{
string memberName = props[i].Trim();
groupByPropertyNames.Add(memberName);
}
InitializeGroupByProperties();
CheckGroupBy();
}
if (!string.IsNullOrEmpty(keyColumns))
{
string[] columns = keyColumns.Split(',');
for (int i = 0; i < columns.Length; i++)
{
string column = columns[i].Trim();
keyPropertyNames.Add(column);
}
InitializeKeysProperties();
CheckKeysProperties();
}
}
#region IResultMap Members
///
/// Create an instance Of result.
///
/// An array of values that matches the number, order and type
/// of the parameters for this constructor.
/// An object.
public object CreateInstanceOfResult(object[] parameters)
{
if (!isSimpleType)
{
parameters = RaisePreCreateEvent(parameters);
object instance = objectFactory.CreateInstance(parameters);
instance = RaisePostCreateEvent(instance);
return instance;
}
return TypeUtils.InstantiatePrimitiveType(type);
}
///
/// Set the value of an object property.
///
/// The object to set the property.
/// The result property to use.
/// The database value to set.
public void SetValueOfProperty(ref object target, ResultProperty property, object dataBaseValue)
{
dataExchange.SetData(ref target, property, dataBaseValue);
}
///
/// Resolve the submap
///
///
///
public IResultMap ResolveSubMap(IDataReader dataReader)
{
IResultMap subMap = this;
if (discriminator != null)
{
ResultProperty mapping = discriminator.ResultProperty;
object dataBaseValue = mapping.GetDataBaseValue(dataReader);
if (dataBaseValue != null)
{
subMap = discriminator.GetSubMap(dataBaseValue.ToString());
if (subMap == null)
{
subMap = this;
}
else if (subMap != this)
{
subMap = subMap.ResolveSubMap(dataReader);
}
}
else
{
subMap = nullResultMap;
}
}
return subMap;
}
///
/// The Key properties names (used for resolved circular reference).
///
///
public List KeyPropertyNames
{
get { return keyPropertyNames; }
}
///
/// The Keys Properties.
///
///
public ResultPropertyCollection KeysProperties
{
get { return keysProperties; }
}
#endregion
///
/// Initializes the key Column properties.
///
private void InitializeKeysProperties()
{
for (int i = 0; i < keyPropertyNames.Count; i++)
{
ResultProperty resultProperty = properties.FindByPropertyName(keyPropertyNames[i]);
if (resultProperty == null)
{
resultProperty = parameters.FindByPropertyName(keyPropertyNames[i]);
}
KeysProperties.Add(resultProperty);
}
}
///
/// Checks the group by.
///
private void CheckGroupBy()
{
try
{
// Verify that that each groupBy element correspond to a class member
// of one of result property
for (int i = 0; i < groupByProperties.Count; i++)
{
string memberName = GroupByPropertyNames[i];
if (!properties.Contains(memberName))
{
throw new ConfigurationException(
string.Format(
"Could not configure ResultMap named \"{0}\". Check the groupBy attribute. Cause: there's no result property named \"{1}\".",
id, memberName));
}
}
}
catch (Exception e)
{
throw new ConfigurationException(
string.Format("Could not configure ResultMap named \"{0}\", Cause: {1}", id, e.Message)
, e);
}
}
///
/// Initializes the groupBy properties.
///
private void InitializeGroupByProperties()
{
for (int i = 0; i < GroupByPropertyNames.Count; i++)
{
ResultProperty resultProperty = Properties.FindByPropertyName(GroupByPropertyNames[i]);
GroupByProperties.Add(resultProperty);
}
}
///
/// Checks the key Column.
///
private void CheckKeysProperties()
{
try
{
// Verify that that each key column element correspond to a class member
// of one of result property
for (int i = 0; i < keyPropertyNames.Count; i++)
{
string memberName = keyPropertyNames[i];
if (!properties.Contains(memberName))
{
if (!parameters.Contains(memberName))
{
throw new ConfigurationException(
string.Format(
"Could not configure ResultMap named \"{0}\". Check the keyPropertyNames attribute. Cause: there's no result property or parameter constructor named \"{1}\".",
id, memberName));
}
}
}
}
catch (Exception e)
{
throw new ConfigurationException(
string.Format("Could not configure ResultMap named \"{0}\", Cause: {1}", id, e.Message)
, e);
}
}
}
}