Business Logic Framework - Tutorial 15:
This tutorial shows you how to use the Business Objects's capability to build and run complex search criteria. This tutorial shows a class called SearchGroup which extends BaseField and can be used to build search groups " (FirstName LIKE 'j%' AND LastName LIKE '%h') OR (FirstName LIKE '%i%' AND LastName LIKE '%e%') ". The process is simple, and you simply need to create a new instance of the SearchGroup. Once a SearchGroup instances is added to the Fields collection of the Business Object the rest of the fields are ignored, and only the SearchGroups are used. The related objects's Fields collections are ignored as well. To include a related object's field in the search criteria the field can be simply added to the SearchGroup's Fields collection.
| // Purpose of this sample/tutorial is to demonstrate the ability to run searches with search criteria where fields are grouped together to handle complex scenarios. | |
| // The target here is to search for Customers whose first name starts with 'j' and last name ends with 'h' or their first name contains 'i' and last name contains 'e' | |
| // SELECT CustomerID, FirstName, LastName FROM Customers | |
| // WHERE (FirstName LIKE 'j%' AND LastName LIKE '%h') OR (FirstName LIKE '%i%' AND LastName LIKE '%e%') | |
| using (Customers customers = new Customers()) | |
| { | |
| customers.ObjectMode = Akal.QuickObjects.ObjectBase.ObjectModes.Search; | |
| // If PartialTextMatch property is set to true the business object uses all string field's values for partial text matches. However, this can be overridden | |
| // at each field individually and also the PartialTextMode property can be used to specify weather to look for values that start/end with or contain the specified value. | |
| customers.PartialTextMatch = true; | |
| customers.FirstName.Value = "j"; | |
| // Setting the PartialTextMode to "StartsWith" will cause the customers object to search for FirstName value that starts with "j" | |
| customers.FirstName.PartialTextMode = PartialTextModes.StartsWith; | |
| customers.LastName.Value = "h"; | |
| // Setting the PartialTextMode to "EndsWith" will cause the customers object to search for LastName value that ends with "h" | |
| customers.LastName.PartialTextMode = PartialTextModes.EndsWith; | |
| // SearchGroup is a Powerful class that can be used to create complex search criteria where multiple fields can be grouped together and each group seperated by AND/OR (SearchCondition property). | |
| // Once one or more SearchGroup instances are constructed as shown in the following line, the search criteria will exclude all fields except the ones that are added to the "Fields" collection of the SearchGroup | |
| // Each SearchGroup must be given a unique name. | |
| SearchGroup sg1 = new SearchGroup(customers, "sg1"); | |
| // Set the SearchCondition to " OR ". Since this is the first SearchGroup being added the SearchCondition will actually be ignored but it is shown here just to demonstrate the capability. | |
| sg1.SearchCondition = " OR "; | |
| // Add the FirstName and LastName fields to the SearchGroup. The field thats added first its SearchCondition is ignored since the generated where clause will look like this | |
| // WHERE ( FirstName LIKE :FirstNameParam AND LastName LIKE LastNameParam ) | |
| sg1.Fields.Add(customers.FirstName); | |
| sg1.Fields.Add(customers.LastName); | |
| // Lets create another SearchGroup and this time it displays an alternate means of specified a "GroupName". | |
| // GroupName must be unique for each group and can be any string of your choice. | |
| SearchGroup sg2 = new SearchGroup(customers); | |
| // You can set the GroupName property or you can alternatively set the sg2.Name.Value = "sg2" and they both have the same affect. | |
| sg2.GroupName = "sg2"; | |
| // Set the SearchCondition to " OR ". | |
| sg2.SearchCondition = " OR "; | |
| // Since we have already used the FirstName field lets create a new StringField that will hold the search criteria for "FirstName" to be included in the second search group. | |
| // customers object instance already has a field for "FirstName" so when add a second field for "FirstName" we must give it an alias (i.e. set the ResultSetName property). | |
| // The StringField constructor used below takes three arguments 1. BaseBusinessObject instance 2. A String that is the field's name 3. A String that will be used as the field's alias (ResultSetName) | |
| StringField firstName = new StringField(customers, "FirstName", "FN_ALIAS"); | |
| // By setting this instance's Visible property to false we make sure that a duplicate "FirstName" column will not appear in the result set. | |
| firstName.Visible = false; | |
| // By setting the PartialTextMatch to "true" this field's search criteria will automatically | |
| firstName.PartialTextMatch = true; | |
| // By Setting the PartialTextMode to "PartialTextModes.Contains" the search criteria that will look for FirstName that contains the Value (i.e. 'i') | |
| firstName.PartialTextMode = PartialTextModes.Contains; | |
| firstName.Value = "i"; | |
| // Since we have already used the LastName field lets create a new StringField that will hold the search criteria for "LastName" to be included in the second search group. | |
| // customers object instance already has a field for "LastName" so when add a second field for "LastName" we must give it an alias (i.e. set the ResultSetName property). | |
| // The following lines show an alternate way to achieve the same that we did in one line for firstName field instance. | |
| StringField lastName = new StringField(customers); | |
| // Name property must be given a field name that belong to the underlying table. | |
| lastName.Name.Value = "LastName"; | |
| // An alias must be created since LastName field already exists in the customers object | |
| lastName.ResultSetName.Value = "LN_ALIAS"; | |
| // the following lines follow the same reasoning as the firstName field instance. | |
| lastName.Visible = false; | |
| lastName.PartialTextMatch = true; | |
| lastName.PartialTextMode = PartialTextModes.Contains; | |
| lastName.Value = "e"; | |
| // Lets add the firstName and lastName instances into the sg2 SearchGroup | |
| sg2.Fields.Add(firstName); | |
| sg2.Fields.Add(lastName); | |
| // Calling the Find method will create and run the desired sql statement. | |
| customers.Find(); | |
| // AffectedRecords property can be used to determine how many records were returned by the business object. | |
| if (customers.AffectedRecords > 0) | |
| { | |
| this.GridView1.DataSource = customers.ResultSet; | |
| this.GridView1.DataMember = customers.GetResultSetName(); | |
| } | |
| } | |
| 'Purpose of this sample/tutorial is to demonstrate the ability to run searches with search criteria where fields are grouped together to handle complex scenarios. | |
| 'The target here is to search for Customers whose first name starts with 'j' and last name ends with 'h' or their first name contains 'i' and last name contains 'e' | |
| 'SELECT CustomerID, FirstName, LastName FROM Customers | |
| 'WHERE (FirstName LIKE 'j%' AND LastName LIKE '%h') OR (FirstName LIKE '%i%' AND LastName LIKE '%e%') | |
| Using customers As Customers = New Customers() | |
| customers.ObjectMode = Akal.QuickObjects.ObjectBase.ObjectModes.Search | |
| 'If PartialTextMatch property is set to true the business object uses all string field's values for partial text matches. However, this can be overridden | |
| 'at each field individually and also the PartialTextMode property can be used to specify weather to look for values that start/end with or contain the specified value. | |
| customers.PartialTextMatch = True | |
| customers.FirstName.Value = "j" | |
| 'Setting the PartialTextMode to "StartsWith" will cause the customers object to search for FirstName value that starts with "j" | |
| customers.FirstName.PartialTextMode = PartialTextModes.StartsWith | |
| customers.LastName.Value = "h" | |
| 'Setting the PartialTextMode to "EndsWith" will cause the customers object to search for LastName value that ends with "h" | |
| customers.LastName.PartialTextMode = PartialTextModes.EndsWith | |
| 'SearchGroup is a Powerful class that can be used to create complex search criteria where multiple fields can be grouped together and each group seperated by AND/OR (SearchCondition property). | |
| 'Once one or more SearchGroup instances are constructed as shown in the following line, the search criteria will exclude all fields except the ones that are added to the "Fields" collection of the SearchGroup | |
| 'Each SearchGroup must be given a unique name. | |
| Dim sg1 = New SearchGroup(customers, "sg1") | |
| 'Set the SearchCondition to " OR ". Since this is the first SearchGroup being added the SearchCondition will actually be ignored but it is shown here just to demonstrate the capability. | |
| sg1.SearchCondition = " OR " | |
| 'Add the FirstName and LastName fields to the SearchGroup. The field thats added first its SearchCondition is ignored since the generated where clause will look like this | |
| 'WHERE ( FirstName LIKE :FirstNameParam AND LastName LIKE LastNameParam ) | |
| sg1.Fields.Add(customers.FirstName) | |
| sg1.Fields.Add(customers.LastName) | |
| 'Lets create another SearchGroup and this time it displays an alternate means of specified a "GroupName". | |
| 'GroupName must be unique for each group and can be any string of your choice. | |
| Dim sg2 = New SearchGroup(customers) | |
| 'You can set the GroupName property or you can alternatively set the sg2.Name.Value = "sg2" and they both have the same affect. | |
| sg2.GroupName = "sg2" | |
| 'Set the SearchCondition to " OR ". | |
| sg2.SearchCondition = " OR " | |
| 'Since we have already used the FirstName field lets create a new StringField that will hold the search criteria for "FirstName" to be included in the second search group. | |
| 'customers object instance already has a field for "FirstName" so when add a second field for "FirstName" we must give it an alias (i.e. set the ResultSetName property). | |
| 'The StringField constructor used below takes three arguments 1. BaseBusinessObject instance 2. A String that is the field's name 3. A String that will be used as the field's alias (ResultSetName) | |
| Dim firstName As StringField = New StringField(customers, "FirstName", "FN_ALIAS") | |
| 'By setting this instance's Visible property to false we make sure that a duplicate "FirstName" column will not appear in the result set. | |
| firstName.Visible = False | |
| 'By setting the PartialTextMatch to "true" this field's search criteria will automatically | |
| firstName.PartialTextMatch = True | |
| 'By Setting the PartialTextMode to "PartialTextModes.Contains" the search criteria that will look for FirstName that contains the Value (i.e. 'i') | |
| firstName.PartialTextMode = PartialTextModes.Contains | |
| firstName.Value = "i" | |
| 'Since we have already used the LastName field lets create a new StringField that will hold the search criteria for "LastName" to be included in the second search group. | |
| 'customers object instance already has a field for "LastName" so when add a second field for "LastName" we must give it an alias (i.e. set the ResultSetName property). | |
| 'The following lines show an alternate way to acheive the same that we did in one line for firstName field instance. | |
| Dim lastName As StringField = New StringField(customers) | |
| 'Name property must be given a field name that belong to the underlying table. | |
| lastName.Name.Value = "LastName" | |
| 'An alias must be created since LastName field already exists in the customers object | |
| lastName.ResultSetName.Value = "LN_ALIAS" | |
| 'the following lines follow the same reasoning as the firstName field instance. | |
| lastName.Visible = False | |
| lastName.PartialTextMatch = True | |
| lastName.PartialTextMode = PartialTextModes.Contains | |
| lastName.Value = "e" | |
| 'Lets add the firstName and lastName instances into the sg2 SearchGroup | |
| sg2.Fields.Add(firstName) | |
| sg2.Fields.Add(lastName) | |
| 'Calling the Find method will create and run the desired sql statement. | |
| customers.Find() | |
| 'AffectedRecords property can be used to determine how many records were returned by the business object. | |
| If customers.AffectedRecords > 0 Then | |
| Me.GridView1.DataSource = customers.ResultSet | |
| Me.GridView1.DataMember = customers.GetResultSetName() | |
| End If | |
| End Using | |
