Package com.google.appengine.codelab

Source Code of com.google.appengine.codelab.Localidade

/**
* Copyright 2011 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.appengine.codelab;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PropertyProjection;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;

/**
* This class defines the methods for basic operations of create, update & retrieve
* for Localidade entity
*
* @author
*
*/
public class Localidade {

  /**
   * Checks if the entity is existing and if it is not, it creates the entity
   * else it updates the entity
   *
   * @param localidade_latitude
   *          : latitude for the Localidade
   * @param localidade_longitude
   *          : latitude of the Localidade
   * @param localidade_address
   *          : address of the Localidade
   * @param localidade_city
   *          : city of Localidade
   * @param localidade_state
   *          : state phone number
   * @param localidade_country
   *          : country of Localidade        
   * @param localidade_zip
   *          : email id of Localidade
   */
  public static void createOrUpdateLocalidade(String localidade_id,  Float localidade_latitude, Float localidade_longitude,
      String localidade_address, String localidade_city, String localidade_state, String localidade_country, String localidade_zip) {
   
    Entity localidade = getSingleLocalidade(localidade_id);
    if (localidade == null) {
      localidade = new Entity("Localidade", localidade_id);
      localidade.setProperty("localidade_id", localidade_id);
      localidade.setProperty("localidade_latitude", localidade_latitude);
      localidade.setProperty("localidade_longitude", localidade_longitude);
      localidade.setProperty("localidade_address", localidade_address);
      localidade.setProperty("localidade_city", localidade_city);
      localidade.setProperty("localidade_state", localidade_state);
      localidade.setProperty("localidade_country", localidade_country);
      localidade.setProperty("localidade_zip", localidade_zip);
   
    } else {
      if (localidade_latitude != null && !"".equals(localidade_latitude)) {
        localidade.setProperty("localidade_latitude", localidade_latitude);
      }
      if (localidade_latitude != null && !"".equals(localidade_latitude)) {
        localidade.setProperty("localidade_longitude", localidade_longitude);
      }
      if (localidade_address != null && !"".equals(localidade_address)) {
        localidade.setProperty("localidade_address", localidade_address);
      }
      if (localidade_city != null && !"".equals(localidade_city)) {
        localidade.setProperty("localidade_city", localidade_city);
      }
      if (localidade_state != null && !"".equals(localidade_state)) {
        localidade.setProperty("localidade_state", localidade_state);
      }
      if (localidade_country != null && !"".equals(localidade_country)) {
        localidade.setProperty("localidade_country", localidade_country);
      }
      if (localidade_zip != null && !"".equals(localidade_zip)) {
        localidade.setProperty("localidade_zip", localidade_zip);
      }
    }
    Util.persistEntity(localidade);
  }
 
  /**
   * List all the localidades available
   *
   * @return an iterable list with all the localidades
   */
  public static Iterable<Entity> getAllLocalidades() {
    Iterable<Entity> entities = Util.listEntities("Localidade", null, null);
    return entities;
  }

  /**
   * Searches for a Localidade and returns the entity as an iterable The search is
   * performed by creating a query and searching for the attribute
   *
   * @param localidade
   *          : zip of the localidade
   * @return iterable with the localidades searched for
   */
  public static Iterable<Entity> getLocalidade(String localidade_id) {
    Iterable<Entity> entities = Util.listEntities("Localidade", "localidade_id",localidade_id);
    return entities;
  }

  public static Iterable<Entity> listarEstados( String pais ){
    if( pais == null || pais.isEmpty() ){
      return null;
    }
   
    Query query = new Query("Localidade");
    query.addFilter("localidade_country", FilterOperator.EQUAL, pais);
    query.addProjection(new PropertyProjection("localidade_state", String.class));
    query.setDistinct(true);

    return DatastoreServiceFactory.getDatastoreService().prepare(query).asIterable();
  }

  public static Iterable<Entity> listarCidades( String estado ){
    if( estado == null || estado.isEmpty() ){
      return null;
    }
   
    Query query = new Query("Localidade");
    query.addFilter("localidade_state", FilterOperator.EQUAL, estado);
    query.addProjection(new PropertyProjection("localidade_city", String.class));
    query.setDistinct(true);

    return DatastoreServiceFactory.getDatastoreService().prepare(query).asIterable();
  }

  public static List<String> listarPaises()
  {
      Query query = new Query("Localidade");
      query.addProjection(new PropertyProjection("localidade_country", String.class));
      query.setDistinct(true);

      Iterable<Entity> iterable = DatastoreServiceFactory.getDatastoreService().prepare(query).asIterable();
      List<String> paises = new ArrayList<String>();
     
      for( Entity entity : iterable ){
        paises.add( (String) entity.getProperty("localidade_country"));
      }
     
      return paises;
  }
 
 
  /**
   * Searches for a localidade and returns the entity as an iterable The search is
   * key based instead of query
   *
   * @param localidade_id
   *          : id of the localidade
   * @return the entity with the localidade as key
   */
  public static Entity getSingleLocalidade(String localidade_id) {
      Iterable<Entity> results = Util.listEntities("Localidade", "localidade_id", localidade_id);
      List<Entity> entity = new ArrayList<Entity>();
      for(Entity e : results)
        if(e!=null)
          entity.add(e);
        if (!entity.isEmpty()) {
          return (Entity)entity.remove(0);
        }
      return null;
}
 
    //Metodo Adicionado por Anderson - US13
   public static Iterable<Entity> getLocalidadePaisEstadoCidade(String localidade_country,String localidade_state, String localidade_city) {
        Iterable<Entity> entities = Util.listEntitiesLocalidadePaisEstadoCidade(localidade_country, localidade_state, localidade_city);
       
        return entities;
     }
}
TOP

Related Classes of com.google.appengine.codelab.Localidade

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.