1
00:00:00,000 --> 00:00:03,003
- As I was drawing these sequence and clause diagrams

2
00:00:03,003 --> 00:00:06,003
that you saw in previous lesson, I was also coding

3
00:00:06,003 --> 00:00:10,001
which help me make sure that whatever I was thinking

4
00:00:10,001 --> 00:00:12,005
was also in implementable in code.

5
00:00:12,005 --> 00:00:15,006
I will now show you the code for search use case

6
00:00:15,006 --> 00:00:18,003
which starts when the user chooses search option

7
00:00:18,003 --> 00:00:21,006
from the homepage. The homepage is index.jsp

8
00:00:21,006 --> 00:00:24,008
but the link for search is in header.jsp.

9
00:00:24,008 --> 00:00:28,006
Here you can see that we have POC link in line 17

10
00:00:28,006 --> 00:00:32,000
from the previous iteration which we no longer need.

11
00:00:32,000 --> 00:00:35,002
So I have commented it out. In the next line,

12
00:00:35,002 --> 00:00:38,005
you see that search link is pointing to search,

13
00:00:38,005 --> 00:00:41,009
which is actually the SearchController Servlet.

14
00:00:41,009 --> 00:00:45,003
All search-related java classes are now in a package

15
00:00:45,003 --> 00:00:49,001
near search inside com.hplussport.red30.

16
00:00:49,001 --> 00:00:52,002
In SearchController, we have just regular doGet

17
00:00:52,002 --> 00:00:53,009
and doPost methods.

18
00:00:53,009 --> 00:00:57,008
Both for running the request to search in inputs.jsp.

19
00:00:57,008 --> 00:01:03,004
Search Inputs.jsp is in a folder Search, inside jsp folder.

20
00:01:03,004 --> 00:01:06,003
This has one form in line 12,

21
00:01:06,003 --> 00:01:09,006
that has a search box in line 13.

22
00:01:09,006 --> 00:01:12,009
When the user submits a searchString in this box,

23
00:01:12,009 --> 00:01:15,009
the request is forwarded to searchproducts

24
00:01:15,009 --> 00:01:18,003
which is also a Servlet.

25
00:01:18,003 --> 00:01:22,002
In searchproducts, there are several things going on.

26
00:01:22,002 --> 00:01:25,002
First, as you see in line 28 to 30,

27
00:01:25,002 --> 00:01:29,001
it extracts the session and the searchString parameter

28
00:01:29,001 --> 00:01:30,008
from request option.

29
00:01:30,008 --> 00:01:34,005
It then save the searchString as a session attribute.

30
00:01:34,005 --> 00:01:36,009
This makes sure that searchString is available

31
00:01:36,009 --> 00:01:38,008
for rest of the session.

32
00:01:38,008 --> 00:01:43,002
Next, in line 33, it asks DAO to searchProductOnName

33
00:01:43,002 --> 00:01:46,007
using this searchString. This is a new method in DAO.

34
00:01:46,007 --> 00:01:48,008
So let's take a look at that.

35
00:01:48,008 --> 00:01:54,002
In DAO, in line 52 it takes searchString as a parameter,

36
00:01:54,002 --> 00:01:58,003
creates a Product ArrayList in line 53 and then

37
00:01:58,003 --> 00:02:01,009
in a for loop, searches the productMap for products

38
00:02:01,009 --> 00:02:04,002
that have searchString in their names.

39
00:02:04,002 --> 00:02:06,009
As it finds these products, it keeps adding them

40
00:02:06,009 --> 00:02:09,008
to the list in line 56.

41
00:02:09,008 --> 00:02:14,005
And then returns the list back to searchProduct Servlet.

42
00:02:14,005 --> 00:02:17,009
In line 33, we now have the products

43
00:02:17,009 --> 00:02:20,001
that have searchString in their names.

44
00:02:20,001 --> 00:02:23,000
Next, we need to find nutrients in the product

45
00:02:23,000 --> 00:02:25,000
that is at index one.

46
00:02:25,000 --> 00:02:29,007
So in line 38, we invoke another DAO method.

47
00:02:29,007 --> 00:02:32,005
This time, to search nutrients for a product

48
00:02:32,005 --> 00:02:35,001
passing the product's FDC ID.

49
00:02:35,001 --> 00:02:38,006
So let us go back to DAO.

50
00:02:38,006 --> 00:02:43,001
Here in line 64, we have a method searchNutrients

51
00:02:43,001 --> 00:02:45,009
for product that takes an FDC ID.

52
00:02:45,009 --> 00:02:49,009
It first creates an empty list of nutrients in line 65,

53
00:02:49,009 --> 00:02:53,008
fetches the product from the map with the given FDC ID,

54
00:02:53,008 --> 00:02:57,006
and then uses the nutrient IDs in the ProductNutrientMap

55
00:02:57,006 --> 00:03:01,001
to find nutrient objects from nutrientMap.

56
00:03:01,001 --> 00:03:04,003
These nutrients are then added into nutrientsList

57
00:03:04,003 --> 00:03:08,003
one-by-one and then nutrientsList is returned.

58
00:03:08,003 --> 00:03:13,000
Back to searchProducts, the productList, the nutrientsList

59
00:03:13,000 --> 00:03:14,009
and the selectedProduct which is nothing

60
00:03:14,009 --> 00:03:18,004
but the first product, are saved as session attributes

61
00:03:18,004 --> 00:03:21,000
in line 39 to 41.

62
00:03:21,000 --> 00:03:26,000
Finally, the request is forwarded to searchResults.jsp.

63
00:03:26,000 --> 00:03:29,000
In searchResults.jsp,

64
00:03:29,000 --> 00:03:32,008
first in line 17 to 20,

65
00:03:32,008 --> 00:03:35,007
it extracts all session attributes,

66
00:03:35,007 --> 00:03:38,009
and then it creates a counter in line 21

67
00:03:38,009 --> 00:03:42,002
to number the products that are to be displayed.

68
00:03:42,002 --> 00:03:45,001
It then displays the productsList in a table

69
00:03:45,001 --> 00:03:48,001
starting in line 29.

70
00:03:48,001 --> 00:03:51,008
Notice that this entire table is inside a form

71
00:03:51,008 --> 00:03:54,004
declared in line 23.

72
00:03:54,004 --> 00:03:57,008
This form is submitted when the user selects

73
00:03:57,008 --> 00:04:02,002
a radio button defined for each product in line 49.

74
00:04:02,002 --> 00:04:03,008
As this form is submitted,

75
00:04:03,008 --> 00:04:06,008
the control goes to search nutrients Servlet

76
00:04:06,008 --> 00:04:09,000
as you can see in line 52.

77
00:04:09,000 --> 00:04:11,009
But before we go there, let us continue with the initial

78
00:04:11,009 --> 00:04:14,007
scenario where the selectedProduct by default

79
00:04:14,007 --> 00:04:17,000
is the first product in the list.

80
00:04:17,000 --> 00:04:20,007
In that case, the nutrient table starts in line 64

81
00:04:20,007 --> 00:04:24,005
which displays nutrient name, quantity per 100 units

82
00:04:24,005 --> 00:04:28,003
of product and unit of measure for nutrient.

83
00:04:28,003 --> 00:04:31,003
Finally, if user selects a different product,

84
00:04:31,003 --> 00:04:35,001
the control goes to searchNutrient.

85
00:04:35,001 --> 00:04:40,006
In searchNutrient, here in line 30, the Servlet gets fdcId

86
00:04:40,006 --> 00:04:43,009
parameter coming from radio button selection.

87
00:04:43,009 --> 00:04:48,002
Using this fdcId, it uses DAO's productMap

88
00:04:48,002 --> 00:04:51,007
to get the product object in line 33...

89
00:04:51,007 --> 00:04:54,002
And it search nutrients for product method

90
00:04:54,002 --> 00:04:57,007
to get it's nutrient in line 35.

91
00:04:57,007 --> 00:05:00,003
Both these are saved as session attributes

92
00:05:00,003 --> 00:05:03,002
in line 37 and 38.

93
00:05:03,002 --> 00:05:07,002
The request is sent back to searchResults.jsp

94
00:05:07,002 --> 00:05:10,003
which refreshes the nutrients table on the webpage.

95
00:05:10,003 --> 00:05:13,006
Now we are all set to run our application.

96
00:05:13,006 --> 00:05:18,004
For that, I go to Run As. Run on Server.

97
00:05:18,004 --> 00:05:22,005
And you can see that is beginning to load all the data.

98
00:05:22,005 --> 00:05:25,009
The data loading is now completed

99
00:05:25,009 --> 00:05:28,005
and the application is up and running.

100
00:05:28,005 --> 00:05:32,000
Here, if I say search...

101
00:05:32,000 --> 00:05:36,002
Let's say I do "apple pie". I can see the results here.

102
00:05:36,002 --> 00:05:38,006
I can also run this application from outside,

103
00:05:38,006 --> 00:05:44,009
so let me go to browser, localhostRed30.

104
00:05:44,009 --> 00:05:48,005
Search products, let's say "apple pie" one more time

105
00:05:48,005 --> 00:05:50,007
and you can see the results.

106
00:05:50,007 --> 00:05:54,003
If I change the product selection from radio buttons,

107
00:05:54,003 --> 00:05:58,004
you can see that the set of nutrients in the table below

108
00:05:58,004 --> 00:06:00,001
are also changing.

109
00:06:00,001 --> 00:06:03,007
At this time, I do want to say that I am not a UI designer.

110
00:06:03,007 --> 00:06:05,004
As you can probably tell.

111
00:06:05,004 --> 00:06:08,004
This UI is just bare bones to get the functionality

112
00:06:08,004 --> 00:06:11,005
up and running. This would be a good time to involve

113
00:06:11,005 --> 00:06:15,004
your UI design expert if you have one in your team.

114
00:06:15,004 --> 00:06:19,000
I don't have any, so we will have to live with this beauty.

