1
00:00:00,000 --> 00:00:02,004
- [Instructor] Before we implement our POC,

2
00:00:02,004 --> 00:00:05,009
we need to take a look at our use case diagram,

3
00:00:05,009 --> 00:00:08,008
as there is one change we need to make.

4
00:00:08,008 --> 00:00:10,001
We were initially thinking

5
00:00:10,001 --> 00:00:12,004
of connecting directly to USDA DB,

6
00:00:12,004 --> 00:00:15,008
but later we decided to have the data loaded

7
00:00:15,008 --> 00:00:19,002
upfront into our own Red30 DB.

8
00:00:19,002 --> 00:00:22,001
So we will not show it in this diagram any more,

9
00:00:22,001 --> 00:00:24,002
because any database that is fully

10
00:00:24,002 --> 00:00:26,002
controlled by our application

11
00:00:26,002 --> 00:00:28,007
is part of the system that we are creating,

12
00:00:28,007 --> 00:00:31,005
and not an external actor.

13
00:00:31,005 --> 00:00:34,005
Now, about the functionality for POC.

14
00:00:34,005 --> 00:00:37,003
A good idea is to take something that is simple,

15
00:00:37,003 --> 00:00:40,000
is part of one of the core use cases,

16
00:00:40,000 --> 00:00:42,007
and cuts across the entire technology stack.

17
00:00:42,007 --> 00:00:45,008
So how about just printing the number of products

18
00:00:45,008 --> 00:00:47,006
that are there in the database?

19
00:00:47,006 --> 00:00:50,006
I think it checks all the boxes.

20
00:00:50,006 --> 00:00:52,007
So starting from the front end,

21
00:00:52,007 --> 00:00:55,008
there was this UI mock-up, which we saw earlier.

22
00:00:55,008 --> 00:00:59,005
So we do have some idea of the look and feel.

23
00:00:59,005 --> 00:01:01,007
Instead of implementing the functionalities,

24
00:01:01,007 --> 00:01:04,002
such as search, compare, et cetera,

25
00:01:04,002 --> 00:01:08,001
all we need here is a POC link, which when I click,

26
00:01:08,001 --> 00:01:11,004
it should display the product map sites.

27
00:01:11,004 --> 00:01:15,000
To get that going, I have used the classic approach

28
00:01:15,000 --> 00:01:17,007
of iteratively moving between sequence diagram,

29
00:01:17,007 --> 00:01:19,008
class diagram, and code.

30
00:01:19,008 --> 00:01:22,004
It helped me model the behavioral

31
00:01:22,004 --> 00:01:24,009
and structural aspects of the system,

32
00:01:24,009 --> 00:01:28,008
so whatever I'm going to show you did not come in one shot.

33
00:01:28,008 --> 00:01:31,003
It took some back and forth between modeling

34
00:01:31,003 --> 00:01:34,000
and coding to get this straight.

35
00:01:34,000 --> 00:01:35,009
So let us see what we got.

36
00:01:35,009 --> 00:01:39,003
Very early in the cycle, I realized that if I sent

37
00:01:39,003 --> 00:01:41,001
an SQL query to the database

38
00:01:41,001 --> 00:01:44,002
for every search or comparison request,

39
00:01:44,002 --> 00:01:46,008
the response time gets really poor.

40
00:01:46,008 --> 00:01:49,004
And since all food product-related data

41
00:01:49,004 --> 00:01:52,002
coming from USDA is read-only,

42
00:01:52,002 --> 00:01:56,003
there is no need to read it afresh for every query,

43
00:01:56,003 --> 00:01:59,007
so I decided to load all food products-related data

44
00:01:59,007 --> 00:02:02,007
into memory when the application starts.

45
00:02:02,007 --> 00:02:06,000
And I did that by creating a DataLoader servlet

46
00:02:06,000 --> 00:02:10,000
whose init is involved when application starts up.

47
00:02:10,000 --> 00:02:13,004
This init does the job of initial data loading.

48
00:02:13,004 --> 00:02:16,001
Another class I created here is Dao.

49
00:02:16,001 --> 00:02:19,002
That is data access object, which will connect with

50
00:02:19,002 --> 00:02:22,008
the data source that we defined in context XML.

51
00:02:22,008 --> 00:02:25,006
Let us model this init sequence diagram.

52
00:02:25,006 --> 00:02:27,003
Starting from top left,

53
00:02:27,003 --> 00:02:30,006
when server calls init on DataLoader,

54
00:02:30,006 --> 00:02:33,002
the DataLoader reaches out to Dao,

55
00:02:33,002 --> 00:02:37,002
whose instance establishes connection with red30db,

56
00:02:37,002 --> 00:02:40,005
and then passes itself to DataLoader.

57
00:02:40,005 --> 00:02:43,006
The DataLoader then loads all the data.

58
00:02:43,006 --> 00:02:46,001
So my final class diagram

59
00:02:46,001 --> 00:02:48,009
for initial data load looks like this.

60
00:02:48,009 --> 00:02:52,008
Here we have DataLoader extending HttpServlet,

61
00:02:52,008 --> 00:02:56,009
that has in it destroy and three methods for loading data.

62
00:02:56,009 --> 00:03:00,007
It contains an instance of Dao that has two maps,

63
00:03:00,007 --> 00:03:02,008
Product map and Nutrient map,

64
00:03:02,008 --> 00:03:06,000
which is where DataLoader loads all the data.

65
00:03:06,000 --> 00:03:08,006
Product and Nutrient are two beans

66
00:03:08,006 --> 00:03:11,009
that have their properties, sectors, and getters.

67
00:03:11,009 --> 00:03:15,000
I have also created non-default constructors

68
00:03:15,000 --> 00:03:18,002
which initialize some of their properties.

69
00:03:18,002 --> 00:03:21,008
Okay, now let us take a look at Eclipse.

70
00:03:21,008 --> 00:03:24,005
First, the overall structure.

71
00:03:24,005 --> 00:03:28,002
In JavaResources inside sourceMainJava,

72
00:03:28,002 --> 00:03:31,000
I have a package, commHPlus4_red30,

73
00:03:31,000 --> 00:03:35,002
that has Dao, DataLoader, and PocController,

74
00:03:35,002 --> 00:03:38,002
which will implement the functionality for POC.

75
00:03:38,002 --> 00:03:42,000
Then in beans I have Nutrient and Product beans.

76
00:03:42,000 --> 00:03:45,001
The JSPs are inside webapp.

77
00:03:45,001 --> 00:03:49,005
Inside src main I have css folder here.

78
00:03:49,005 --> 00:03:51,000
That has stylecss.

79
00:03:51,000 --> 00:03:53,008
Images, that has the logo image.

80
00:03:53,008 --> 00:03:57,003
Jsp folder, that has another folder, POC,

81
00:03:57,003 --> 00:04:00,006
with pocResults.jsp.

82
00:04:00,006 --> 00:04:03,002
Jsp also has header.jsp,

83
00:04:03,002 --> 00:04:06,006
which is the common header across all pages.

84
00:04:06,006 --> 00:04:09,002
WEB-INR, which has web.xml.

85
00:04:09,002 --> 00:04:14,001
And index.jsp, that is where everything gets started.

86
00:04:14,001 --> 00:04:19,009
Now let us take a look at code inside all these files.

87
00:04:19,009 --> 00:04:23,003
Starting with Dao,

88
00:04:23,003 --> 00:04:27,008
in Dao class there is a private constructor at line 30

89
00:04:27,008 --> 00:04:30,003
in which it creates an initial context,

90
00:04:30,003 --> 00:04:33,000
and then looks up the DataSource resource,

91
00:04:33,000 --> 00:04:36,002
which we have defined in our context XML file.

92
00:04:36,002 --> 00:04:38,003
It uses this resource to initialize

93
00:04:38,003 --> 00:04:41,008
the connection variable in line 36.

94
00:04:41,008 --> 00:04:46,000
In line 43 the getInstance method checks

95
00:04:46,000 --> 00:04:49,002
if the private member variable Dao is null.

96
00:04:49,002 --> 00:04:53,009
If yes, it invokes the constructor and returns its instance.

97
00:04:53,009 --> 00:04:56,009
This is singleton pattern, which ensures that we have

98
00:04:56,009 --> 00:05:00,000
only one instance of Dao running in our system,

99
00:05:00,000 --> 00:05:01,009
which then ensures that we have

100
00:05:01,009 --> 00:05:05,001
just one connection to database at a time.

101
00:05:05,001 --> 00:05:08,007
Now let us look at DataLoader.

102
00:05:08,007 --> 00:05:13,007
In its init method, we get Dao's instance in line 30,

103
00:05:13,007 --> 00:05:17,008
and then invoke the three methods for loading data.

104
00:05:17,008 --> 00:05:20,003
The destroy method just cleans up the memory

105
00:05:20,003 --> 00:05:22,008
and closes the connection.

106
00:05:22,008 --> 00:05:26,002
And then there are three data loading methods:

107
00:05:26,002 --> 00:05:32,006
loadProducts, loadNutrients, and loadProductNutrients.

108
00:05:32,006 --> 00:05:36,000
These methods use SQL queries to pull the data

109
00:05:36,000 --> 00:05:39,009
and load the Product and Nutrient maps.

110
00:05:39,009 --> 00:05:43,002
Product and Nutrient are two beans.

111
00:05:43,002 --> 00:05:49,005
Nutrient has id, name, and unit_name as three properties.

112
00:05:49,005 --> 00:05:52,004
I have used these same names as the field names

113
00:05:52,004 --> 00:05:56,000
in database table for better understandability.

114
00:05:56,000 --> 00:05:59,006
Product similarly has many properties,

115
00:05:59,006 --> 00:06:03,007
all named as per field names in the database tables.

116
00:06:03,007 --> 00:06:07,005
There is this productNutrientMap in line 15

117
00:06:07,005 --> 00:06:09,004
which will store all nutrients

118
00:06:09,004 --> 00:06:12,002
and their amounts in the product.

119
00:06:12,002 --> 00:06:14,004
Now let us take a look at JSPs,

120
00:06:14,004 --> 00:06:17,003
which will help us understand the flow.

121
00:06:17,003 --> 00:06:20,009
They're all in the src main, as I showed you earlier.

122
00:06:20,009 --> 00:06:23,007
Starting with index.jsp,

123
00:06:23,007 --> 00:06:27,007
here we have header.jsp included in line 11,

124
00:06:27,007 --> 00:06:30,007
followed by some simple text.

125
00:06:30,007 --> 00:06:34,004
Then in header.jsp we have image logo

126
00:06:34,004 --> 00:06:36,006
and a set of links that you see

127
00:06:36,006 --> 00:06:40,007
for index, POC, search, compare, and login.

128
00:06:40,007 --> 00:06:42,004
We will implement the search,

129
00:06:42,004 --> 00:06:44,009
compare, login functionalities later.

130
00:06:44,009 --> 00:06:47,008
For now, we will have POC,

131
00:06:47,008 --> 00:06:51,000
which is pointing to our PocController,

132
00:06:51,000 --> 00:06:53,000
so let's take a look at that.

133
00:06:53,000 --> 00:06:54,009
In PocController we have

134
00:06:54,009 --> 00:06:58,005
just simple doGet and doPost methods.

135
00:06:58,005 --> 00:07:01,006
The doGet method is forwarding

136
00:07:01,006 --> 00:07:05,006
the request to pocResults.jsp,

137
00:07:05,006 --> 00:07:09,001
and pocResults.jsp is simply printing

138
00:07:09,001 --> 00:07:12,005
the productMap.size in line 14.

139
00:07:12,005 --> 00:07:16,008
Okay, now let us run the application.

140
00:07:16,008 --> 00:07:23,000
I go into project, run as, run on server.

141
00:07:23,000 --> 00:07:25,008
This will take a little bit of a time,

142
00:07:25,008 --> 00:07:28,008
because it has to load all the data, as you can see,

143
00:07:28,008 --> 00:07:31,006
and the loading is completed,

144
00:07:31,006 --> 00:07:35,005
and our application is up and running.

145
00:07:35,005 --> 00:07:38,003
If I click on POC I can see

146
00:07:38,003 --> 00:07:41,002
the number of products in database.

147
00:07:41,002 --> 00:07:43,007
So the entire flow that you just looked at,

148
00:07:43,007 --> 00:07:47,004
I have traced it out in a sequence diagram here.

149
00:07:47,004 --> 00:07:49,008
I see several benefits of doing so.

150
00:07:49,008 --> 00:07:52,005
First, by laying out the entire flow,

151
00:07:52,005 --> 00:07:55,003
it helps me think clearly about my design,

152
00:07:55,003 --> 00:07:57,002
and identify any incorrect

153
00:07:57,002 --> 00:08:00,008
or unnecessary objects, loops, or flows.

154
00:08:00,008 --> 00:08:03,007
Second, it helps anyone else reading

155
00:08:03,007 --> 00:08:06,003
and trying to understand my code and design.

156
00:08:06,003 --> 00:08:08,009
And third, it is a great pedagogical tool,

157
00:08:08,009 --> 00:08:11,008
because it helps one teach and learn.

158
00:08:11,008 --> 00:08:15,001
Using the objects identified in this sequence diagram,

159
00:08:15,001 --> 00:08:17,007
here is the final class diagram.

160
00:08:17,007 --> 00:08:20,003
You can find this and many other diagrams

161
00:08:20,003 --> 00:08:23,003
in the artifacts provided along with this course.

162
00:08:23,003 --> 00:08:26,000
Now that we have looked at implementation,

163
00:08:26,000 --> 00:08:29,000
it's time to move all this to production server.

