Tuesday, April 14, 2009

App Engine Tutorial : Creating the Beer Radar Server component

App Engine App #1.0 - Server Component for Android Application Beer Radar.

I created the Beer Radar Android Application last February 2009, and I was looking for a suitable Java server application. I don't know when it was announced, but I just heard about the Google App Engine support for Java last week (April 10, 2009).

Here's the basic App Engine Application which I'll connect the Beer Radar app soon. :) http://beer.androidph.com/

I quickly signed up to see what is it all about. So, here's what I learned so far.

Here are the steps to create a web app using Google App Engine with Java Lanuage Support.

1. Sign-up for an App Engine Account http://appengine.google.com/ you should click link for Java then click the sign-up button (I waited for 2 days for the confirmation). I don't have a screenshot for it, but its easy to spot.

2. Download the Google App Engine SDK http://code.google.com/appengine/downloads.html

3. Install the App Engine Eclipse Plugin
From the Eclipse Menu, go to HELP > Software Update...
Click the Available Software Tab
Click Add Site, type http://dl.google.com/eclipse/plugin/3.4
Then Install
See http://code.google.com/appengine/docs/java/tools/eclipse.html for more details.
Restart Eclipse


4. Click the New Web Application Icon as shown below


5. Type in the Project name and package. I unchecked the GWT option since I'm not going to use it.


6. Then click Finish. Everything is created for you, even the libraries that you need for your application.


7. Run the server by right-clicking on the project, select Debug As > Web Application


8. If you have tomcat running by default, you'd probably get this error message
WARNING: failed SelectChannelConnector@127.0.0.1:8080
java.net.BindException: Address already in use: bind


9. Fix this by going to the Debug Configurations, then on the Main Tab, set a new port or click "Automatically select an unused port".





10. Click Debug.

11. Now open your browser to http://localhost:8989.

12. Now you can test your application.


13. Once you're done with you application and you already got the Google confirmation message, you can now create an application.


14. Type in a unique Application Identifier and the Application Title. This will be used in deploying your application.


15. Once everything is done, you can now deploy by clicking the Deploy App Engine Project icon.


16. You will need to click the App Engine project settings to enter your Application Identifier.


17. Enter you Application ID then type in a version then click OK.


18. Enter your Google App Engine Account Email and Password and that is done.


20. Here's the app http://beerradar.appspot.com/ I deployed, to be used as the server component of the Beer Radar project.

21. AND YES, I had to put in the ads. ;)

What I really like about the app engine is the JDO support.

Here's the code for my Persistent Object
Beer.java

package com.androidph.beerserver;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Beer {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;

@Persistent
private String name;

@Persistent
private Double price;

@Persistent
private Date lastCallTime;

@Persistent
private String promotions;

@Persistent
private String address;

@Persistent
private Double longitude;

@Persistent
private Double latitude;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Double getPrice() {
return price;
}

public void setPrice(Double price) {
this.price = price;
}

public Date getLastCallTime() {
return lastCallTime;
}

public void setLastCallTime(Date lastCallTime) {
this.lastCallTime = lastCallTime;
}

private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

public String getLastCallTimeString() {
if(lastCallTime != null) {
return sdf.format(lastCallTime);
} else {
return "-";
}
}

public String getPromotions() {
return promotions;
}

public void setPromotions(String promotions) {
this.promotions = promotions;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public Double getLongitude() {
return longitude;
}

public void setLongitude(Double longitude) {
this.longitude = longitude;
}

public Double getLatitude() {
return latitude;
}

public void setLatitude(Double latitude) {
this.latitude = latitude;
}

public String toString() {
return id + " | " + name + " | " + price + " | " + lastCallTime + " | " + promotions + " | " + address + " | " + longitude + " | " + latitude;
}

public byte[] convertToStream() {
return null;
}

public void fromStream(byte[] data) {

}
}

Here's the code to get a Persistence Manager. I got this from the GuestBook demo included in the AppEngine SDK

PMF.java

package com.androidph.beerserver;

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;
}
}


Here are code snippets on how to use the Persistence Manager.

Persisting an Object

PersistenceManager persistenceManager = PMF.get()
.getPersistenceManager();
Beer beer = beerValidator.getBeer(name, address, price,
lastCallTime, promotions, longitude, latitude);
persistenceManager.makePersistent(beer);

Retrieving Data

String query = "select from " + Beer.class.getName()
+ " order by name asc";
List beerLocations = (List) persistenceManager
.newQuery(query).execute();
for (Beer beer : beerLocations) {
beerServletData.addLog("beer.toString() : " + beer.toString());
}

Email

java.padawan@androidph.com