Home‎ > ‎Server‎ > ‎App Engine‎ > ‎

JDO

Some snippets I use:

Example 1

This is using GWT RPC to get data and send data. I believe this is second best to RequestFactory, mainly due to RPC can create Type explosion. Request factory can compare difference and only sends whats needed for update too. I use an owned collection LinkedHashSet here. It is similar to one to many.
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashSet;

import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.gonevertical.client.layout.school.user.learingplan.classes.ClassData;
import com.gonevertical.client.layout.school.user.learingplan.classhours.ClassProgressData;
import com.gonevertical.server.ServerPersistence;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable
public class ClassJdo {
 
 
@NotPersistent
 
private ServerPersistence sp;

 
@PrimaryKey
 
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
 
private Key key;

 
@Persistent
 
private Integer index;

 
@Persistent
 
private Date dateCreated;

 
@Persistent
 
private Integer semester;

 
@Persistent
 
private String name;

 
@Persistent
 
private Integer hoursHome;

 
@Persistent
 
private Integer hoursSchool;

 
@Persistent(defaultFetchGroup = "true", dependentElement = "true")
 
private LinkedHashSet<ClassProgressJdo> progress;

 
/**
   * init
   */

 
public ClassJdo(ServerPersistence sp) {
   
this.sp = sp;
 
}
 
public ClassJdo(ServerPersistence sp, Key parentKey, ClassData data) {
   
this.sp = sp;
   
if (data == null) {
     
return;
   
}
    setId
(parentKey, data.getId());
    index
= data.getIndex();
    semester
= data.getSemester();
    name
= data.getName();
    hoursHome
= data.getHoursHome();
    hoursSchool
= data.getHoursSchool();
    setProgress
(data.getProgress());
   
//System.out.println("SchoolPlanClassJdo(): this=" + this);
 
}

 
public ClassData getData() {
   
ClassData d = new ClassData();
    d
.setId(getId());
    d
.setIndex(index);
    d
.setSemester(semester);
    d
.setName(name);
    d
.setHoursHome(hoursHome);
    d
.setHoursSchool(hoursSchool);
    d
.setProgress(getProgress());
   
//System.out.println("SchoolPlanClassJdo.getData(): this=" + this);
   
return d;
 
}

 
private void setId(Key parentKey, Long id) {
   
if (id == null || id == 0 || parentKey == null) {
      dateCreated
= new Date();
     
return;
   
}
    key
= KeyFactory.createKey(parentKey, ClassJdo.class.getSimpleName(), id);
 
}
 
private Long getId() {
   
Long id = null;
   
if (key != null) {
      id
= key.getId();
   
}
   
return id;
 
}
 
 
private LinkedHashSet<ClassProgressData> getProgress() {
   
if (progress == null || progress.size() == 0) {
     
return null;
   
}
   
LinkedHashSet<ClassProgressData> r = new LinkedHashSet<ClassProgressData>();
   
Iterator<ClassProgressJdo> itr = progress.iterator();
   
while(itr.hasNext()) {
     
ClassProgressJdo j = itr.next();
     
if (j != null) {
        r
.add(j.getData());
     
}
   
}
   
return r;
 
}

 
private void setProgress(LinkedHashSet<ClassProgressData> progress) {
   
if (progress == null || progress.size() == 0) {
     
this.progress = null;
     
return;
   
}
   
LinkedHashSet<ClassProgressJdo> lhs = new LinkedHashSet<ClassProgressJdo>();
   
Iterator<ClassProgressData> itr = progress.iterator();
   
while(itr.hasNext()) {
     
ClassProgressData d = itr.next();
     
ClassProgressJdo j = new ClassProgressJdo(key, d);
      lhs
.add(j);
   
}
   
this.progress = lhs;
 
}

 
public boolean deletePlanClass(Long parentId, Long id) {
   
if (parentId == null || id == null) {
     
return true;
   
}
   
boolean success = false;
   
PersistenceManager pm = sp.getPersistenceManager();
   
Transaction tx = pm.currentTransaction();
   
try {
     
Key keyParent = KeyFactory.createKey(LearningPlanJdo.class.getSimpleName(), parentId);
     
Key key = KeyFactory.createKey(keyParent, ClassJdo.class.getSimpleName(), id);
     
ClassJdo j = pm.getObjectById(ClassJdo.class, key);
     
if (sp.getIsSchoolAdmin() == true) {
        tx
.begin();
        pm
.deletePersistent(j);
        tx
.commit();
     
}
      success
= true;
   
} catch (Exception e) {
      success
= false;
   
}  finally {
     
if (tx.isActive()) {
        tx
.rollback();
     
}
      pm
.close();
   
}
   
return success;
 
}
 
 
public String toString() {
   
String s = "";
    s
+= "key=" + key + " ";
    s
+= "index=" + index + " ";
    s
+= "semester=" + semester + " ";
    s
+= "name=" + name + " ";
    s
+= "hoursHome=" + hoursHome + " ";
    s
+= "hoursSchool=" + hoursSchool + " ";
    s
+= "progress={ " + progress + " } ";
   
return s;
 
}
 
}




Comments