Web implementation of the Gourmet Recipe Manager
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

268 lines
5.6 KiB

package com.mousetech.gourmetj;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import com.mousetech.gourmetj.persistence.model.Recipe;
import com.mousetech.gourmetj.persistence.service.RecipeService;
/**
* Main control panel backing bean.
*
* @author timh
* @since Jun 28, 2012
*/
@Named
@ViewScoped
public class AdminMainBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2L;
public AdminMainBean() {
}
/* Logger */
private static final Logger log =
LoggerFactory.getLogger(AdminMainBean.class);
/**
* Persistency service for Recipes
*/
@Inject
transient RecipeService recipeService;
public void setRecipeService(RecipeService service) {
log.debug("INJECT RECIPESERVICE===" + service);
this.recipeService = service;
}
// **
@Inject
private UserSession userSession;
/**
* @return the userSession
*/
public UserSession getUserSession() {
return userSession;
}
/**
* @param userSession the userSession to set
*/
public void setUserSession(UserSession userSession) {
this.userSession = userSession;
}
// **
private String searchText;
/**
* @return the searchText
*/
public String getSearchText() {
if (this.searchResults == null) {
// Fake around broken @PostConstruct
this.setSearchText(userSession.getLastSearch());
}
return searchText;
}
/**
* @param searchText the searchText to set
*/
public void setSearchText(String searchText) {
this.searchText = searchText;
userSession.setLastSearch(searchText);
}
private List<String> suggestionList = null;
public void resetSuggestions() {
suggestionList = null;
}
public List<String> searchSuggestionList(String query) {
if (suggestionList == null) {
switch (this.userSession.getSearchType()) {
case rst_BY_CATEGORY:
suggestionList = recipeService.findCategories();
break;
case rst_BY_CUISINE:
suggestionList = recipeService.findCuisines();
break;
default:
suggestionList = new ArrayList<String>(1);
}
}
return suggestionList;
}
/**/
transient DataModel<Recipe> searchResults;
/**
* @return the searchResults
*/
public DataModel<Recipe> getSearchResults() {
if (searchResults == null) {
searchResults = new ListDataModel<Recipe>();
init(); // @PostConstruct is broken
}
return searchResults;
}
/**
* @param searchResults the searchResults to set
*/
public void setSearchResults(
DataModel<Recipe> searchResults) {
this.searchResults = searchResults;
}
/**
* Return to last search, if any No longer broken (required
* Maven include for annotation).
*/
@PostConstruct
void init() {
log.debug("Initializing AdminMainBean " + this);
this.setSearchText(userSession.getLastSearch());
// Clean up from any previous operations.
this.userSession.setRecipe(null);
doFind();
}
/**
* Search, driven by AJAX
*
* @param event Notused
*/
public void ajaxUpdateList(AjaxBehaviorEvent event) {
this.doFind();
}
/**
* Reset search and display
*/
public void ajaxClearList() {
this.setSearchText("");
this.doFind();
}
/**
* Finder
*
* @return Navigation string
*/
public String doFind() {
List<Recipe> recipes = null;
switch (this.getUserSession().getSearchType()) {
case rst_BY_NAME:
recipes = recipeService.findByTitle(searchText);
break;
case rst_BY_CATEGORY:
recipes =
recipeService.findByCategoryLike(searchText);
break;
case rst_BY_CUISINE:
recipes =
recipeService.findByCuisineLike(searchText);
break;
case rst_BY_INGREDIENT:
recipes = recipeService
.findByIngredientLike(searchText);
break;
default:
log.error("Invalid recipe search type: "
+ this.getUserSession().getSearchType());
break;
}
getSearchResults().setWrappedData(recipes);
this.userSession.setLastSearch(this.getSearchText());
return null; // Stay on page
}
/**
* Prep to create a new recipe
*
* @return navigation to detailEdit page
*/
public String doNewRecipe() {
// Clear for new recipe
this.userSession.setLastEdit(null);
// Construct a blank recipe to be created.
this.userSession.setRecipe(new Recipe());
return "detailEdit?faces-redirect=true";
}
/**
* Navigate to "More features" page (shopping list and
* maint.)
*/
public String doMore() {
return "shoppingList.jsf";
}
/**
* Show selected recipe
*
* @return
*/
public String showRecipe() {
long recipeId = getSearchResults().getRowData().getId();
// Flash Scope is buggy under Mojarra plus now using
// session
// JSFUtils.flashScope().put("recipeID",
// Long.valueOf(recipeId));
userSession.setLastEdit(recipeId);
userSession.setRecipe(null); // forces loading of line
// items.
return "recipeDetails?faces-redirect=true";
}
/**
* Get printable preptime. Database version is in seconds.
*
* @deprecated User {@link UserSession#formatTime(Long)}
*
* @return Formatted time. Called from EL on main page.
*/
public String formatPreptime(int timesec) {
StringBuffer sb = new StringBuffer(20);
int preptime = timesec / 60;
if (preptime > 60) {
int hours = preptime / 60;
sb.append(hours);
sb.append(" h. ");
preptime %= 60;
}
if (preptime > 0) {
sb.append(preptime);
sb.append(" min.");
}
return sb.toString();
}
}