diff options
author | Kjetil Ørbekk <orbekk@pvv.ntnu.no> | 2010-09-03 01:13:15 -0400 |
---|---|---|
committer | Kjetil Ørbekk <orbekk@pvv.ntnu.no> | 2010-09-03 01:13:15 -0400 |
commit | 1fb83a8c3edefbc2a43d26cc6db835e592d65934 (patch) | |
tree | 73047a16e263ae3f84031bc2d82e3961d2140430 /src/lq | |
parent | b2dd8bf148b70c3076625840f908b5c7d32e9ee1 (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.
Diffstat (limited to 'src/lq')
-rw-r--r-- | src/lq/DateUtil.java | 15 | ||||
-rw-r--r-- | src/lq/DropData.java | 30 | ||||
-rw-r--r-- | src/lq/QuoteUtils.java | 54 | ||||
-rw-r--r-- | src/lq/Strings.java | 12 | ||||
-rw-r--r-- | src/lq/Test.java | 7 |
5 files changed, 118 insertions, 0 deletions
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("&","&") + .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"; + } +} |