Package edu.dartmouth.cs.gae_sample.data

Source Code of edu.dartmouth.cs.gae_sample.data.ContactDatastore

package edu.dartmouth.cs.gae_sample.data;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.appengine.api.datastore.Transaction;

public class ContactDatastore {

  private static final Logger mLogger = Logger
      .getLogger(ContactDatastore.class.getName());
  private static final DatastoreService mDatastore = DatastoreServiceFactory
      .getDatastoreService();
 
  //DatastoreService ds=DatastoreServiceFactory.getDatastoreService();

  private static Key getParentKey() {
    return KeyFactory.createKey(Contact.CONTACT_PARENT_ENTITY_ID,
        Contact.CONTACT_PARENT_KEY_ID);
  }

  private static void createParentEntity() {
    Entity entity = new Entity(getParentKey());

    mDatastore.put(entity);
  }

  public static boolean add(Contact contact) {
    if (getContactById(contact.mId, null) != null) {
      mLogger.log(Level.INFO, "contact exists");
      return false;
    }

    Key parentKey = getParentKey();
    try {
      mDatastore.get(parentKey);
    } catch (Exception ex) {
      createParentEntity();
    }

    Entity entity = new Entity(Contact.CONTACT_ENTITY_ID, contact.mId,
        parentKey);
    entity.setProperty(Contact.FIELD_ID_ID, contact.mId);
    entity.setProperty(Contact.FIELD_ID_TEMA, contact.mTema);
    entity.setProperty(Contact.FIELD_ID_TITULO, contact.mTitulo);
    entity.setProperty(Contact.FIELD_ID_RESUMEN, contact.mResumen);
    entity.setProperty(Contact.FIELD_ID_AUTORIND, contact.mAutorind);
    entity.setProperty(Contact.FIELD_ID_AUTOR1, contact.mAutor1);
    entity.setProperty(Contact.FIELD_ID_AUTOR2, contact.mAutor2);
    entity.setProperty(Contact.FIELD_ID_AUTOR3, contact.mAutor3);
    entity.setProperty(Contact.FIELD_ID_AUTOR4, contact.mAutor4);
    entity.setProperty(Contact.FIELD_ID_AUTOR5, contact.mAutor5);
    entity.setProperty(Contact.FIELD_ID_AUTOR6, contact.mAutor6);
    entity.setProperty(Contact.FIELD_ID_AFILIACION, contact.mAfiliacion);
    entity.setProperty(Contact.FIELD_ID_UNAL, contact.mUnal);
    entity.setProperty(Contact.FIELD_ID_INSTITUCION, contact.mInstitucion);
    entity.setProperty(Contact.FIELD_ID_CARGO, contact.mCargo);
    entity.setProperty(Contact.FIELD_ID_DIA, contact.mDia);
    entity.setProperty(Contact.FIELD_ID_MES, contact.mMes);
    entity.setProperty(Contact.FIELD_ID_ANIO, contact.mAnio);
    entity.setProperty(Contact.FIELD_ID_LUGAR, contact.mLugar);
    entity.setProperty(Contact.FIELD_ID_FECHA, contact.mFecha);
    entity.setProperty(Contact.FIELD_ID_NOMBRE, contact.mNombre);
    entity.setProperty(Contact.FIELD_ID_EDITORIAL, contact.mEditorial);
    entity.setProperty(Contact.FIELD_ID_UBICACION, contact.mUbicacion);
    entity.setProperty(Contact.FIELD_ID_ACCESO, contact.mAcceso);
    entity.setProperty(Contact.FIELD_ID_IMPORTANCIA, contact.mImportancia);
    entity.setProperty(Contact.FIELD_ID_OBSERV, contact.mObserv);
    entity.setProperty(Contact.FIELD_ID_AMBIENTAL, contact.mAmbiental);
    entity.setProperty(Contact.FIELD_ID_BIENESTAR, contact.mBienestar);
    entity.setProperty(Contact.FIELD_ID_DEMOCRACIA, contact.mDemocracia);
    entity.setProperty(Contact.FIELD_ID_FORMACION, contact.mFormacion);
    entity.setProperty(Contact.FIELD_ID_INVEST, contact.mInvest);
    entity.setProperty(Contact.FIELD_ID_LEGADO, contact.mLegado);
    entity.setProperty(Contact.FIELD_ID_MEGAT, contact.mMegat);
    entity.setProperty(Contact.FIELD_ID_NALPUB, contact.mNalPub);
    entity.setProperty(Contact.FIELD_ID_TERRIT, contact.mTerrit);
    mDatastore.put(entity);

    return true;
  }

  public static boolean update(Contact contact) {
    Entity result = null;
    try {
      result = mDatastore.get(KeyFactory.createKey(getParentKey(),
          Contact.CONTACT_ENTITY_ID, contact.mId));
      result.setProperty(Contact.FIELD_ID_TEMA, contact.mTema);
      result.setProperty(Contact.FIELD_ID_TITULO,  contact.mTitulo);
      result.setProperty(Contact.FIELD_ID_RESUMEN, contact.mResumen);
      result.setProperty(Contact.FIELD_ID_AUTORIND,  contact.mAutorind);
      result.setProperty(Contact.FIELD_ID_AUTOR1, contact.mAutor1);
      result.setProperty(Contact.FIELD_ID_AUTOR2,  contact.mAutor2);
      result.setProperty(Contact.FIELD_ID_AUTOR3, contact.mAutor3);
      result.setProperty(Contact.FIELD_ID_AUTOR4,  contact.mAutor4);
      result.setProperty(Contact.FIELD_ID_AUTOR5, contact.mAutor5);
      result.setProperty(Contact.FIELD_ID_AUTOR6,  contact.mAutor6);
      result.setProperty(Contact.FIELD_ID_AFILIACION, contact.mAfiliacion);
      result.setProperty(Contact.FIELD_ID_UNAL,  contact.mUnal);
      result.setProperty(Contact.FIELD_ID_INSTITUCION, contact.mInstitucion);
      result.setProperty(Contact.FIELD_ID_CARGO,  contact.mCargo);
      result.setProperty(Contact.FIELD_ID_DIA, contact.mDia);
      result.setProperty(Contact.FIELD_ID_MES,  contact.mMes);
      result.setProperty(Contact.FIELD_ID_ANIO, contact.mAnio);
      result.setProperty(Contact.FIELD_ID_LUGAR,  contact.mLugar);
      result.setProperty(Contact.FIELD_ID_FECHA, contact.mFecha);
      result.setProperty(Contact.FIELD_ID_NOMBRE,  contact.mNombre);
      result.setProperty(Contact.FIELD_ID_EDITORIAL, contact.mEditorial);
      result.setProperty(Contact.FIELD_ID_UBICACION,  contact.mUbicacion);
      result.setProperty(Contact.FIELD_ID_ACCESO, contact.mAcceso);
      result.setProperty(Contact.FIELD_ID_IMPORTANCIA,  contact.mImportancia);
      result.setProperty(Contact.FIELD_ID_OBSERV, contact.mObserv);
      result.setProperty(Contact.FIELD_ID_AMBIENTAL,  contact.mAmbiental);
      result.setProperty(Contact.FIELD_ID_BIENESTAR, contact.mBienestar);
      result.setProperty(Contact.FIELD_ID_DEMOCRACIA,  contact.mDemocracia);
      result.setProperty(Contact.FIELD_ID_FORMACION, contact.mFormacion);
      result.setProperty(Contact.FIELD_ID_INVEST,  contact.mInvest);
      result.setProperty(Contact.FIELD_ID_LEGADO, contact.mLegado);
      result.setProperty(Contact.FIELD_ID_MEGAT,  contact.mMegat);
      result.setProperty(Contact.FIELD_ID_NALPUB, contact.mNalPub);
      result.setProperty(Contact.FIELD_ID_TERRIT,  contact.mTerrit);
           

      mDatastore.put(result);
    } catch (Exception ex) {

    }
    return false;
  }

  public static boolean delete(String id) {
    // you can also use name to get key, then use the key to delete the
    // entity from datastore directly
    // because name is also the entity's key
   
    // query
    Filter filter = new FilterPredicate(Contact.FIELD_ID_ID,
        FilterOperator.EQUAL, id);

    Query query = new Query(Contact.CONTACT_ENTITY_ID);
    query.setFilter(filter);

    // Use PreparedQuery interface to retrieve results
    PreparedQuery pq = mDatastore.prepare(query);

    Entity result = pq.asSingleEntity();
    boolean ret = false;
    if (result != null) {
      // delete
      mDatastore.delete(result.getKey());
      ret = true;
    }

    return ret;
  }

  // Recibimos el vector
  public static ArrayList<Contact> query(String id, String vec[]) {
    ArrayList<Contact> resultList = new ArrayList<Contact>();
    if (id != null && !id.equals("")) {     
      Contact contact = getContactById(id, null);
      if (contact != null) {
        resultList.add(contact);
      }
    } else {
      Query query = new Query(Contact.CONTACT_ENTITY_ID);
      // get every record from datastore, no filter
      query.setFilter(null);
      // set query's ancestor to get strong consistency
      query.setAncestor(getParentKey());

      PreparedQuery pq = mDatastore.prepare(query);
     
      int cont = 0;
      for (Entity entity : pq.asIterable()) {
        Contact contact = getContactFromEntity(entity);
       
        // Si vector da 1 guardamos si no no -> if(vector[i]==1 resultList.add(contact))
       
        if ((contact != null) /**&& (vec[cont] == "1") **/) {
          resultList.add(contact);
        }
        cont++;
      }
    }
    return resultList;
  }

  public static Contact getContactById(String id, Transaction txn) {
    Entity result = null;
    try {
      result = mDatastore.get(KeyFactory.createKey(getParentKey(),Contact.CONTACT_ENTITY_ID, id));
    } catch (Exception ex) {

    }

    return getContactFromEntity(result);
  }

  private static Contact getContactFromEntity(Entity entity) {
    if (entity == null) {
      return null;
    }

    return new Contact(
        (String) entity.getProperty(Contact.FIELD_ID_ID),
        (String) entity.getProperty(Contact.FIELD_ID_TEMA),
        (String) entity.getProperty(Contact.FIELD_ID_TITULO),
        (String) entity.getProperty(Contact.FIELD_ID_RESUMEN),
        (String) entity.getProperty(Contact.FIELD_ID_AUTORIND),
        (String) entity.getProperty(Contact.FIELD_ID_AUTOR1),
        (String) entity.getProperty(Contact.FIELD_ID_AUTOR2),
        (String) entity.getProperty(Contact.FIELD_ID_AUTOR3),
        (String) entity.getProperty(Contact.FIELD_ID_AUTOR4),
        (String) entity.getProperty(Contact.FIELD_ID_AUTOR5),
        (String) entity.getProperty(Contact.FIELD_ID_AUTOR6),
        (String) entity.getProperty(Contact.FIELD_ID_AFILIACION),
        (String) entity.getProperty(Contact.FIELD_ID_UNAL),
        (String) entity.getProperty(Contact.FIELD_ID_INSTITUCION),
        (String) entity.getProperty(Contact.FIELD_ID_CARGO),
        (String) entity.getProperty(Contact.FIELD_ID_DIA),
        (String) entity.getProperty(Contact.FIELD_ID_MES),
        (String) entity.getProperty(Contact.FIELD_ID_ANIO),
        (String) entity.getProperty(Contact.FIELD_ID_LUGAR),
        (String) entity.getProperty(Contact.FIELD_ID_FECHA),
        (String) entity.getProperty(Contact.FIELD_ID_NOMBRE),
        (String) entity.getProperty(Contact.FIELD_ID_EDITORIAL),
        (String) entity.getProperty(Contact.FIELD_ID_UBICACION),
        (String) entity.getProperty(Contact.FIELD_ID_ACCESO),
        (String) entity.getProperty(Contact.FIELD_ID_IMPORTANCIA),
        (String) entity.getProperty(Contact.FIELD_ID_OBSERV),
        (String) entity.getProperty(Contact.FIELD_ID_AMBIENTAL),
        (String) entity.getProperty(Contact.FIELD_ID_BIENESTAR),
        (String) entity.getProperty(Contact.FIELD_ID_DEMOCRACIA),
        (String) entity.getProperty(Contact.FIELD_ID_FORMACION),
        (String) entity.getProperty(Contact.FIELD_ID_INVEST),
        (String) entity.getProperty(Contact.FIELD_ID_LEGADO),
        (String) entity.getProperty(Contact.FIELD_ID_MEGAT),
        (String) entity.getProperty(Contact.FIELD_ID_NALPUB),
        (String) entity.getProperty(Contact.FIELD_ID_TERRIT)     
       
        );
  }
}
TOP

Related Classes of edu.dartmouth.cs.gae_sample.data.ContactDatastore

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.