// 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.Collections;
using Persist.Criteria;
using Persist.Statements;
namespace Persist.Maps{
/// <summary>
/// This class contains information about an association map.
/// </summary>
public class UniDirectionalAssociationMap
{
/// <summary>
/// Cardinality of the Association map
/// </summary>
public enum CardinalityEnum
{
/// <summary>
/// 1..1 Assocation
/// </summary>
ONE_TO_ONE,
/// <summary>
/// 1..* Association
/// </summary>
ONE_TO_MANY
};
private CardinalityEnum cardinality = CardinalityEnum.ONE_TO_ONE;
private bool saveAutomatic = false;
private bool deleteAutomatic = false;
private bool retrieveAutomatic = false;
private ClassMap forClass = null;
private AttributeMap target = null;
private ArrayList entries = new ArrayList();
private bool inverse = false;
private string targetName = null;
/// <summary>
/// Creates UniDirectionalAssociationMap.
/// </summary>
public UniDirectionalAssociationMap()
{
}
/// <summary>
/// Adds new entry to this association map.
/// </summary>
/// <param name="entry">the Entry</param>
public void AddEntry(UDAMapEntry entry)
{
entries.Add(entry);
}
/// <summary>
/// Cardinality of the association.
/// </summary>
public CardinalityEnum Cardinality
{
get
{
return cardinality;
}
set
{
cardinality = value;
}
}
/// <summary>
/// Returns entry with the given index.
/// </summary>
/// <param name="index">Entry Index</param>
/// <returns>the Entry</returns>
public UDAMapEntry GetEntry(int index)
{
return (UDAMapEntry)entries[index];
}
/// <summary>
/// The Desitnation ClassMap of the Assocation
/// </summary>
public ClassMap ForClass
{
get
{
return forClass;
}
set
{
forClass = value;
}
}
/// <summary>
/// Returns number of entries.
/// </summary>
public int EntriesCount
{
get
{
return entries.Count;
}
}
/// <summary>
/// the Target Attribute of the association
/// </summary>
public AttributeMap Target
{
get
{
return target;
}
set
{
target = value;
}
}
/// <summary>
/// Returns the name of the target for this attribute map.
/// </summary>
public string TargetName
{
get
{
return targetName;
}
set
{
targetName = value;
}
}
/// <summary>
/// Returns true if this association is inversed.
/// </summary>
public bool IsInverse
{
get
{
return inverse;
}
set
{
inverse = value;
}
}
/// <summary>
/// Specified if the childs must be deleted when the parent is deleted
/// </summary>
public bool IsDeleteAutomatic
{
get
{
return deleteAutomatic;
}
set
{
deleteAutomatic = value;
}
}
/// <summary>
/// Specified if the child(s) must be retreived automatically when the parent is retrieved
/// </summary>
public bool IsRetrieveAutomatic
{
get
{
return retrieveAutomatic;
}
set
{
retrieveAutomatic = value;
}
}
/// <summary>
/// Specified if the child(s) must be saved automatically when the parent is saved
/// </summary>
public bool IsSaveAutomatic
{
get
{
return saveAutomatic;
}
set
{
saveAutomatic = value;
}
}
/// <summary>
/// Return the Criteria of this Association
/// </summary>
/// <param name="orderAttrs">Sort order of the retrieve</param>
/// <returns>the Criteria</returns>
// internal RetrieveCriteria GetCriteria(Order[] orderAttrs)
// {
// RetrieveCriteria criteria = new RetrieveCriteria(forClass);
// if(isInverse)
// {
// for(int i = 0; i < entries.Count; i++)
// {
// criteria.WhereCondition.AddAndCriteria(criteria.GetEqualToCriteria(GetEntry(i).From.Name));
// }
// }
// else
// {
// for(int i = 0; i < entries.Count; i++)
// {
// criteria.WhereCondition.AddAndCriteria(criteria.GetEqualToCriteria(GetEntry(i).To.Name));
// }
// }
//
// if(orderAttrs != null)
// {
// for(int i = 0; i < orderAttrs.Length; i++)
// {
// criteria.AddOrderAttribute(orderAttrs[i].Name,orderAttrs[i].Ascend);
// }
// }
// return criteria;
// }
internal RetrieveStatement GetStatement(Order[] orderAttrs)
{
RetrieveStatement criteriaStatement = new RetrieveStatement(forClass);
if(IsInverse)
{
for(int i = 0; i < entries.Count; i++)
{
criteriaStatement.WhereCondition.AddAndCriteria(criteriaStatement.GetEqualToCriteria(GetEntry(i).From.Name));
}
}
else
{
for(int i = 0; i < entries.Count; i++)
{
criteriaStatement.WhereCondition.AddAndCriteria(criteriaStatement.GetEqualToCriteria(GetEntry(i).To.Name));
}
}
if(orderAttrs != null)
{
for(int i = 0; i < orderAttrs.Length; i++)
{
criteriaStatement.AddOrderAttribute(orderAttrs[i].Name,orderAttrs[i].Ascend);
}
}
return criteriaStatement;
}
/// <summary>
/// Return a parameter collection for the association criteria
/// </summary>
/// <param name="obj">the PersistentObject where the value have to be readed</param>
/// <returns></returns>
internal ICollection GetStatementParameters(PersistentObject obj)
{
ArrayList statementParameters = new ArrayList();
if(IsInverse)
{
for(int i = 0; i < entries.Count; i++)
{
statementParameters.Add(GetEntry(i).To.GetValue(obj));
}
}
else
{
for(int i = 0; i < entries.Count; i++)
{
statementParameters.Add(GetEntry(i).From.GetValue(obj));
}
}
return statementParameters;
}
}
}
|