com.googlecode.objectify.impl.save.LeafFieldSaver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of objectify Show documentation
Show all versions of objectify Show documentation
*** THIS VERSION UPLOADED FOR USE WITH CEDAR-COMMON, TO AVOID DEPENDENCIES ON GOOGLE CODE-BASED MAVEN REPOSITORIES. *** The simplest convenient interface to the Google App Engine datastore
The newest version!
package com.googlecode.objectify.impl.save;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Entity;
import com.googlecode.objectify.annotation.Serialized;
import com.googlecode.objectify.impl.TypeUtils;
import com.googlecode.objectify.impl.conv.Conversions;
import com.googlecode.objectify.impl.conv.ConverterSaveContext;
/**
* Saver which knows how to save basic leaf values. Leaf values are things that
* go into the datastore: basic types or collections of basic types. Basically
* anything except an @Embedded.
*/
public class LeafFieldSaver extends FieldSaver implements ConverterSaveContext
{
/** */
Conversions conversions;
/** If true, we add values to a collection inside the entity */
boolean collectionize;
/** If true, we serialize the value into a Blob */
boolean serialize;
/** If true, null values are not saved. Leaf collection types are treated this way. */
boolean ignoreIfNull;
/**
* @param field must be a noncollection, nonarray type if collectionize is true
* @param ignoreClassIndexing see the FieldSaver javadocs
* @param collectionize when true will cause this leaf saver to persist simple basic
* types in a collection inside the entity property. If set is called multiple times,
* the collection will be appended to.
*/
public LeafFieldSaver(Conversions conv, Class examinedClass, Field field, boolean ignoreClassIndexing, boolean collectionize)
{
super(examinedClass, field, ignoreClassIndexing, collectionize);
this.conversions = conv;
this.collectionize = collectionize;
this.serialize = field.isAnnotationPresent(Serialized.class);
if (this.collectionize)
if (!this.serialize && TypeUtils.isArrayOrCollection(field.getType()))
throw new IllegalStateException("Cannot place array or collection properties inside @Embedded arrays or collections. The offending field is " + field);
// Don't save null arrays or collections
if (!this.serialize && TypeUtils.isArrayOrCollection(field.getType()))
this.ignoreIfNull = true;
}
/* (non-Javadoc)
* @see com.googlecode.objectify.impl.save.FieldSaver#saveValue(java.lang.Object, com.google.appengine.api.datastore.Entity, boolean)
*/
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void saveValue(Object value, Entity entity, Path path, boolean index)
{
value = this.prepareForSave(value);
// Maybe we are supposed to ignore this
if (value == null && this.ignoreIfNull)
return;
if (this.collectionize)
{
Collection