MCS 270 Spring 2014

23
MCS 270 Spring 2014 Object-Oriented Software Development

description

MCS 270 Spring 2014. Object-Oriented Software Development. MCS 270 Object-Oriented Software Development. Today ’ s schedule. GWT Persistence - JDO. MCS 270 Object-Oriented Software Development. Persistence. - PowerPoint PPT Presentation

Transcript of MCS 270 Spring 2014

Page 1: MCS 270 Spring 2014

MCS 270 Spring 2014

Object-Oriented Software Development

Page 2: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Today’s schedule

G

WT Persistence - JDO

MCS 270 Object-Oriented Software Development

Page 3: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Persistence

Persistence is the ability of an object to survive the lifecycle of the process in which it resides.

Objects that “die“ with the end of a process are called transient

Web Apps - storage is primarily on the server side. (Cookies can be set on client, but not guaranteed to persist)

CRUD - create, read, update and delete basics of persistence process

MCS 270 Object-Oriented Software Development

Page 4: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Google Web Toolkit Storage Options

Java Data Objects (JDO)A standardized way of storing objects

Create an object, store it, load it later

Java Persistence API (JPA)Another standardized way of storing objects

JPA is basically a refinement of JDO

As well supported in GAE as JDO

MCS 270 Object-Oriented Software Development

Page 5: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

JDO Overview

Java Data Objects (JDO) is a standard interface for

storing objects containing data into a database.

The standard defines interfaces for annotating Java

objects, retrieving objects with queries, and interacting

with a database using transactions

MCS 270 Object-Oriented Software Development

Page 6: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

JDO Overview

An application that uses the JDO interface can work

with different kinds of databases without using any

database-specific code, including relational databases,

hierarchical databases, and object databases.

Note: JDO is Not an object database

May use conventional RDBMS, OODB, etc- datastore

MCS 270 Object-Oriented Software Development

Page 7: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

JDO Overview

MCS 270 Object-Oriented Software Development

Page 8: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Eclipse and JDO: GWT Plug-In:

JDO and DataNucleus App Engine plugin JARs

(datanucleus) placed in app's war/WEB-INF/lib/

directory.

Build process performs post-compilation

"enhancement" step on compiled data classes (PostData)

to associate them with the JDO implementation.

MCS 270 Object-Oriented Software Development

Page 9: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

PersistenceManager

Web app interacts with JDO (Storage) using an instance of

the PersistenceManager class.

Use PersistenceManagerFactory to get PM.

PersistenceManagerFactory instance takes time to

initialize,

– an app should reuse a single static instance.

– to enforce this, an exception is thrown if the app

instantiates more than one PersistenceManagerFactory

MCS 270 Object-Oriented Software Development

Page 10: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Example – PMF.java

MCS 270 Object-Oriented Software Development

package edu.gac.mcs270.hvidsten.guslist.server;

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

Page 11: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Example – PMF class – used in GusListModel.java

MCS 270 Object-Oriented Software Development

public class GusListModel { static final PersistenceManagerFactory pmf = PMF.get();

. . .

}

Page 12: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Defining Data Classes for Storage

JDO is used to store plain Java data objects ("Plain Old

Java Objects" or "POJOs") in the datastore.

Each object made persistent with the PersistenceManager

becomes an entity in the datastore.

Code Annotations tell JDO how to store and recreate

instances of your data classes.

MCS 270 Object-Oriented Software Development

Page 13: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

MCS 270 Object-Oriented Software Development

.java

.class

.class

Java Compiler

JDOEnhancer

JDO MetaData(XML)

Byte code enhancement

Page 14: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Code Annotations

PersistenceCapable - The interface that all persistent

objects must implement. Annotated before class

definition.

Persistent – Designates that field of class will be stored

PrimaryKey – Unique identifier of class

Unowned – Designates an unowned 1-to-1 relationship

(i.e. class that is a member of another class, but will

not disappear when parent class disappears)

MCS 270 Object-Oriented Software Development

Page 15: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Example

@PersistenceCapable(identityType=IdentityType.APPLICATION)

public class PostData implements Serializable {

@PrimaryKey

@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)

// Needed for RPC communication of PostData class to server

@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true”)

private String id;

@Persistent

private String title="no title";

@Persistent

private String description="empty";

@Persistent

private double price=0.0;

MCS 270 Object-Oriented Software Development

Page 16: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Example (cont’d)

// Need to define the Seller and Buyer as "Unowned" child objects,

// as they do not disappear when PostData object is deleted.

@Persistent

@Unowned

private Seller seller;

@Persistent

@Unowned

private Buyer buyer;

…..

}

MCS 270 Object-Oriented Software Development

Page 17: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Enabling Group Transactions

Default JDO mode: one group stored (persisted) at one time.

PostData has a child Seller that is unowned – different group.

JDO configuration – can be set to allow more than one group

(up to 5) to be stored in a single transaction, a so-called

“cross-group” transaction.

Add to src/META-INF/jdoconfig.xml

<property

name="datanucleus.appengine.datastoreEnableXGTransac

tions" value="true" />

MCS 270 Object-Oriented Software Development

Page 18: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Storing PersistenceCapable Object (GusListModel)

public static void storePost(PostData post) {

PersistenceManager pm = pmf.getPersistenceManager();

// Transactions lock all records in a datastore and keep them locked

// until they are ready to commit their changes.

try {

pm.currentTransaction().begin();

pm.makePersistent(post);

pm.currentTransaction().commit();

} finally {

if (pm.currentTransaction().isActive())

pm.currentTransaction().rollback();

if (!pm.isClosed()) pm.close();

}}

MCS 270 Object-Oriented Software Development

Page 19: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Reading PersistenceCapable Object (GusListModel)

public static List<PostData> getPostData() {

PersistenceManager pm = pmf.getPersistenceManager();

Query query = pm.newQuery(PostData.class);

List<PostData> posts = (List<PostData>) query.execute();

// Child classes are loaded "lazily" - not until they are accessed.

// To make sure they are loaded before the PersistenceManager closes,

// we reference them here so they are forced to load.

for(PostData post: posts){

post.getSeller();

post.getBuyer(); }

return new ArrayList<PostData>(posts);

}

MCS 270 Object-Oriented Software Development

Page 20: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Inspecting the Datastore

After running an instance of the web app, go to to

http://localhost:8888/_ah/admin/

Browse through the data - The Full Key is shown

GWT pug-in (Eclipse) generates persistence files in war

directory

MCS 270 Object-Oriented Software Development

Page 21: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

MCS 270 Object-Oriented Software Development

Page 22: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

MCS 270 Object-Oriented Software Development

Page 23: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Demo

MCS 270 Object-Oriented Software Development