summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKjetil Ørbekk <orbekk@pvv.ntnu.no>2010-09-02 21:39:31 -0400
committerKjetil Ørbekk <orbekk@pvv.ntnu.no>2010-09-02 21:39:31 -0400
commit70dd53cfac863b08de345f8f74e489da2aa7e4b9 (patch)
treecadced02268acc4e302e4dcfaae11db69dd46eca /src
parent693da35cb6db4ffd024b491e5c9851638883c93b (diff)
Add ImportQuotes script.
Imports quotes and votes from an xml dump from mysql.
Diffstat (limited to 'src')
-rw-r--r--src/lq/ImportQuotes.java120
-rw-r--r--src/lq/Quote.java22
-rw-r--r--src/lq/Vote.java13
3 files changed, 145 insertions, 10 deletions
diff --git a/src/lq/ImportQuotes.java b/src/lq/ImportQuotes.java
index 84cf577..7bfb5d8 100644
--- a/src/lq/ImportQuotes.java
+++ b/src/lq/ImportQuotes.java
@@ -6,10 +6,26 @@ import java.util.Scanner;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import javax.jdo.PersistenceManager;
+import org.jdom.Document;
+import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
+import org.jdom.xpath.XPath;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.logging.Logger;
public class ImportQuotes extends HttpServlet {
+ private static final Logger logger = Logger.getLogger(ImportQuotes.class.getName());
+ private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
+ private static final SimpleDateFormat timestampFormat =
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
@@ -21,11 +37,111 @@ public class ImportQuotes extends HttpServlet {
SAXBuilder builder = new SAXBuilder();
try {
- builder.build(path);
- resp.getWriter().println("Parsed successfully.");
+ Document document = builder.build(path);
+ Element root = document.getRootElement();
+ List quoteRows = XPath.selectNodes(root,
+ "//table_data[@name='quotes']/*");
+ List voteRows = XPath.selectNodes(root,
+ "//table_data[@name='votes']/*");
+
+ List<Quote> quotes = importQuotes(quoteRows);
+ List<Vote> votes = importVotes(quotes, voteRows);
+
+ PersistenceManager pm = PMF.get().getPersistenceManager();
+
+ try {
+ pm.makePersistentAll(quotes);
+ pm.makePersistentAll(votes);
+ }
+ finally {
+ pm.close();
+ }
} catch (Exception e) {
resp.getWriter().println("Failed to parse xml: " + e.toString());
+ e.printStackTrace();
+ }
+ }
+
+ private List<Quote> importQuotes(List quoteRows) throws Exception {
+ List<Quote> quotes = new ArrayList<Quote>();
+ for (Object row : quoteRows) {
+ @SuppressWarnings("unchecked")
+ Element elem = (Element) row;
+ quotes.add(importQuote(elem));
+ }
+ logger.info("Imported " + quotes.size() + " quotes");
+ return quotes;
+ }
+
+ private Quote importQuote(Element quoteRow) throws Exception {
+ Long id = Long.parseLong(getChildWithName("id", quoteRow));
+
+ String approvedString = getChildWithName("approved", quoteRow);
+ Boolean approved = approvedString.equals("1") ? true : false;
+
+ String name = getChildWithName("name", quoteRow);
+
+ String dateString = getChildWithName("date", quoteRow);
+ Date date = dateFormat.parse(dateString);
+
+ String content = getChildWithName("text", quoteRow);
+
+ String timeString = getChildWithName("time", quoteRow);
+ Date timestamp = timestampFormat.parse(timeString);
+
+ String ip = getChildWithName("ip", quoteRow);
+
+ Quote quote = new Quote(date, name, content, ip);
+ quote.setId(id);
+ quote.setTimestamp(timestamp);
+ quote.setApproved(approved);
+ return quote;
+ }
+
+ private List<Vote> importVotes(List<Quote> quotes, List voteRows) throws Exception {
+ List<Vote> votes = new ArrayList<Vote>();
+ for (Object row : voteRows) {
+ @SuppressWarnings("unchecked")
+ Element elem = (Element) row;
+ Vote vote = importVote(elem, quotes);
+ if (vote != null) {
+ votes.add(vote);
+ }
+ }
+ logger.info("Imported " + votes.size() + " votes");
+ return votes;
+ }
+
+ private Vote importVote(Element voteRow, List<Quote> quotes) throws Exception {
+ Long quoteId = Long.parseLong(getChildWithName("id", voteRow));
+ Quote quote = getQuoteWithId(quotes, quoteId);
+ if (quote == null) {
+ logger.warning("Could not find quote with id " + quoteId);
+ return null;
+ }
+ Long rating = Long.parseLong(getChildWithName("vote", voteRow));
+ Date timestamp = timestampFormat.parse(getChildWithName("time", voteRow));
+ String ip = getChildWithName("ip", voteRow);
+
+ Vote vote = new Vote(rating, ip);
+ vote.setTimestamp(timestamp);
+ quote.getVotes().add(vote);
+ return vote;
+ }
+
+ private String getChildWithName(String name, Element parent) throws Exception {
+ String xpath = "field[@name='" + name + "']";
+ Element child = (Element) XPath.selectSingleNode(parent, xpath);
+ return child.getValue();
+ }
+
+ private Quote getQuoteWithId(List<Quote> quotes, Long id) {
+ for (Quote quote : quotes) {
+ if (quote.getId().equals(id)) {
+ return quote;
+ }
}
+ throw new RuntimeException("Could not find quote.");
}
}
diff --git a/src/lq/Quote.java b/src/lq/Quote.java
index 7475ba0..b0595b7 100644
--- a/src/lq/Quote.java
+++ b/src/lq/Quote.java
@@ -1,6 +1,10 @@
package lq;
+import com.google.appengine.api.datastore.Key;
+import com.google.appengine.api.datastore.Text;
+import java.util.ArrayList;
import java.util.Date;
+import java.util.List;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
@@ -11,6 +15,9 @@ import javax.jdo.annotations.PrimaryKey;
public class Quote {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
+ private Key key;
+
+ @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
@@ -26,33 +33,40 @@ public class Quote {
private String author;
@Persistent
- private String content;
+ private Text content;
@Persistent
private String ip;
+ @Persistent
+ private List<Vote> votes;
+
public Quote(Date quoteDate, String author, String content, String ip) {
this.quoteDate = quoteDate;
this.author = author;
- this.content = content;
+ this.content = new Text(content);
this.ip = ip;
+ this.votes = new ArrayList<Vote>();
this.timestamp = new Date();
}
+ public Key getKey() { return key; }
public Long getId() { return id; }
public Date getTimestamp() { return timestamp; }
public Boolean getApproved() { return approved; }
public Date getQuoteDate() { return quoteDate; }
public String getAuthor() { return author; }
- public String getContent() { return content; }
+ public String getContent() { return content.getValue(); }
public String getIp() { return ip; }
+ public List<Vote> getVotes() { return votes; }
+ public void setKey(Key key) { this.key = key; }
public void setId(Long id) { this.id = id; }
public void setTimestamp(Date timestamp) { this.timestamp = timestamp; }
public void setApproved(Boolean approved) { this.approved = approved; }
public void setQuoteDate(Date quoteDate) { this.quoteDate = quoteDate; }
public void setAuthor(String author) { this.author = author; }
- public void setContent(String content) { this.content = content; }
+ public void setContent(String content) { this.content = new Text(content); }
public void setIp(String ip) { this.ip = ip; }
}
diff --git a/src/lq/Vote.java b/src/lq/Vote.java
index 0d3c9ac..9b9dca1 100644
--- a/src/lq/Vote.java
+++ b/src/lq/Vote.java
@@ -1,5 +1,6 @@
package lq;
+import com.google.appengine.api.datastore.Key;
import java.util.Date;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
@@ -11,29 +12,33 @@ import javax.jdo.annotations.PrimaryKey;
public class Vote {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
- private Long id;
+ private Key key;
@Persistent
private Quote quote;
@Persistent
+ private Long rating;
+
+ @Persistent
private Date timestamp;
@Persistent
private String ip;
- public Vote(Quote quote, String ip) {
+ public Vote(Long rating, String ip) {
this.quote = quote;
this.ip = ip;
+ this.rating = rating;
timestamp = new Date();
}
- public Long getId() { return id; }
+ public Key getKey() { return key; }
public Quote getQuote() { return quote; }
public Date getTimestamp() { return timestamp; }
public String getIp() { return ip; }
- public void setId(Long id) { this.id = id; }
+ public void setKey(Key key) { this.key = key; }
public void setQuote(Quote quote) { this.quote = quote; }
public void setTimestamp(Date timestamp) { this.timestamp = timestamp; }
public void setIp(String ip) { this.ip = ip; }