*
*/
public void createBlogPost(String authorEmail, String title, String blogText,
List<String> tags) {
AuthorImpl a = getAuthor(authorEmail);
if(title == null) title = "";
Date publishDate = new Date(System.currentTimeMillis());
if(tags == null) tags = new ArrayList<String>();
try {
Connection connection = dataSource.getConnection();
// let's find the max id from the blogentry table
String sql = "SELECT max(id) FROM BLOGENTRY";
PreparedStatement ppsm = connection.prepareStatement(sql);
ResultSet rs = ppsm.executeQuery();
// we only expect to have one row returned
rs.next();
long max_id = rs.getLong(1);
ppsm.close();
long post_id = max_id + 1;
sql = "INSERT INTO BLOGENTRY VALUES (?,?,?,?,?,?)";
ppsm = connection.prepareStatement(sql);
ppsm.setLong(1, post_id);
ppsm.setString(2, blogText);
if (publishDate != null)
ppsm
.setDate(3, new java.sql.Date(publishDate
.getTime()));
else
ppsm.setDate(3, null);
ppsm.setString(4, title);
ppsm.setDate(5, null);
ppsm.setString(6, a.getEmail());
int rows = ppsm.executeUpdate();
if (rows != 1)
throw new IllegalArgumentException(
"The blog entry record cannot be inserted: "
+ blogText);
ppsm.close();
// insert a row in the relationship table
sql = "INSERT INTO Author_BlogEntry VALUES (?,?)";
ppsm = connection.prepareStatement(sql);
ppsm.setString(1, a.getEmail());
ppsm.setLong(2, post_id);
rows = ppsm.executeUpdate();
ppsm.close();
connection.close();
if (rows != 1)
throw new IllegalArgumentException(
"The Author_BlogEntry record cannot be inserted: "
+ a.getEmail() + " , " + post_id);
} catch (SQLException sqle) {
sqle.printStackTrace();
}