2012年12月28日金曜日

GAE/JのスタートガイドをJPA対応する。

GAE/JのスタートガイドをみたところJDOベースになっていたためJPAに対応してみた。

EMF.java
package guestbook;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class EMF {
 
 private static final EntityManagerFactory emfInstance =
   Persistence.createEntityManagerFactory("transactions-optional");
 
 private EMF(){}
 
 public static EntityManagerFactory get(){
  return emfInstance;
 }

}
Greeting.java
package guestbook;

import java.util.Date;

import javax.jdo.annotations.Persistent;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.User;

@Entity
public class Greeting {

 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private Key key;
 
 private User author;
 
 private String content;
 
 private Date date;
 
    public Greeting(User author, String content, Date date) {
        this.author = author;
        this.content = content;
        this.date = date;
    }

    public Key getKey() {
        return key;
    }

    public User getAuthor() {
        return author;
    }

    public String getContent() {
        return content;
    }

    public Date getDate() {
        return date;
    }

    public void setAuthor(User author) {
        this.author = author;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void setDate(Date date) {
        this.date = date;
    } 
 
}

SignServlet.java
package guestbook;

import java.io.IOException;
import java.util.Date;
import java.util.logging.Logger;

import javax.persistence.EntityManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class SignServlet extends HttpServlet{

 private static final Logger log = Logger.getLogger(SignServlet.class.getName());
 
 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException{
  UserService userService = UserServiceFactory.getUserService();
  User user = userService.getCurrentUser();
  String content = req.getParameter("content");
  Date date = new Date();
  Greeting greeting = new Greeting(user,content,date);
  EntityManager em = EMF.get().createEntityManager();
  try{
   em.persist(greeting);
  }finally{
   em.close();
  }
  resp.sendRedirect("/test.jsp");
 }
 
}
test.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="javax.persistence.EntityManager" %> 
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="guestbook.Greeting" %>
<%@ page import="guestbook.EMF" %>

<html>
  <body>
   <head>
    <link type="text/css" rel="stylesheet" href="/stylesheet/main.css" />
   </head>
<%
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    if (user != null) {
%>
<p>Hello, <%= user.getNickname() %>! (You can
<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out</a>.)</p>
<%
    } else {
%>
<p>Hello!!
<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign in</a>
to include your name with greetings you post.</p>
<%
    }
%>
<%
 EntityManager em = EMF.get().createEntityManager();
 String query = "select i from Greeting i";
 List<Greeting> greetings = (List<Greeting>)em.createQuery(query).getResultList();
 if(greetings.isEmpty()){
%>
<p>The guestbook has no messages.</p>
<%
 }else{
  
  for(Greeting g: greetings){
   if(g.getAuthor() == null){  
%>
<p>An anonymous person wrote:</p>
<%
   } else {
%>
<p><b><%= g.getAuthor().getNickname() %></b> wrote:</p>
<%
   }
%>
<blockquote><%= g.getContent() %></blockquote>
<%
  }
 }
 em.close();
%>
  <form action="/sign" method="post">
    <div><textarea name="content" rows="3" cols="60"></textarea></div>
    <div><input type="submit" value="Post Greeting" /></div>
  </form>
  </body>
</html>

0 件のコメント: