Package adios.dao

Source Code of adios.dao.RecipeDao

package adios.dao;


import javax.persistence.Column;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import adios.model.Recipe;
import adios.model.UserRole;


@Component
public class RecipeDao {

    // Injected database connection:
    @PersistenceContext private EntityManager em;
    /**
     * persit a recipe
     * @param recipe
     * @return
     */
    @Transactional
    public boolean persist(Recipe recipe) {
      try{
            em.persist(recipe);
            return true;       
      }catch(Exception ex){
        return false;
      }
    }
   
    /**
     * return a recipe for a recipe Id
     * @param id
     * @return
     */
    @Transactional
    public Recipe getRecipeForId(int id){
      TypedQuery<Recipe> query = em.createQuery(
      "SELECT r FROM Recipe r WHERE r.recipeId = "+id+"", Recipe.class);
  return query.getSingleResult();
     
    }
   

    /**
     * return a recipe for a user Id and a recipe name
     * @param userId
     * @param name
     * @return
     */
    @Transactional
    public Recipe getRecipeForUserIdAndName(int userId, String name){
      TypedQuery<Recipe> query = em.createQuery(
      "SELECT r FROM Recipe r WHERE r.userId = "+userId+" AND r.name LIKE '"+name+"' ", Recipe.class);
  return query.getSingleResult();
     
    }
   
   
    /**
     * returns all the recipes for a user name
     * @param userId
     * @return
     */
    @Transactional
    public ArrayList<Recipe> getRecipeForUserIdAndName(int userId){
      TypedQuery<Recipe> query = em.createQuery(
      "SELECT r FROM Recipe r WHERE r.userId = "+userId+"", Recipe.class);
  return (ArrayList<Recipe>) query.getResultList();
     
    }
   
   
    /**
     * returns all the recipes that contains the parameter value in theirs name.
     * @param text
     * @return
     */
    @Transactional
    public ArrayList<Recipe> getRecipeForName(String text){
      TypedQuery<Recipe> query = em.createQuery(
          "SELECT r FROM Recipe r WHERE LOWER(r.name) LIKE '%"+text+"%'", Recipe.class);
  return (ArrayList<Recipe>) query.getResultList();
    }
   
   
   
//    SELECT a.au_lname, a.au_fname, t.title
//    FROM authors a INNER JOIN titleauthor ta
//       ON a.au_id = ta.au_id JOIN titles t
//       ON ta.title_id = t.title_id
//    WHERE t.type = 'trad_cook'
//    ORDER BY t.title ASC
   
//    SELECT a.email FROM usuarios a INNER JOIN eventos b on a.user_id = b.user_id INNER JOIN eventos_detalles c ON c.eventoid = b.eventoid WHERE c.evento_detallesID = 2481
    /**
     * returns all the recipes that contains the parameter value in theirs ingredients name.
     * @param text
     * @return
     */
    @Transactional
    public ArrayList<Recipe> getRecipeForIngredientName (String text){
      String q = "SELECT r "+
            "FROM Recipe r ,TableRecipeIngredient t, Ingredient i "+
            "WHERE r.recipeId = t.recipeId "+
            "AND i.ingredientId  t.ingredientId "+
            "AND i.name LIKE '%"+text+"%'";
      TypedQuery<Recipe> query = em.createQuery(q, Recipe.class);
//      em.
  return (ArrayList<Recipe>) query.getResultList();
    }
   
   
   
   

    /**
     * updates instructions for a recipe
     * @param r
     * @return
     */
    @Transactional
    public boolean update(Recipe r){
      String imageName = this.createImageName(r.getName(),r.getUserId());
        Recipe recipeDB = em.find(Recipe.class, r.getRecipeId());
        recipeDB.setInstructions(r.getInstructions());
        recipeDB.setName(r.getName());
        recipeDB.setDescription(r.getDescription());
        recipeDB.setExternalMedia(r.getExternalMedia());
        recipeDB.setImageName(imageName);
        em.persist(recipeDB);
  return true;
     
    }
   
   
    /**
     * updates a recipe
     * @param r
     * @return
     */
    @Transactional
    public int updateSimpleDataForRecipeForUserIdAndName(Recipe r){
      String imageName = this.createImageName(r.getName(),r.getUserId());
      Query query = em.createQuery(
          "UPDATE Recipe r "+
          "SET r.userId = "+r.getUserId()+","+
          "r.name = '"+r.getName()+"',"+
          "r.description = '"+r.getDescription()+"',"+
          "r.externalMedia = '"+r.getExternalMedia()+"',"+
          "r.imageName = '"+imageName+"' "+
//          "r.instructions = '"+r.getInstructions()+"' "+   
          "WHERE r.recipeId = "+r.getRecipeId()+"");

  return query.executeUpdate();
     
    }

   
    /**
     * returns the last 5 recipes, 5 will be hardoded, it should be moved to a properties files or a parameter
     * @param pageSize
     * @return
     */
    @Transactional
    public ArrayList<Recipe> getLastRecipes(int pageSize){

      TypedQuery<Recipe> query = em.createQuery(
              "SELECT r FROM Recipe r ORDER BY r.recipeId DESC", Recipe.class).setMaxResults(5);
     
      ArrayList<Recipe> rList=(ArrayList)query.getResultList();
      return rList;
    }

    /**
     * generates the name for the image of a recipe
     * @param name
     * @param userId
     * @return
     */
    public String createImageName(String name, int userId){
      String imageName = userId+name.replace(" ","");
    imageName = imageName+".jpg";
      return imageName;
    }
   
    // Retrieves all the guests:
//    public UserRole getRoleForRoleId(UserRole role) {
//      TypedQuery<UserRole> query = em.createQuery(
//            "SELECT r FROM UserRole r WHERE r.roleId = "+role.getRoleId()+"", UserRole.class);
//      return query.getSingleResult();
//    }
}
TOP

Related Classes of adios.dao.RecipeDao

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.