summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Ørbekk <orbekk@pvv.ntnu.no>2010-09-03 01:13:15 -0400
committerKjetil Ørbekk <orbekk@pvv.ntnu.no>2010-09-03 01:13:15 -0400
commit1fb83a8c3edefbc2a43d26cc6db835e592d65934 (patch)
tree73047a16e263ae3f84031bc2d82e3961d2140430
parentb2dd8bf148b70c3076625840f908b5c7d32e9ee1 (diff)
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.
-rw-r--r--html/quotes.jsp96
-rw-r--r--src/WEB-INF/web.xml35
-rw-r--r--src/lq/DateUtil.java15
-rw-r--r--src/lq/DropData.java30
-rw-r--r--src/lq/QuoteUtils.java54
-rw-r--r--src/lq/Strings.java12
-rw-r--r--src/lq/Test.java7
7 files changed, 226 insertions, 23 deletions
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"
+%>
+<html>
+<head>
+<meta name="robots" content="noindex, nofollow" />
+<title>Quotes fra #linux.no på freenode</title>
+<style type="text/css">
+body {font-family: monospace;}
+hr {
+ border-style: solid;
+ border-color: black;
+ border-width: 1px;
+}
+</style>
+</head>
+<body bgcolor="#FFFFFF" text="#000000" link="#000000" vlink="#000000">
+
+<pre>
+ _ _
+ | (_)_ __ _ ___ __ _ __ ___
+ _|_|_ | | | '_ \| | | \ \/ / | '_ \ / _ \
+ _|_|_ | | | | | | |_| |) ( _| | | | (_) |
+ | | |_|_|_| |_|\__,_/_/\_(_)_| |_|\___/
+ -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+ Quotes fra #linux.no på freenode
+ Klikk <a href="submit.jsp">her</a> for å legge til en quote
+ eller <a href="quotevote">her</a> for raskere stemming
+
+ Sortér etter <a href="quotes.jsp?order=date">dato</a> | <a href="quotes.jsp?order=score">score</a> | <a href="quotes.jsp?order=number">id</a>
+</pre>
+<hr>
+
+<script>
+
+function ajaxvote(id, value) {
+ var http = new XMLHttpRequest();
+ http.open("GET","ajaxvote.jsp?id="+id+"&vote="+value);
+ http.onreadystatechange=function() {
+ if(http.readyState==4) {
+ document.getElementById("v"+id).innerHTML = http.responseText;
+ }
+ }
+ http.send(null);
+}
+</script>
+
+
+<%
+
+String order = request.getParameter("order");
+
+List<lq.Quote> 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("<br>");
+ out.println("<a href=\"vote.jsp?id=" + quote.getId() + "\">" +
+ "#" + quote.getId() +
+ "</a>"+
+ ", lagt til av " + lq.Strings.escape(quote.getAuthor()) + "<br>");
+
+ String score = quote.getScore().toString();
+ String date = lq.DateUtil.dateFormat.format(quote.getQuoteDate());
+ out.println("Dato: " + date + ", Score: ");
+ out.println("<span id=\"v" + quote.getId() + "\">");
+ out.println((score==null?"-":(score+ " (fra " + quote.getVotes().size() +")")));
+ out.println("<br> Vote: <font size=\"-1\">");
+ for(int nv=1; nv<=5; nv++)
+ out.println("<a href=\"javascript:ajaxvote(" + quote.getId() + ","+nv+")\">"+nv+"</a> ");
+ out.println("</font> </span>");
+
+ out.println("<br> <br>");
+ out.println();
+ String content = lq.Strings.escape(quote.getContent());
+ out.println(content
+ .replaceAll("(http://[^ \r\n]+)","<a href=\"$1\">$1</a>")
+ .replaceAll("\n","<br>\n"));
+ out.println("");
+ out.println("<hr>");
+}
+
+%>
+</body>
+</html>
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">
<display-name>Quotes fra #linux.no på freenode</display-name>
-
- <!--
+
<servlet>
- <servlet-name>linoquotes</servlet-name>
- <servlet-class>lq.ViewQuotes</servlet-class>
+ <servlet-name>importquotes</servlet-name>
+ <servlet-class>lq.ImportQuotes</servlet-class>
</servlet>
<servlet-mapping>
- <servlet-name>linoquotes</servlet-name>
- <url-pattern>/quotes</url-pattern>
+ <servlet-name>importquotes</servlet-name>
+ <url-pattern>/import</url-pattern>
</servlet-mapping>
- -->
<servlet>
- <servlet-name>redirect-to-quotes</servlet-name>
- <servlet-class>lq.Redirect</servlet-class>
- <init-param>
- <param-name>destination</param-name>
- <param-value>/quotes</param-value>
- </init-param>
+ <servlet-name>dropdb</servlet-name>
+ <servlet-class>lq.DropData</servlet-class>
</servlet>
<servlet-mapping>
- <servlet-name>redirect-to-quotes</servlet-name>
- <url-pattern>/</url-pattern>
+ <servlet-name>dropdb</servlet-name>
+ <url-pattern>/dropdb</url-pattern>
</servlet-mapping>
- <servlet>
- <servlet-name>importquotes</servlet-name>
- <servlet-class>lq.ImportQuotes</servlet-class>
- </servlet>
+ <welcome-file-list>
+ <welcome-file>quotes.jsp</welcome-file>
+ </welcome-file-list>
- <servlet-mapping>
- <servlet-name>importquotes</servlet-name>
- <url-pattern>/import</url-pattern>
- </servlet-mapping>
</web-app>
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<Quote> quotes = (List<Quote>) pm.newQuery(Quote.class).execute();
+ for (Quote q : quotes) {
+ pm.deletePersistent(q);
+ }
+ List<Vote> votes = (List<Vote>) 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<Quote> getQuotes() {
+ PersistenceManager pm = PMF.get().getPersistenceManager();
+ try {
+ List<Quote> quotes = (List<Quote>) pm.newQuery(Quote.class).execute();
+ pm.retrieveAll(quotes);
+ return quotes;
+ }
+ finally {
+ pm.close();
+ }
+ }
+
+ public static List<Quote> getQuotesOrderedByIdDesc() {
+ List<Quote> quotes = getQuotes();
+ Collections.sort(quotes,
+ new Comparator<Quote>() {
+ public int compare(Quote q1, Quote q2) {
+ return Long.signum(q2.getId() - q1.getId());
+ }
+ });
+ return quotes;
+ }
+
+ public static List<Quote> getQuotesOrderedByScoreDesc() {
+ List<Quote> quotes = getQuotes();
+ Collections.sort(quotes,
+ new Comparator<Quote>() {
+ public int compare(Quote q1, Quote q2) {
+ throw new RuntimeException("Score ordering not yet implemented");
+ }
+ });
+ return quotes;
+ }
+
+ public static List<Quote> getQuotesOrderedByDateDesc() {
+ List<Quote> quotes = getQuotes();
+ Collections.sort(quotes,
+ new Comparator<Quote>() {
+ 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("&","&amp;")
+ .replaceAll("<","&lt;")
+ .replaceAll(">","&gt;")
+ .replaceAll(" ","&nbsp;&nbsp;");
+
+ }
+}
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";
+ }
+}