
package thobach;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;
public final class PMF {
private static final PersistenceManagerFactory pmfInstance =
JDOHelper.getPersistenceManagerFactory("transactions-optional");
private PMF() {}
public static PersistenceManagerFactory get() {
return pmfInstance;
}
}package thobach;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.*;
import thobach.PMF;
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Cocktail {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
private String name;
@NotPersistent
private static PersistenceManager pm;
public Long getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
/**
* Creates a new Cocktail object with the given name
*
* @param name
* String with the name of the Cocktail
*/
public Cocktail(String name) {
this.setName(name);
}
/**
* Returns the PersistenceManager for the Cocktail Class, creates one if not
* existent or closed
*
* @return PersistenceManager
*/
private static PersistenceManager getPersistenceManager() {
if (pm == null) {
pm = PMF.get().getPersistenceManager();
} else if (pm.isClosed()) {
pm = PMF.get().getPersistenceManager();
}
return pm;
}
/**
* Finds the Cocktail in the database by the given id
*
* @param id
* integer with the id of the Cocktail
* @return Cocktail object found in the database
*/
public static Cocktail find(int id) {
Cocktail cocktail = Cocktail.getPersistenceManager().getObjectById(
Cocktail.class, id);
return cocktail;
}
/**
* Makes the Cocktail object persistent
*
* @return boolean true if successful, false if not
*/
public boolean persist() {
boolean wasSuccessful;
try {
getPersistenceManager().makePersistent(this);
wasSuccessful = true;
} catch (Exception e) {
wasSuccessful = false;
} finally {
getPersistenceManager().close();
}
return wasSuccessful;
}
/**
* Deletes the Cocktail object from the persistent storage
*
* @return
*/
public boolean delete() {
boolean wasSuccessful;
try {
getPersistenceManager().deletePersistent(this);
wasSuccessful = true;
} catch (Exception e) {
wasSuccessful = false;
} finally {
getPersistenceManager().close();
}
return wasSuccessful;
}
/**
* Returns all Cocktails as a List from the persistent storage
*
* @return
*/
@SuppressWarnings("unchecked")
public static List findAll() {
String sqlFetchAll = "select from " + Cocktail.class.getName();
Query query = Cocktail.getPersistenceManager().newQuery(sqlFetchAll);
List cocktails = (List) query.execute();
return cocktails;
}
}
package thobach;
import java.util.List;
import thobach.Cocktail;
public class CocktailList {
private List<Cocktail> cocktails;
public CocktailList(List<Cocktail> cocktails) {
this.cocktails = cocktails;
}
public String display() {
String output = "<h2>Exisiting cocktails:</h2>" + "<ul>";
for (Cocktail c : cocktails) {
output += "<li>" + c.getName() + " <a href=\"?id=" + c.getId()
+ "&action=edit\">[edit]</a> <a href=\"?id="
+ c.getId() + "&action=delete\">[delete]</a></li>";
}
output += "</ul>";
return output;
}
}
Die Klasse CocktailForm stellt ein Formular zum Hinzufügen und Bearbeiten von Cocktails dar und hat folgenden Inhalt:package thobach;
import thobach.Cocktail;
public class CocktailForm {
private Cocktail editCocktail = null;
public CocktailForm(Cocktail editCocktail) {
if (editCocktail != null) {
this.editCocktail = editCocktail;
}
}
public String display() {
String output = "<form action=\"/crud\" method=\"post\">"
+ "<fieldset><legend>Cocktail</legend>"
+ "<label for=\"name\">Cocktail name:</label> "
+ "<input type=\"text\" name=\"name\" id=\"name\" value=\""
+ (editCocktail != null ? editCocktail.getName() : "")
+ "\" />"
+ (editCocktail != null ? "<input type=\"hidden\" name=\"id\" value=\""
+ editCocktail.getId() + "\" />"
: "")
+ (editCocktail == null ? "<input type=\"hidden\" name=\"action\" value=\"add\" />"
: "")
+ (editCocktail != null ? "<input type=\"hidden\" name=\"action\" value=\"edit\" />"
: "") + "<input type=\"submit\" value=\""
+ (editCocktail != null ? "edit" : "add") + " cocktail\" /> "
+ "<input type=\"submit\" name=\"action\" value=\"reset\" />"
+ "</fieldset>" + "</form>";
return output;
}
}
package thobach;
public class WebPage {
private String foot;
private String head;
private String content = "";
public WebPage(String title) {
head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">"
+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">" + "<head>"
+ "<title>" + title + "</title>" + "</head>" + "<body>";
foot = "</body>" + "</html>";
}
public String display() {
String output = head + content + foot;
return output;
}
public void addContent(String content) {
this.content += content;
}
}
package thobach;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import thobach.Cocktail;
import thobach.CocktailForm;
import thobach.CocktailList;
import thobach.WebPage;
@SuppressWarnings("serial")
public class CRUDServlet extends HttpServlet {
private String action;
private int id;
private String name;
private HttpServletRequest req;
private HttpServletResponse resp;
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
init(req, resp);
// delete cocktail by id
if (action.equals("delete") && id > 0) {
Cocktail deleteCocktail = Cocktail.find(id);
deleteCocktail.delete();
id = 0;
}
displayContent();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
init(req, resp);
// add a new cocktail
if (action.equals("add") && !name.equals("")) {
Cocktail cocktail = new Cocktail(name);
cocktail.persist();
}
// edit cocktail by id - set name
if (action.equals("edit") && id > 0 && name != null && !name.equals("")) {
Cocktail editCocktail = Cocktail.find(id);
editCocktail.setName(name);
editCocktail.persist();
editCocktail = null;
id = 0;
}
displayContent();
}
/**
* Sets the content type and inits all given parameters
*
* @param _req
* @param _resp
* @throws IOException
*/
private void init(HttpServletRequest _req, HttpServletResponse _resp)
throws IOException {
req = _req;
resp = _resp;
resp.setContentType("text/xml");
// set name
name = "";
if (req.getParameter("name") != null
&& !req.getParameter("name").equals("")) {
name = req.getParameter("name");
}
// set id
id = 0;
if (req.getParameter("id") != null) {
id = Integer.parseInt(req.getParameter("id"));
}
// set action
action = "";
if (req.getParameter("action") != null
&& !req.getParameter("action").equals("")) {
action = req.getParameter("action");
}
}
private void displayContent() throws IOException {
// create XHTML page
WebPage page = new WebPage("Cocktail Database");
page.addContent("<h1>Cocktail Database</h1>");
// add list with cocktails
List<Cocktail> cocktails = Cocktail.findAll();
CocktailList list = new CocktailList(cocktails);
page.addContent(list.display());
// add form to add and edit cocktails
Cocktail editCocktail = null;
if (id > 0) {
editCocktail = Cocktail.find(id);
}
CocktailForm form = new CocktailForm(editCocktail);
page.addContent(form.display());
// print generated XHTML page
PrintWriter out = resp.getWriter();
out.println(page.display());
}
}
Kategorien: crud app engine tutorial anleitung google