From 1fb83a8c3edefbc2a43d26cc6db835e592d65934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Fri, 3 Sep 2010 01:13:15 -0400 Subject: Quote listing Ported the quotes.jsp page from the old quote system. This commit contains some utility classes related to this. DropData is a temporary hack to remove all data in the datastore. TODO: Remove this when we are finished migrating the application. --- html/quotes.jsp | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/WEB-INF/web.xml | 35 +++++++----------- src/lq/DateUtil.java | 15 ++++++++ src/lq/DropData.java | 30 ++++++++++++++++ src/lq/QuoteUtils.java | 54 ++++++++++++++++++++++++++++ src/lq/Strings.java | 12 +++++++ src/lq/Test.java | 7 ++++ 7 files changed, 226 insertions(+), 23 deletions(-) create mode 100644 html/quotes.jsp create mode 100644 src/lq/DateUtil.java create mode 100644 src/lq/DropData.java create mode 100644 src/lq/QuoteUtils.java create mode 100644 src/lq/Strings.java create mode 100644 src/lq/Test.java diff --git a/html/quotes.jsp b/html/quotes.jsp new file mode 100644 index 0000000..4d0c5d7 --- /dev/null +++ b/html/quotes.jsp @@ -0,0 +1,96 @@ +<%@ page contentType="text/html; charset=UTF-8" + import="java.util.List" +%> + + + +Quotes fra #linux.no på freenode + + + + +
+                     _ _  
+                    | (_)_ __  _   ___  __  _ __   ___  
+              _|_|_ | | | '_ \| | | \ \/ / | '_ \ / _ \ 
+              _|_|_ | | | | | | |_| |)  ( _| | | | (_) | 
+               | |  |_|_|_| |_|\__,_/_/\_(_)_| |_|\___/ 
+             -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+                   Quotes fra #linux.no på freenode
+                  Klikk her for å legge til en quote
+                    eller her for raskere stemming
+
+                    Sortér etter dato | score | id
+
+
+ + + + +<% + +String order = request.getParameter("order"); + +List quotes; + +if (order == null) { + quotes = lq.QuoteUtils.getQuotesOrderedByIdDesc(); +} else if(order.equals("id")) { + quotes = lq.QuoteUtils.getQuotesOrderedByIdDesc(); +} else if(order.equals("score")) { + quotes = lq.QuoteUtils.getQuotesOrderedByScoreDesc(); +} else if(order.equals("date")) { + quotes = lq.QuoteUtils.getQuotesOrderedByDateDesc(); +} else { + quotes = lq.QuoteUtils.getQuotesOrderedByIdDesc(); +} + +for (lq.Quote quote : quotes) { + out.println("
"); + out.println("" + + "#" + quote.getId() + + ""+ + ", lagt til av " + lq.Strings.escape(quote.getAuthor()) + "
"); + + String score = quote.getScore().toString(); + String date = lq.DateUtil.dateFormat.format(quote.getQuoteDate()); + out.println("Dato: " + date + ", Score: "); + out.println(""); + out.println((score==null?"-":(score+ " (fra " + quote.getVotes().size() +")"))); + out.println("
Vote: "); + for(int nv=1; nv<=5; nv++) + out.println(""+nv+" "); + out.println("
"); + + out.println("

"); + out.println(); + String content = lq.Strings.escape(quote.getContent()); + out.println(content + .replaceAll("(http://[^ \r\n]+)","$1") + .replaceAll("\n","
\n")); + out.println(""); + out.println("
"); +} + +%> + + diff --git a/src/WEB-INF/web.xml b/src/WEB-INF/web.xml index c9a636a..bb255cb 100644 --- a/src/WEB-INF/web.xml +++ b/src/WEB-INF/web.xml @@ -5,40 +5,29 @@ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> Quotes fra #linux.no på freenode - - - redirect-to-quotes - lq.Redirect - - destination - /quotes - + dropdb + lq.DropData - redirect-to-quotes - / + dropdb + /dropdb - - importquotes - lq.ImportQuotes - + + quotes.jsp + - - importquotes - /import - diff --git a/src/lq/DateUtil.java b/src/lq/DateUtil.java new file mode 100644 index 0000000..7b91a9a --- /dev/null +++ b/src/lq/DateUtil.java @@ -0,0 +1,15 @@ +package lq; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; + +public final class DateUtil { + public static final SimpleDateFormat dateFormat = + new SimpleDateFormat("yyyy-MM-dd"); + + public static final SimpleDateFormat timestampFormat = + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + private DateUtil() { + } +} diff --git a/src/lq/DropData.java b/src/lq/DropData.java new file mode 100644 index 0000000..06f3583 --- /dev/null +++ b/src/lq/DropData.java @@ -0,0 +1,30 @@ +package lq; + +import java.io.IOException; +import java.util.List; +import javax.jdo.PersistenceManager; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class DropData extends HttpServlet { + + @Override + public void doGet(HttpServletRequest req, HttpServletResponse resp) + throws IOException { + PersistenceManager pm = PMF.get().getPersistenceManager(); + try { + List quotes = (List) pm.newQuery(Quote.class).execute(); + for (Quote q : quotes) { + pm.deletePersistent(q); + } + List votes = (List) pm.newQuery(Vote.class).execute(); + for (Vote v : votes) { + pm.deletePersistent(v); + } + } + finally { + pm.close(); + } + } +} diff --git a/src/lq/QuoteUtils.java b/src/lq/QuoteUtils.java new file mode 100644 index 0000000..f19dbf6 --- /dev/null +++ b/src/lq/QuoteUtils.java @@ -0,0 +1,54 @@ +package lq; + +import java.util.Collections; +import java.util.List; +import java.util.Comparator; +import javax.jdo.PersistenceManager; +import javax.jdo.Query; + +public class QuoteUtils { + public static List getQuotes() { + PersistenceManager pm = PMF.get().getPersistenceManager(); + try { + List quotes = (List) pm.newQuery(Quote.class).execute(); + pm.retrieveAll(quotes); + return quotes; + } + finally { + pm.close(); + } + } + + public static List getQuotesOrderedByIdDesc() { + List quotes = getQuotes(); + Collections.sort(quotes, + new Comparator() { + public int compare(Quote q1, Quote q2) { + return Long.signum(q2.getId() - q1.getId()); + } + }); + return quotes; + } + + public static List getQuotesOrderedByScoreDesc() { + List quotes = getQuotes(); + Collections.sort(quotes, + new Comparator() { + public int compare(Quote q1, Quote q2) { + throw new RuntimeException("Score ordering not yet implemented"); + } + }); + return quotes; + } + + public static List getQuotesOrderedByDateDesc() { + List quotes = getQuotes(); + Collections.sort(quotes, + new Comparator() { + public int compare(Quote q1, Quote q2) { + return q2.getQuoteDate().compareTo(q1.getQuoteDate()); + } + }); + return quotes; + } +} diff --git a/src/lq/Strings.java b/src/lq/Strings.java new file mode 100644 index 0000000..f3604f0 --- /dev/null +++ b/src/lq/Strings.java @@ -0,0 +1,12 @@ +package lq; + +public class Strings { + public static final String escape(String raw) { + return raw + .replaceAll("&","&") + .replaceAll("<","<") + .replaceAll(">",">") + .replaceAll(" ","  "); + + } +} diff --git a/src/lq/Test.java b/src/lq/Test.java new file mode 100644 index 0000000..e294b7f --- /dev/null +++ b/src/lq/Test.java @@ -0,0 +1,7 @@ +package lq; + +public class Test { + public static final String test() { + return "Hello from Test"; + } +} -- cgit v1.2.3