package com.mousetech.gourmetj; import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.mousetech.gourmetj.persistence.model.Recipe; import com.mousetech.gourmetj.persistence.service.RecipeService; import jakarta.annotation.PostConstruct; import jakarta.faces.event.AjaxBehaviorEvent; import jakarta.faces.model.DataModel; import jakarta.faces.model.ListDataModel; import jakarta.faces.view.ViewScoped; import jakarta.inject.Inject; import jakarta.inject.Named; /** * 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 CookieBean cookieBean; /** * @return the cookieBean */ public CookieBean getCookieBean() { return cookieBean; } /** * @param cookieBean the cookieBean to set */ public void setCookieBean(CookieBean cookieBean) { this.cookieBean = cookieBean; } // ** @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) { // this.setSearchText(cookieBean.getSearchText()); // } this.searchText = cookieBean.getSearchText(); return searchText; } /** * @param searchText the searchText to set */ public void setSearchText(String searchText) { this.searchText = searchText; cookieBean.setSearchText(searchText); } private List suggestionList = null; public void resetSuggestions() { suggestionList = null; } public List searchSuggestionList(String query) { if (suggestionList == null) { switch (searchtypeEnum()) { case rst_BY_CATEGORY: suggestionList = recipeService.findCategories(); break; case rst_BY_CUISINE: suggestionList = recipeService.findCuisines(); break; default: suggestionList = new ArrayList(1); } } return suggestionList; } private RecipeSearchType searchtypeEnum() { int stn = cookieBean.getSearchType(); return searchtypeEnum(stn); } private RecipeSearchType searchtypeEnum(int stn) { RecipeSearchType st = RecipeSearchType.values()[stn]; return st; } /**/ transient DataModel searchResults; /** * @return the searchResults */ public DataModel getSearchResults() { if (searchResults == null) { searchResults = new ListDataModel(); init(); // @PostConstruct is broken TODO: fixed?? } return searchResults; } /** * @param searchResults the searchResults to set */ public void setSearchResults( DataModel 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(cookieBean.getSearchText()); // 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 recipes = null; if ( searchText == null ) { setSearchText(""); } searchText = searchText.trim(); // Persist current settings try { cookieBean.saveCookies(); } catch (UnsupportedEncodingException e) { // Something is really wrong if we can't create UTF-8! log.error("Unable to save cookies!", e); } RecipeSearchType st = searchtypeEnum(); switch (st) { 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: " + st); break; } getSearchResults().setWrappedData(recipes); 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?redirect=true"; } /** * 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"; } public String doLogout() { JSFUtils.logout(); return null; } }