public class AccountSearchBuilder : BaseAccountSearchBuilder, ISqlMapper
{
#region Column Mappings
// column mappings for the business object
#endregion
#region Base Class Implementations
/// <summary>
/// Construct the hashtable containing the sortable properties for the
/// business object, mapping it to the column name corresponding to
/// the property.
/// </summary>
override protected void constructSortPropertyMap()
{
//use the same property map as provided by the AccountBuilder class
using (AccountBuilder accountBuilder = new AccountBuilder())
{
_sortPropertyMap = accountBuilder.SortPropertyMap;
}
}
#endregion
#region Constructor
/// <summary>
/// Default constructor.
/// </summary>
public AccountSearchBuilder()
: base()
{
}
#endregion
#region ISqlMapper Members
/// <summary>
/// Maps the data in the passed row to the object.
/// </summary>
/// <param name="svObject">
/// The object whose data is being written.</param>
/// <param name="objectRow">
/// The data row containing the object's data to be mapped.</param>
/// <returns>
/// Populated business object with data from the passed row.</returns>
new public BaseSVObject WriteObjectProperties(BaseSVObject svObject, DataRow objectRow)
{
//call base method to populate its common properties and return the object
AccountSearch svAccountSearch = (AccountSearch)base.WriteObjectProperties(svObject, objectRow);
//write consumer-specific properties
//return the populated object
return svAccountSearch;
}
/// <summary>
/// Maps the data in the passed table to a collection of objects.
/// </summary>
/// <param name="objectTable">
/// The data table containing the collection of objects to be mapped.</param>
/// <returns>
/// Populated business object list with data from the passed table.</returns>
new public SVList<BaseSVObject> WriteObjectProperties(DataTable objectTable)
{
//create the list object
SVList<BaseSVObject> objectList = new SVList<BaseSVObject>();
//iterate through each row in the table
foreach (DataRow objectRow in objectTable.Rows)
{
//create new object
AccountSearch svAccountSearch = new AccountSearch();
//call overload to write its properties
svAccountSearch = (AccountSearch)this.WriteObjectProperties(svAccountSearch, objectRow);
//add the completed object to the list
objectList.Add(svAccountSearch);
}
//return the populated list
return objectList;
}
#endregion
#region Read Methods
/// <summary>
/// Reads a Account Search record by its ID.
/// </summary>
/// <param name="accountSearchID">
/// ID of the Account Search record to be returned.</param>
/// <returns>
/// Populated Account Search record.</returns>
public AccountSearch Build(int accountSearchID)
{
//initialize return object
AccountSearch accountSearch = new AccountSearch();
//get the command from the base
SqlCommand readCommand = base.commandRead(accountSearchID, Enumerations.SearchTypeValue.AccountSearch);
//call method on database controller to get the dataset
DataSet dsSearch = base._sqlController.ExecuteReadCommand(readCommand);
if (dsSearch != null &&
dsSearch.Tables.Count > 0 &&
dsSearch.Tables[0].Rows.Count > 0)
{
//call method to assemble the object
accountSearch = (AccountSearch)this.WriteObjectProperties(accountSearch, dsSearch.Tables[0].Rows[0]);
}
else
{
//set object to null as it could not be found
accountSearch = null;
}
//return completed object
return accountSearch;
}
/// <summary>
/// Reads a collection of Account Search records for an entity.
/// </summary>
/// <param name="entityID">
/// The ID of the Entity for which all Account Search records
/// will be returned.</param>
/// <param name="entityType">
/// The Type of Entity for which all Account Search records
/// will be returned.</param>
/// <returns>
/// Collection of Account Search objects for the Entity.</returns>
public SVList<BaseSVObject> BuildForEntity(int entityID, Enumerations.EntityTypeValue entityType)
{
//initialize the return list
SVList<BaseSVObject> searchList = null;
//get the command from the base
SqlCommand readCommand = base.commandRead(entityID, (int)entityType, Enumerations.SearchTypeValue.AccountSearch);
//call method on database controller to get the dataset for the object
DataSet dsSearch = base._sqlController.ExecuteReadCommand(readCommand);
if (dsSearch != null &&
dsSearch.Tables.Count > 0 &&
dsSearch.Tables[0].Rows.Count > 0)
{
//call method to assemble the collection objects
searchList = this.WriteObjectProperties(dsSearch.Tables[0]);
}
else
{
//if the dataset is null - construct an empty collection
searchList = new SVList<BaseSVObject>();
}
//return the list
return searchList;
}
/// <summary>
/// This method will perform a search for Accounts based on the criteria passed
/// in the custom search object.
/// </summary>
/// <param name="accountSearch">
/// The search options.</param>
/// <returns>
/// The custom search object with its SearchResults property populated.</returns>
public AccountSearch PerformSearch(AccountSearch accountSearch)
{
//initialize returned results from search
SVList<BaseSVObject> searchResults = null;
//get command to execute
SqlCommand searchCommand = base.commandSearch(accountSearch);
//execute the command
DataSet dsResults = base._sqlController.ExecuteReadCommand(searchCommand);
if (dsResults != null &&
dsResults.Tables.Count > 0 &&
dsResults.Tables[0].Rows.Count > 0)
{
//instaniate the Meeting builder class
using (AccountBuilder accountBuilder = new AccountBuilder())
{
//call its method to build the resultset
searchResults = accountBuilder.WriteObjectProperties(dsResults.Tables[0]);
}
}
else
{
//empty list
searchResults = new SVList<BaseSVObject>();
}
//keep the updated pagination object
searchResults.PaginationSettings = base.PaginationSettings;
//add the converted list to the search results of the object
accountSearch.SearchResults = searchResults.ConvertAll(new Converter<BaseSVObject, Account>(Account.Convert));
//return the populated search object
return accountSearch;
}
#endregion
#region Write Methods
/// <summary>
/// Writes the object to the database.
/// </summary>
/// <param name="accountSearch">
/// Account Search object to be written to the database.</param>
/// <returns>
/// Updated object after writing to the database.</returns>
public AccountSearch WriteRecordToDatabase(AccountSearch accountSearch)
{
//get the appropriate command
bool isCreateCommand = false;
SqlCommand writeCommand = null;
if (accountSearch.ID <= 0)
{
//get the insert command
writeCommand = base.commandInsert(accountSearch, constructSearchMemberList(accountSearch));
//add the search class-specific parameters
writeCommand.Parameters.AddRange(constructWriteParameters(accountSearch));
isCreateCommand = true;
//execute the insert command
base._sqlController.ExecuteInsertCommand(writeCommand);
}
else
{
//get the update command
writeCommand = base.commandUpdate(accountSearch, constructSearchMemberList(accountSearch));
//add the search class-specific parameters
writeCommand.Parameters.AddRange(constructWriteParameters(accountSearch));
//execute the update command
base._sqlController.ExecuteUpdateCommand(writeCommand);
}
int recordID = 0;
if (isCreateCommand)
{
//get the returned ID from the output parameter
object outputIDValue = getParameterValue(PARAM_ID, writeCommand.Parameters);
if (outputIDValue == null)
{
//TODO: throw exception as the ID could not be found
throw new ApplicationException("The output ID parameter could not be found.");
}
//conver the value to an integer
recordID = (int)outputIDValue;
accountSearch.ID = recordID; //put the ID onto the record
}
//return the updated object
return accountSearch;
}
/// <summary>
/// Deletes the Account Search record from the database.
/// </summary>
/// <param name="accountSearchID">
/// The ID of the Account Search record to be deleted.</param>
public void DeleteSearch(int accountSearchID)
{
//get the command from the base class
SqlCommand deleteCommand = base.commandDelete(accountSearchID);
//execute the command
_sqlController.ExecuteDeleteCommand(deleteCommand);
}
#endregion
#region Search Property Methods
/// <summary>
/// This method examines the Meeting Search object and constructs
/// the list of member names that are relevant to the
/// criteria of the search.
/// </summary>
/// <param name="meetingSearch">
/// The object containing the search criteria.</param>
/// <returns>
/// List of member names to be used in the search.</returns>
protected override List<string> constructSearchMemberList(BaseSVSearch svSearch)
{
//validate and cast the object
BaseSVObject.ValidateObjectType(svSearch, typeof(AccountSearch));
AccountSearch accountSearch = (AccountSearch)svSearch;
//initialize list
List<string> memberList = new List<string>();
//add the base meeting search members
memberList.AddRange(base.constructSearchMemberList(accountSearch));
//add consumer-specific members (if needed)
//return completed list
return memberList;
}
#endregion
#region Parameter Methods
/// <summary>
/// This method constructs the search class-specific parameters, unique to the search
/// object being written.
/// </summary>
/// <param name="svSearch">
/// The search object for which parameters are to be constructed.</param>
/// <returns>
/// Array of parameter objects to be included on a search object write (insert and update) command.</returns>
override protected SqlParameter[] constructWriteParameters(BaseSVSearch svSearch)
{
//get base search class parameters
ArrayList writeParameters = new ArrayList();
writeParameters.AddRange(base.constructWriteParameters(svSearch));
//add any consumer-specific parameters
//return the collection of parameters
SqlParameter[] parameterArray = (SqlParameter[])writeParameters.ToArray(typeof(SqlParameter));
return parameterArray;
}
#endregion
}