package adios.service;
import java.util.ArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import adios.dao.RecipeDao;
import adios.model.Recipe;
@Service("RecipeService")
public class RecipeService {
protected final Log Log = LogFactory.getLog(getClass());
@Autowired
private RecipeDao recipeDao;
/**
* returns a recipe for a recipe Id
* @param id
* @return
*/
public Recipe getRecipe(int id){
Recipe r=null;
try{
r = recipeDao.getRecipeForId(id);
}catch(Exception ex){
Log.info("ERROR ::: getting Recipe!!!");
return null;
}
return r;
}
/**
* return the last recipes
* @param pageSize
* @return
*/
public ArrayList<Recipe> getLastRecipes(int pageSize){
ArrayList<Recipe> rList=null;
try{
rList=recipeDao.getLastRecipes(pageSize);
}catch(Exception ex){
Log.info("ERROR ::: getting Recipe!!!");
return null;
}
return rList;
}
/**
* update a recipe, image name is generated dinamicly on the dao layer
* @param r
* @return
*/
public boolean updateRecipe(Recipe r){
try{
recipeDao.update(r);
}catch(Exception e){
Log.error("!!!!!! Unexpected errorsaving recipe !!!!!1 cause ::::"+e.getCause());
return false;
}
return true;
}
/**
* Create a recipe and return the created recipe, getting it from the database
* @param r
* @return
*/
public Recipe createRecipe(Recipe r){
try{
recipeDao.persist(r);
r = recipeDao.getRecipeForUserIdAndName(r.getUserId(), r.getName());
}catch(Exception ex){
Log.info("WARNING ::: recipe already in db");
Recipe oldRecipe = recipeDao.getRecipeForUserIdAndName(r.getUserId(), r.getName());
r.setRecipeId(oldRecipe.getRecipeId());
recipeDao.update(r);
}
return r;
}
/**
* returns all the recipes that contains the parameter value in theirs name.
* @param text
* @return
*/
public ArrayList<Recipe> getRecipeForName(String text){
return recipeDao.getRecipeForName(text);
}
/**
* returns all the recipes that contains the parameter value in theirs ingredients name.
* @param text
* @return
*/
public ArrayList<Recipe> getRecipeForIngredientName(String text){
return recipeDao.getRecipeForIngredientName(text);
}
/**
* returns a recipe for a given user id
* @param userId
* @return
*/
public ArrayList<Recipe> getRecipeForUserId(int userId){
ArrayList<Recipe> rList = recipeDao.getRecipeForUserIdAndName(userId);
return rList;
}
}