From 3f0d7c1ebef350624d17045a54adb3b43ec3abf4 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Fri, 3 Sep 2010 18:18:54 -0400 Subject: Add ViewQuote servlet. ViewQuote displays a single quote. --- src/WEB-INF/web.xml | 10 ++++++++++ src/lq/Printer.java | 2 +- src/lq/QuoteUtil.java | 21 +++++++++++++++++++++ src/lq/ViewQuote.java | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/lq/ViewQuote.java diff --git a/src/WEB-INF/web.xml b/src/WEB-INF/web.xml index 1955309..432d10a 100644 --- a/src/WEB-INF/web.xml +++ b/src/WEB-INF/web.xml @@ -46,6 +46,16 @@ /new_admin + + view_quote + lq.ViewQuote + + + + view_quote + /view_quote + + quotes.jsp diff --git a/src/lq/Printer.java b/src/lq/Printer.java index 1790a5b..7677450 100644 --- a/src/lq/Printer.java +++ b/src/lq/Printer.java @@ -12,7 +12,7 @@ public class Printer { public void printQuote(Quote quote) { out.println("
"); - out.println("" + + out.println("" + "#" + quote.getId() + ""+ ", lagt til av " + Strings.escape(quote.getAuthor()) + "
"); diff --git a/src/lq/QuoteUtil.java b/src/lq/QuoteUtil.java index 5104cb0..72b94af 100644 --- a/src/lq/QuoteUtil.java +++ b/src/lq/QuoteUtil.java @@ -21,6 +21,27 @@ public class QuoteUtil { } } + public static Quote getQuoteWithId(Long id) { + PersistenceManager pm = PMF.get().getPersistenceManager(); + try { + Query quoteQuery = pm.newQuery(Quote.class); + quoteQuery.setFilter("id == idParam"); + quoteQuery.declareParameters("Long idParam"); + List quotes = (List) quoteQuery.execute(id); + if (quotes.isEmpty()) { + return null; + } + else { + Quote result = quotes.get(0); + pm.retrieve(result); + return result; + } + } + finally { + pm.close(); + } + } + public static List getQuotesPendingApproval() { PersistenceManager pm = PMF.get().getPersistenceManager(); try { diff --git a/src/lq/ViewQuote.java b/src/lq/ViewQuote.java new file mode 100644 index 0000000..2575523 --- /dev/null +++ b/src/lq/ViewQuote.java @@ -0,0 +1,32 @@ +package lq; + +import java.io.IOException; +import java.text.ParseException; +import java.util.Date; +import java.util.List; +import javax.jdo.PersistenceManager; +import javax.jdo.Query; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class ViewQuote extends HttpServlet { + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws IOException { + String idParam = req.getParameter("id"); + Long id = Long.parseLong(idParam); + Quote quote = QuoteUtil.getQuoteWithId(id); + + resp.setContentType("text/html"); + + if (quote != null) { + Printer printer = new Printer(resp.getWriter()); + printer.printQuote(quote); + } + else { + resp.getWriter().println("Quote not found."); + } + } +} -- cgit v1.2.3