Thursday, November 22, 2007

Addresses

using System;using System.Collections.Generic;using System.Text;using Csla.Data;using BAF.Genesis.Biz;using Microsoft.Practices.EnterpriseLibrary.Data;using System.Data;using BAF.Genesis.Common.Biz;using BAF.Caching;
namespace BAF.Genesis.GenericEntities.Biz{ [Serializable()] public class Addresses : GenericEntityListBase { #region Constructors private Addresses(int parentID, int relationType) : base(parentID, relationType) { } #endregion Constructors
#region Business Properties and Methods public Address GetAddressByID(int addressID) { foreach (Address add in this) { if (add.ID == addressID) return add; } return null; } public Address CreateNewAddress() { return Address.NewAddress(_parentID, _relationTypeID, this); }
public Address CreateNewAddress(Address address) { return Address.NewCloneAddress(address, this); }
public new Address AddNew() { Address a = CreateNewAddress(); this.Add(a); return a; }
public Address AddNew(Address address) { Address a = CreateNewAddress(address); this.Add(a); return a; }
public void Remove(int addressID) { Address a = this.GetAddressByID(addressID); this.Remove(a); } #endregion Business Properties and Methods
#region Static Methods
internal static Addresses NewAddresses(int parentID, int relationType) { return new Addresses(parentID, relationType); }
internal static Addresses NewAddresses(int parentID, DBAddressRelationType relationType) { return NewAddresses(parentID, (int)relationType); } #endregion Static Methods
#region Data Access
internal static Addresses GetAddresses(int parentID, int relationType) { Addresses a = new Addresses(parentID, relationType); a.Fetch(); return a; }
internal static Addresses GetAddresses(SafeDataReader dr, int parentID, int relationType) { Addresses a = new Addresses(parentID, relationType); a.Fetch(dr); return a; }
internal static Addresses GetAddresses(int parentID, DBAddressRelationType relationType) { return GetAddresses(parentID, (int)relationType); }
internal static Addresses GetAddresses(SafeDataReader dr, int parentID, DBAddressRelationType relationType) { return GetAddresses(dr, parentID, (int)relationType); }
private void Fetch(SafeDataReader dr) { while (dr.Read()) { this.Add(Address.GetAddress(dr, _parentID, _relationTypeID)); } }
private void Fetch() { try { Database db = DatabaseFactory.CreateDatabase("GenesisDB"); string spName = "ADDRESSP.GETADDRESSTLIST"; System.Data.Common.DbCommand dbCommand = db.GetStoredProcCommand(spName);
db.AddInParameter(dbCommand, "pnParentID", DbType.Int32, _parentID); db.AddInParameter(dbCommand, "pnRelationTypeID", DbType.Int32, _relationTypeID); db.AddInParameter(dbCommand, "pcUserName", DbType.String, UserName); dbCommand.Parameters.Add(new System.Data.OracleClient.OracleParameter("cur_OUT", System.Data.OracleClient.OracleType.Cursor, 2000, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Default, Convert.DBNull));
using (SafeDataReader dr = new SafeDataReader(db.ExecuteReader(dbCommand))) // Automatically closes the DataReader { while(dr.Read()) { this.Add(Address.GetAddress(dr, _parentID, _relationTypeID)); } } } catch (Exception ex) { throw ex; } }
internal void Update(T parent) where T : BAFBusinessBase { if (parent.IsDeleted) {//Delete all Addresses and their relations DeleteAddresses(_parentID, _relationTypeID); return; }
//Delete objects in the deleted list foreach (Address aDeleted in DeletedList) aDeleted.DeleteSelf(); //now that they are deleted, remove them from memory too DeletedList.Clear();
// Update or Insert child objects foreach (Address a in this) { if (a.IsNew) a.Insert(); else a.Update(); } }
internal static void DeleteAddresses(int parentID, int relationType) { Address.DeleteAddress(parentID, relationType); }
internal static void DeleteAddresses(int parentID, DBAddressRelationType relationType) { DeleteAddresses(parentID, (int)relationType); } #endregion Data Access
#region State List
public static State StateList { get { if (!ApplicationCache.Contains("State_StateList")) { State _stateList = State.GetStateList(); ApplicationCache.Add("State_StateList", _stateList); return _stateList; }
return (State)ApplicationCache.GetData("State_StateList"); } } #endregion
#region Country State List
public static State CountryStateList { get { return State.GetStateList(true); } } #endregion
#region Country List
public static Country CountryList { get { if (!ApplicationCache.Contains("Country_CountryList")) { Country _countryList = Country.GetList(new NameValueList.NameValuePair(-1, "[No Data]"), new NameValueList.NameValuePair(-1, "[Select Country]")); ApplicationCache.Add("Country_CountryList", _countryList); return _countryList; } return (Country)ApplicationCache.GetData("Country_CountryList"); } } #endregion
#region Public Methods public new void BeginEdit() { foreach (Address address in this) address.BeginEdit(); }
public new void ApplyEdit() { foreach (Address address in this) address.ApplyEdit(); }
public new void CancelEdit() { foreach (Address address in this) address.CancelEdit(); } #endregion }}

No comments: