addReview_session.jsp
001 <!-- ----------------------------------------------------------------------- -->
002 <!-- Adds a review to a randomly seleceted Book by a randomly selected -->
003 <!-- Reviewer. -->
004 <!-- Displays the review headers for the Book and all the reviewes by the -->
005 <!-- Reviewer before and after the operation. -->
006 <!-- -->
007 <!-- Request parameter -->
008 <!-- rating an integer between and 5, both inclusive -->
009 <!-- comment a string as review comment -->
010 <!-- ----------------------------------------------------------------------- -->
011 
012 <%page errorPage="exception.jsp" %>
013 <link href="sample.css" rel="stylesheet"></link>
014 <HTML>
015 <BODY>
016 <%include file="header.html" %>
017 <%include file="common_session.jsp" %>
018 
019 This example creates a new Review. A review maintains two references: one to
020 the item being reviewed and other to the reviewer who created it. <p>
021 On the other hand, the same review is maintained both by the
022 Reviewer as the list of reviewes s/he has created as well as by the item as
023 the list of reviews.<p>
024 
025 This example demonstrates how such relationship integrity is maintained when
026 creating an instance that is linked to multiple instances.
027 <HR>
028 
029 
030 <%
031 
032   List<Book>     books = serviceImpl.getAll(Book.class);
033   if (books == null || books.isEmpty()) {
034     out.println("Found no book in the database");
035     return;
036   }
037 
038   List<Reviewer> reviewers = serviceImpl.getAll(Reviewer.class);
039   if (reviewers == null || reviewers.isEmpty()) {
040     out.println("Found no book in the database");
041     return;
042   }
043 
044   java.util.Random random = new Random(System.currentTimeMillis());
045   Book book = books.get(random.nextInt(books.size()));
046   Reviewer reviewer = reviewers.get(random.nextInt(reviewers.size()));
047 
048   List<Review> reviewsOfBook = serviceImpl.getReviewsByItem(book.getId());
049   List<Review> reviewsByReviewer = serviceImpl.getReviewsByReviewer(reviewer.getName());
050 %>
051 <HR>
052 Selected Reviewer <em><%=reviewer.getName()%></em> out of
053 <em><%=reviewers.size()%></em> existing reviewers<BR>
054 Selected Book <em><%=book.getTitle()%></em> out of
055 <em><%=books.size()%></em> existing books<BR>
056 <HR>
057 
058 <HR>
059 
060 
061 <%
062   if (reviewsOfBook != null && !reviewsOfBook.isEmpty()) {
063 %>
064 <CENTER>Before adding this review, <em><%=book.getTitle()%></em> had
065   <em><%=reviewsOfBook.size()%></em> reviews</CENTER><BR>
066 
067 <DIV ALIGN="CENTER">
068   <TABLE class="table">
069     <TR><TH>Reviewer</TH><TH>Date</TH><TH>Rating</TH>
070       <%
071 
072 
073                   int i = 0;
074                   for (Review r:reviewsOfBook)
075                   {
076                      i++;
077                      String style = (i%2==0"spec" "specalt";
078 
079 
080       %>
081     <TR class="<%=style%>">
082       <TD><%=r.getReviewer().getName()%></TD>
083       <TD><%=dateFormat.format(r.getCreatedDate())%></TD>
084       <TD><%=r.getRating()%></TD>
085       <%
086 
087 
088 
089 
090               }
091 
092 
093 
094 
095       %>
096   </TABLE>
097 </DIV>
098 <%
099   }
100 %>
101 
102 
103 <BR>
104 
105 
106 <%
107   if (reviewsByReviewer != null && !reviewsByReviewer.isEmpty()) {
108 
109 %>
110 <CENTER>Before adding this review, <em><%=reviewer.getName()%></em> had
111   <em><%=reviewsByReviewer.size()%></em> reviews.</CENTER><BR>
112 
113 <DIV ALIGN="CENTER">
114   <TABLE WIDTH="80%" TABLEBORDER=1>
115     <TR><TH>Item</TH><TH>Date</TH><TH>Rating</TH>
116       <%
117 
118 
119 
120 
121                   int i = 0;
122                   for (Review r:reviewsByReviewer)
123                   {
124                      i++;
125                      String style = (i%2==0"spec" "specalt";
126 
127 
128 
129       %>
130     <TR class="<%=style%>">
131       <TD><%=r.getReviewed().getTitle()%></TD>
132       <TD><%=dateFormat.format(r.getCreatedDate())%></TD>
133       <TD><%=r.getRating()%></TD>
134       <%
135 
136 
137 
138 
139               }
140 
141 
142 
143 
144       %>
145   </TABLE>
146 </DIV>
147 <%
148   }
149 %>
150 
151 
152 <%
153   String comment = weblogic.servlet.security.Utils.encodeXSS(request.getParameter("comment"));
154   int rating = Integer.parseInt(weblogic.servlet.security.Utils.encodeXSS(request.getParameter("rating")));
155 %>
156 <HR>
157   Will add a review of <em><%=book.getTitle()%></em> by
158   <em><%=reviewer.getName()%></em> with <p>
159   rating <em><%=rating%></em> and comment "<em><%=comment%></em>".
160 </HR>
161 <%
162   serviceImpl.newReview(reviewer, book, rating, comment);
163 
164   List<Review> newReviewsOfBook = serviceImpl.getReviewsByItem(book.getId());
165   List<Review> newReviewsByReviewer = serviceImpl.getReviewsByReviewer(reviewer.getName());
166 %>
167 
168 <CENTER>New review has now been added</CENTER>
169 <BR>
170 
171 <CENTER>After adding this review, <em><%=book.getTitle()%></em> has
172   <em><%=newReviewsOfBook.size()%></em> reviews</CENTER><BR>
173 
174 <DIV ALIGN="CENTER">
175   <TABLE WIDTH="80%" TABLEBORDER=1>
176     <TR><TH>Reviewer</TH><TH>Date</TH><TH>Rating</TH>
177       <%
178 
179 
180 
181 
182             int i = 0;
183             for (Review r:newReviewsOfBook)
184             {
185                i++;
186                      String style = (i%2==0"spec" "specalt";
187 
188 
189 
190       %>
191     <TR class="<%=style%>">
192       <TD><%=r.getReviewer().getName()%></TD>
193       <TD><%=dateFormat.format(r.getCreatedDate())%></TD>
194       <TD><%=r.getRating()%></TD>
195       <%
196 
197 
198 
199 
200         }
201 
202 
203 
204 
205       %>
206   </TABLE>
207 </DIV>
208 <BR>
209 
210 <CENTER>After adding this review <em><%=reviewer.getName()%></em> has
211 </em><%=newReviewsByReviewer.size()%></em> reviews.</CENTER><BR>
212 
213 <DIV ALIGN="CENTER">
214   <TABLE WIDTH="80%" TABLEBORDER=1>
215     <TR><TH>Item</TH><TH>Date</TH><TH>Rating</TH>
216       <%
217 
218 
219 
220 
221              i = 0;
222             for (Review r:newReviewsByReviewer)
223             {
224                i++;
225                      String style = (i%2==0"spec" "specalt";
226 
227 
228 
229       %>
230     <TR class="<%=style%>">
231       <TD><%=r.getReviewed().getTitle()%></TD>
232       <TD><%=dateFormat.format(r.getCreatedDate())%></TD>
233       <TD><%=r.getRating()%></TD>
234       <%
235 
236 
237 
238 
239         }
240 
241 
242 
243 
244       %>
245   </TABLE>
246 </DIV>
247 </BODY>
248 </HTML>