1
00:00:00,005 --> 00:00:02,001
- [Instructor] This is the UI mockup

2
00:00:02,001 --> 00:00:03,006
that we looked at earlier

3
00:00:03,006 --> 00:00:06,003
when we discussed requirement validation.

4
00:00:06,003 --> 00:00:10,000
In this mockup when we choose search from homepage,

5
00:00:10,000 --> 00:00:12,008
it takes us to a Search page

6
00:00:12,008 --> 00:00:16,002
where we enter some string based on which

7
00:00:16,002 --> 00:00:19,003
it pulls the products and then nutrients.

8
00:00:19,003 --> 00:00:22,004
When I select a different product,

9
00:00:22,004 --> 00:00:25,000
the nutrient table below also changes

10
00:00:25,000 --> 00:00:27,006
to reflect new set of nutrients.

11
00:00:27,006 --> 00:00:30,008
This markup helps us in thinking about the flow,

12
00:00:30,008 --> 00:00:34,004
which will now help us draw a sequence.

13
00:00:34,004 --> 00:00:37,007
We already know the sequence for initial data loading,

14
00:00:37,007 --> 00:00:40,006
which we worked through in the previous chapter.

15
00:00:40,006 --> 00:00:42,009
The food product and nutrients data

16
00:00:42,009 --> 00:00:46,002
are available in DAO in the form of product

17
00:00:46,002 --> 00:00:48,000
and nutrient maps.

18
00:00:48,000 --> 00:00:50,007
From here, our use case starts

19
00:00:50,007 --> 00:00:55,005
when the user chooses the search link on index JSP.

20
00:00:55,005 --> 00:00:58,002
Index JSP then forwards the request

21
00:00:58,002 --> 00:01:00,005
to search controller servlet.

22
00:01:00,005 --> 00:01:02,009
Search controller forwards the request

23
00:01:02,009 --> 00:01:04,007
to search inputs JSP,

24
00:01:04,007 --> 00:01:09,002
where user gets the option to enter a search string.

25
00:01:09,002 --> 00:01:11,006
Our search is currently very simple.

26
00:01:11,006 --> 00:01:13,008
So, all we have is one search box.

27
00:01:13,008 --> 00:01:15,004
But in future springs,

28
00:01:15,004 --> 00:01:18,003
we may offer options such as search by nutrient

29
00:01:18,003 --> 00:01:21,001
or ingredient or brand owner and so on.

30
00:01:21,001 --> 00:01:24,008
User sees the search box and enters a search string.

31
00:01:24,008 --> 00:01:28,008
Search input JSP received the search string

32
00:01:28,008 --> 00:01:32,003
and forwards it to search products servlet.

33
00:01:32,003 --> 00:01:35,006
The job of this servlet is to now connect with DAO

34
00:01:35,006 --> 00:01:37,006
and get search results.

35
00:01:37,006 --> 00:01:39,007
Since DAO has all the data,

36
00:01:39,007 --> 00:01:43,005
we can ask DAO to perform the search and return the results.

37
00:01:43,005 --> 00:01:45,003
Now, for all this to happen,

38
00:01:45,003 --> 00:01:48,006
we need some mechanism of passing the search string

39
00:01:48,006 --> 00:01:52,004
and search results among the JSPs and servlets.

40
00:01:52,004 --> 00:01:56,002
One approach is to use session variables.

41
00:01:56,002 --> 00:01:57,002
In this approach,

42
00:01:57,002 --> 00:02:00,000
the search products servlet get the session

43
00:02:00,000 --> 00:02:01,004
from the request

44
00:02:01,004 --> 00:02:04,007
and saves search string as a session attribute,

45
00:02:04,007 --> 00:02:07,008
then it asks DAO to search for products

46
00:02:07,008 --> 00:02:10,005
that have the search string in their names.

47
00:02:10,005 --> 00:02:13,002
DAO looks them up in its product map,

48
00:02:13,002 --> 00:02:15,003
and then returns a list of products

49
00:02:15,003 --> 00:02:18,003
that have the search string in their names.

50
00:02:18,003 --> 00:02:20,006
Now, if you recall our UI mockup,

51
00:02:20,006 --> 00:02:24,005
the first product in the list is selected by default,

52
00:02:24,005 --> 00:02:25,006
and its nutrients

53
00:02:25,006 --> 00:02:28,004
are displayed just beneath the product list.

54
00:02:28,004 --> 00:02:32,007
That means we need to search for first products nutrients.

55
00:02:32,007 --> 00:02:36,007
So now, the search product servlet makes another request

56
00:02:36,007 --> 00:02:38,001
to DAO this time

57
00:02:38,001 --> 00:02:41,002
to search what nutrients in the first product.

58
00:02:41,002 --> 00:02:42,009
DAO performs the search

59
00:02:42,009 --> 00:02:45,009
and then returns the list of nutrients.

60
00:02:45,009 --> 00:02:48,009
The search servlet has the search string,

61
00:02:48,009 --> 00:02:51,005
the product list, the list of nutrients,

62
00:02:51,005 --> 00:02:54,003
and the selected product, which is by default,

63
00:02:54,003 --> 00:02:55,004
the first product.

64
00:02:55,004 --> 00:02:58,003
It saves all these as session attributes

65
00:02:58,003 --> 00:03:03,000
and forwards the request to search results JSP for display.

66
00:03:03,000 --> 00:03:06,005
At this point, you can see that by tracing this flow,

67
00:03:06,005 --> 00:03:09,009
we have identified two new JSPs,

68
00:03:09,009 --> 00:03:12,006
search inputs and search results,

69
00:03:12,006 --> 00:03:14,004
and two new servlets,

70
00:03:14,004 --> 00:03:17,002
search controller and search product.

71
00:03:17,002 --> 00:03:20,005
You will also notice that this entire flow

72
00:03:20,005 --> 00:03:22,005
is shown in a loop,

73
00:03:22,005 --> 00:03:25,000
indicating that user can go back

74
00:03:25,000 --> 00:03:28,002
to input a new search as many times as needed.

75
00:03:28,002 --> 00:03:31,004
There is just one functionality missing in this workflow.

76
00:03:31,004 --> 00:03:34,005
And that is, user can select a different product

77
00:03:34,005 --> 00:03:36,000
from the product list.

78
00:03:36,000 --> 00:03:38,001
At that point, the search results

79
00:03:38,001 --> 00:03:40,009
should show a different list of nutrients,

80
00:03:40,009 --> 00:03:43,004
and not the nutrients for the first product

81
00:03:43,004 --> 00:03:45,007
which was selected by default.

82
00:03:45,007 --> 00:03:49,003
That means we need to have search results JSP,

83
00:03:49,003 --> 00:03:52,004
respond to a user's product selection based on

84
00:03:52,004 --> 00:03:56,006
which it needs to then change the nutrients display.

85
00:03:56,006 --> 00:04:00,001
And for that we can get a FDC ID of the selected product

86
00:04:00,001 --> 00:04:04,004
from radio button in search results JSP.

87
00:04:04,004 --> 00:04:07,000
It can then submit the form to another servlet,

88
00:04:07,000 --> 00:04:09,002
say, search nutrients.

89
00:04:09,002 --> 00:04:11,005
This servlet will then ask DAO

90
00:04:11,005 --> 00:04:13,008
to find nutrients for the product,

91
00:04:13,008 --> 00:04:15,002
get the list of nutrients,

92
00:04:15,002 --> 00:04:18,003
update the nutrients list session attribute

93
00:04:18,003 --> 00:04:21,007
and forward the request to search results

94
00:04:21,007 --> 00:04:24,005
for displaying the new set of nutrients.

95
00:04:24,005 --> 00:04:28,002
And for that we can get FDC ID of the selected product

96
00:04:28,002 --> 00:04:31,005
from radio buttons in search results JSP

97
00:04:31,005 --> 00:04:33,009
and submit the form to another servlet,

98
00:04:33,009 --> 00:04:36,001
say, search nutrients.

99
00:04:36,001 --> 00:04:38,002
This servlet will then ask DAO

100
00:04:38,002 --> 00:04:40,003
to find nutrients for the product,

101
00:04:40,003 --> 00:04:42,002
get the list of nutrients,

102
00:04:42,002 --> 00:04:45,002
update the nutrients list session attribute,

103
00:04:45,002 --> 00:04:48,000
and forward the request to search results

104
00:04:48,000 --> 00:04:51,000
for displaying the new set of nutrients.

105
00:04:51,000 --> 00:04:54,000
And once again, all this can happen in a loop

106
00:04:54,000 --> 00:04:57,005
where the user continues to select a different product

107
00:04:57,005 --> 00:04:58,009
from the search results,

108
00:04:58,009 --> 00:05:01,009
and the nutrient list keeps changing.

109
00:05:01,009 --> 00:05:03,007
So, with all these classes

110
00:05:03,007 --> 00:05:07,000
and their interaction traced out in a sequence diagram,

111
00:05:07,000 --> 00:05:08,009
we have this class diagram,

112
00:05:08,009 --> 00:05:12,004
where there are three new servlets

113
00:05:12,004 --> 00:05:14,007
and two new methods in DAO,

114
00:05:14,007 --> 00:05:16,009
that perform all the search we need.

115
00:05:16,009 --> 00:05:20,003
Also, I have now written out the hashCode

116
00:05:20,003 --> 00:05:22,005
and equals method in product class,

117
00:05:22,005 --> 00:05:25,003
as we need to compare objects for searching.

118
00:05:25,003 --> 00:05:29,000
With all this, we are now set to look at the code.

