public class AccountSearchBuilder : BaseAccountSearchBuilder, ISqlMapper { #region Column Mappings // column mappings for the business object #endregion #region Base Class Implementations /// /// Construct the hashtable containing the sortable properties for the /// business object, mapping it to the column name corresponding to /// the property. /// 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 /// /// Default constructor. /// public AccountSearchBuilder() : base() { } #endregion #region ISqlMapper Members /// /// Maps the data in the passed row to the object. /// /// /// The object whose data is being written. /// /// The data row containing the object's data to be mapped. /// /// Populated business object with data from the passed row. 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; } /// /// Maps the data in the passed table to a collection of objects. /// /// /// The data table containing the collection of objects to be mapped. /// /// Populated business object list with data from the passed table. new public SVList WriteObjectProperties(DataTable objectTable) { //create the list object SVList objectList = new SVList(); //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 /// /// Reads a Account Search record by its ID. /// /// /// ID of the Account Search record to be returned. /// /// Populated Account Search record. 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; } /// /// Reads a collection of Account Search records for an entity. /// /// /// The ID of the Entity for which all Account Search records /// will be returned. /// /// The Type of Entity for which all Account Search records /// will be returned. /// /// Collection of Account Search objects for the Entity. public SVList BuildForEntity(int entityID, Enumerations.EntityTypeValue entityType) { //initialize the return list SVList 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(); } //return the list return searchList; } /// /// This method will perform a search for Accounts based on the criteria passed /// in the custom search object. /// /// /// The search options. /// /// The custom search object with its SearchResults property populated. public AccountSearch PerformSearch(AccountSearch accountSearch) { //initialize returned results from search SVList 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(); } //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(Account.Convert)); //return the populated search object return accountSearch; } #endregion #region Write Methods /// /// Writes the object to the database. /// /// /// Account Search object to be written to the database. /// /// Updated object after writing to the database. 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; } /// /// Deletes the Account Search record from the database. /// /// /// The ID of the Account Search record to be deleted. 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 /// /// This method examines the Meeting Search object and constructs /// the list of member names that are relevant to the /// criteria of the search. /// /// /// The object containing the search criteria. /// /// List of member names to be used in the search. protected override List constructSearchMemberList(BaseSVSearch svSearch) { //validate and cast the object BaseSVObject.ValidateObjectType(svSearch, typeof(AccountSearch)); AccountSearch accountSearch = (AccountSearch)svSearch; //initialize list List memberList = new List(); //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 /// /// This method constructs the search class-specific parameters, unique to the search /// object being written. /// /// /// The search object for which parameters are to be constructed. /// /// Array of parameter objects to be included on a search object write (insert and update) command. 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 }