Created
October 16, 2009 14:56
-
-
Save ArtemGr/211846 to your computer and use it in GitHub Desktop.
GAE JDO serialized versus JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.{Long => JLong, Integer => JInteger, Double => JDouble} | |
import java.util.{Map => JMap, HashMap => JHashMap, List => JList, LinkedList => JLinkedList} | |
import javax.jdo.annotations._ | |
import scala.collection.jcl.Conversions._ | |
/** | |
* This class tracks all the ratings of a single doctor | |
* (the doctor is identified by country-region-firstName-lastName). | |
*/ | |
@PersistenceCapable{val identityType = IdentityType.APPLICATION, val detachable = "true"} | |
class Doctor { | |
def NULL = null // Workaround for NUCENHANCER-34. | |
// ... | |
/** Here we keep track of doctor's rating<br> | |
* and keep the copy of some of the rating's data close for recalculations.<br> | |
* problem -> ratingKey -> fieldName (actLevStart|actLevEnd) -> fieldValue */ | |
@Persistent{val serialized = "true"} | |
var ratings: JHashMap[String, JHashMap[JLong, DenormalizedRating]] = NULL | |
// Will replace "ratings" if everything works. See | |
// http://groups.google.ru/group/google-appengine-java/browse_frm/thread/747ceed8396c0ed8/9955970bd4c37ec6#9955970bd4c37ec6 | |
@Persistent private[this] var jsonRatings: String = NULL | |
/** Access the "ratings" field, which is JSON-serialized. */ | |
def getRatings: JMap[String, JMap[JLong, DenormalizedRating]] = { | |
import org.codehaus.jackson.`type`.TypeReference | |
JDO.JACKSON_MAPPER.readValue (if (jsonRatings ne null) jsonRatings else "{}", | |
new TypeReference[JMap[String, JMap[JLong, DenormalizedRating]]] {}) | |
} | |
def setRatings (ratings: JMap[String, JMap[JLong, DenormalizedRating]]): Unit = { | |
val sw = new java.io.StringWriter | |
JDO.JACKSON_MAPPER.writeValue (sw, ratings) | |
jsonRatings = sw.toString | |
} | |
// ... | |
} | |
// ... | |
/** Copy the rating's data into the Doctor. */ | |
def updateDoctor (): Unit = if ((doctor ne null) && (actLevStart ne null) && (actLevEnd ne null)) { | |
// Old version using serialized map, updates don't work! | |
if (doctor.ratings eq null) doctor.ratings = new JHashMap | |
if (!doctor.ratings.containsKey (problem)) doctor.ratings.put (problem, new JHashMap) | |
doctor.ratings.get (problem) .put (id.getId, DenormalizedRating (actLevStart.intValue, actLevEnd.intValue)) | |
doctor.ratings = doctor.ratings.clone | |
.asInstanceOf[JHashMap[String, JHashMap[JLong, DenormalizedRating]]] | |
// New version using JSON serialization. | |
val ratings = doctor.getRatings | |
if (!ratings.containsKey (problem)) ratings.put (problem, new JHashMap) | |
ratings.get (problem) .put (id.getId, DenormalizedRating (actLevStart.intValue, actLevEnd.intValue)) | |
doctor.setRatings (ratings) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment