summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn H. Anthony <johnhant@gmail.com>2010-09-04 15:05:28 +0200
committerJohn H. Anthony <johnhant@gmail.com>2010-09-04 15:05:28 +0200
commitb044930b49e51ffd25eb1c1ff49c987d7d2b0ea4 (patch)
treee08fec1b9f85a862aa40e5105f73b41c6fbe1ae5
parentc35a035d25b06bc7ad403ec82dd4495056e599d7 (diff)
implemented voting
Voting now appears to work, using the AddVote servlet. The datastore update is not committed as a transaction yet; the quote and its votes has to be in the same entity group for transactions to work.
-rw-r--r--html/quotes.jsp4
-rw-r--r--src/WEB-INF/web.xml10
-rw-r--r--src/lq/AddVote.java65
-rw-r--r--src/lq/Printer.java2
4 files changed, 78 insertions, 3 deletions
diff --git a/html/quotes.jsp b/html/quotes.jsp
index 0c2fd17..3813350 100644
--- a/html/quotes.jsp
+++ b/html/quotes.jsp
@@ -33,9 +33,9 @@ hr {
<script>
-function ajaxvote(id, value) {
+function vote(id, value) {
var http = new XMLHttpRequest();
- http.open("GET","ajaxvote.jsp?id="+id+"&vote="+value);
+ http.open("GET","/vote?id="+id+"&vote="+value);
http.onreadystatechange=function() {
if(http.readyState==4) {
document.getElementById("v"+id).innerHTML = http.responseText;
diff --git a/src/WEB-INF/web.xml b/src/WEB-INF/web.xml
index 432d10a..1fa3dab 100644
--- a/src/WEB-INF/web.xml
+++ b/src/WEB-INF/web.xml
@@ -17,6 +17,16 @@
</servlet-mapping>
<servlet>
+ <servlet-name>add_vote</servlet-name>
+ <servlet-class>lq.AddVote</servlet-class>
+ </servlet>
+
+ <servlet-mapping>
+ <servlet-name>add_vote</servlet-name>
+ <url-pattern>/vote</url-pattern>
+ </servlet-mapping>
+
+ <servlet>
<servlet-name>importquotes</servlet-name>
<servlet-class>lq.ImportQuotes</servlet-class>
</servlet>
diff --git a/src/lq/AddVote.java b/src/lq/AddVote.java
new file mode 100644
index 0000000..e9a442a
--- /dev/null
+++ b/src/lq/AddVote.java
@@ -0,0 +1,65 @@
+package lq;
+
+import java.io.IOException;
+import javax.jdo.PersistenceManager;
+import javax.jdo.Transaction;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class AddVote extends HttpServlet {
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+ throws IOException {
+
+ resp.setContentType("text/html");
+
+ Long quoteId;
+ Long rating;
+ String ip;
+
+ try {
+ quoteId = Long.parseLong(req.getParameter("id"));
+ rating = Long.parseLong(req.getParameter("vote"));
+ ip = req.getRemoteAddr();
+
+ String forward = req.getHeader("X-Forwarded-For");
+ if (!Strings.nullOrEmpty(forward)) {
+ ip = ip + " (" + forward + ")";
+ }
+ } catch (NumberFormatException e) {
+ resp.getWriter().println("invalid arguments");
+ return;
+ }
+
+ if (rating.longValue() < 0 || rating.longValue() > 5) {
+ resp.getWriter().println("nice try");
+ return;
+ }
+
+ Quote quote = QuoteUtil.getQuoteWithId(quoteId);
+ if (quote == null) {
+ resp.getWriter().println("failed to retrieve quote");
+ return;
+ }
+
+ Vote vote = new Vote(quoteId, rating, ip);
+ QuoteUtil.addVote(quote, vote);
+
+ PersistenceManager pm = PMF.get().getPersistenceManager();
+ // Transaction tx = pm.currentTransaction();
+ try {
+ // tx.begin();
+ pm.makePersistent(quote);
+ pm.makePersistent(vote);
+ // tx.commit();
+ } finally {
+ // if (tx.isActive()) {
+ // tx.rollback();
+ // }
+ pm.close();
+ }
+
+ resp.getWriter().println(QuoteUtil.formatScore(quote));
+ }
+}
diff --git a/src/lq/Printer.java b/src/lq/Printer.java
index 573ab5d..beee449 100644
--- a/src/lq/Printer.java
+++ b/src/lq/Printer.java
@@ -24,7 +24,7 @@ public class Printer {
out.println(QuoteUtil.formatScore(quote));
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("<a href=\"javascript:vote(" + quote.getId() + ","+nv+")\">"+nv+"</a> ");
out.println("</font> </span>");
out.println("<br> <br>");