Package com.googlecode.objectify.impl.translate

Source Code of com.googlecode.objectify.impl.translate.EntityCreator

package com.googlecode.objectify.impl.translate;

import com.google.appengine.api.datastore.EmbeddedEntity;
import com.google.appengine.api.datastore.PropertyContainer;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.impl.Forge;
import com.googlecode.objectify.impl.KeyMetadata;
import com.googlecode.objectify.impl.Path;


/**
* <p>This version is for creating entity objects, which could be embedded or top-level.</p>
*
* @author Jeff Schnitzer <jeff@infohazard.org>
*/
public class EntityCreator<P> extends Creator<P>
{
  /**
   * Metadata associated with the key.
   */
  private KeyMetadata<P> keyMetadata;

  /**
   */
  public EntityCreator(Class<P> clazz, Forge forge, KeyMetadata<P> keyMetadata) {
    super(clazz, forge);

    // We should never have gotten this far in the registration process
    assert clazz.getAnnotation(Entity.class) != null;

    this.keyMetadata = keyMetadata;
  }

  /** */
  public KeyMetadata<P> getKeyMetadata() {
    return keyMetadata;
  }

  @Override
  public P load(PropertyContainer container, LoadContext ctx, Path path) throws SkipException {
    P pojo = construct(path);

    keyMetadata.setKey(pojo, container, ctx, path);

    return pojo;
  }

  @Override
  public PropertyContainer save(P pojo, boolean index, SaveContext ctx, Path path) throws SkipException {
    if (path.isRoot()) {
      return keyMetadata.initEntity(pojo);
    } else {
      EmbeddedEntity ent = new EmbeddedEntity();
      ent.setKey(keyMetadata.getRawKey(pojo));
      return ent;
    }
  }
}
TOP

Related Classes of com.googlecode.objectify.impl.translate.EntityCreator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.