package br.estacio.contatos.server.assembler;
import br.estacio.contatos.shared.Contato;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.KeyFactory;
public class ContatoEntityObjectAssembler implements EntityObjectAssembler<Contato> {
private static final String CONTATO_KIND = "Contato";
private static final String TELEFONE_PROPERTY = "telefone";
private static final String EMAIL_PROPERTY = "email";
private static final String NOME_PROPERTY = "nome";
@Override
public Entity toEntity(Contato c) {
Entity e = new Entity(CONTATO_KIND);
if(c.getId() != null) {
KeyFactory.createKey(CONTATO_KIND, c.getId());
}
e.setProperty(NOME_PROPERTY, c.getNome());
e.setProperty(EMAIL_PROPERTY, c.getEmail());
e.setProperty(TELEFONE_PROPERTY, c.getTelefone());
return e;
}
@Override
public Contato toObject(Entity e) {
Contato c = new Contato();
c.setNome(getStringProperty(e, NOME_PROPERTY));
c.setEmail(getStringProperty(e, EMAIL_PROPERTY));
c.setTelefone(getStringProperty(e, TELEFONE_PROPERTY));
c.setId(e.getKey().getId());
return c;
}
private String getStringProperty(Entity entity, String property) {
return String.valueOf(entity.getProperty(property));
}
}