Make searchtype sticky

This commit is contained in:
Tim Holloway 2022-01-09 12:53:40 -05:00
parent 4bf9a1828c
commit e6817d7c2e
3 changed files with 60 additions and 58 deletions

View File

@ -6,7 +6,6 @@ import javax.annotation.PostConstruct;
import javax.faces.event.AjaxBehaviorEvent; import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.model.DataModel; import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel; import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import javax.faces.view.ViewScoped; import javax.faces.view.ViewScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -14,11 +13,8 @@ import javax.inject.Named;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.mousetech.gourmetj.UserSession;
import com.mousetech.gourmetj.persistence.model.Category;
import com.mousetech.gourmetj.persistence.model.Recipe; import com.mousetech.gourmetj.persistence.model.Recipe;
import com.mousetech.gourmetj.persistence.service.RecipeService; import com.mousetech.gourmetj.persistence.service.RecipeService;
@ -158,21 +154,25 @@ public class AdminMainBean implements Serializable {
public String doFind() { public String doFind() {
List<Recipe> recipes = null; List<Recipe> recipes = null;
switch (this.getSearchType()) { switch (this.getUserSession().getSearchType()) {
case rst_BY_NAME: case rst_BY_NAME:
recipes = recipeService.findByTitle(searchText); recipes = recipeService.findByTitle(searchText);
break; break;
case rst_BY_CATEGORY: case rst_BY_CATEGORY:
recipes = recipeService.findByCategoryLike(searchText); recipes =
recipeService.findByCategoryLike(searchText);
break; break;
case rst_BY_CUISINE: case rst_BY_CUISINE:
recipes = recipeService.findByCuisineLike(searchText); recipes =
recipeService.findByCuisineLike(searchText);
break; break;
case rst_BY_INGREDIENT: case rst_BY_INGREDIENT:
recipes = recipeService.findByIngredientLike(searchText); recipes = recipeService
.findByIngredientLike(searchText);
break; break;
default: default:
log.error("Invalid recipe search type: " + this.getSearchType()); log.error("Invalid recipe search type: "
+ this.getUserSession().getSearchType());
break; break;
} }
@ -233,49 +233,4 @@ public class AdminMainBean implements Serializable {
} }
return sb.toString(); return sb.toString();
} }
// ***
private RecipeSearchType searchType =
RecipeSearchType.rst_BY_NAME;
/**
* @return the searchType
*/
public RecipeSearchType getSearchType() {
return searchType;
}
/**
* @param searchType the searchType to set
*/
public void setSearchType(RecipeSearchType searchType) {
this.searchType = searchType;
}
private List<SelectItem> searchTypeList;
/**
* @return the searchTypeList
*/
public List<SelectItem> getSearchTypeList() {
if (searchTypeList == null) {
searchTypeList = loadSearchTypeList();
}
return searchTypeList;
}
private List<SelectItem> loadSearchTypeList() {
List<SelectItem> list = new ArrayList<SelectItem>(5);
list.add(new SelectItem(RecipeSearchType.rst_BY_NAME,
"Title"));
list.add(new SelectItem(RecipeSearchType.rst_BY_CATEGORY,
"Category"));
list.add(new SelectItem(RecipeSearchType.rst_BY_CUISINE,
"Cuisine"));
list.add(
new SelectItem(RecipeSearchType.rst_BY_INGREDIENT,
"Ingredient"));
return list;
}
} }

View File

@ -1,8 +1,11 @@
package com.mousetech.gourmetj; package com.mousetech.gourmetj;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.SessionScoped; import javax.enterprise.context.SessionScoped;
import javax.faces.model.SelectItem;
import javax.inject.Named; import javax.inject.Named;
import org.primefaces.PrimeFaces; import org.primefaces.PrimeFaces;
@ -28,7 +31,6 @@ public class UserSession implements Serializable {
private static final Logger log = private static final Logger log =
LoggerFactory.getLogger(UserSession.class); LoggerFactory.getLogger(UserSession.class);
private String lastSearch = ""; private String lastSearch = "";
/** /**
@ -108,6 +110,51 @@ public class UserSession implements Serializable {
this.recipe = recipe; this.recipe = recipe;
} }
// ***
private RecipeSearchType searchType =
RecipeSearchType.rst_BY_NAME;
/**
* @return the searchType
*/
public RecipeSearchType getSearchType() {
return searchType;
}
/**
* @param searchType the searchType to set
*/
public void setSearchType(RecipeSearchType searchType) {
this.searchType = searchType;
}
private List<SelectItem> searchTypeList;
/**
* @return the searchTypeList
*/
public List<SelectItem> getSearchTypeList() {
if (searchTypeList == null) {
searchTypeList = loadSearchTypeList();
}
return searchTypeList;
}
private List<SelectItem> loadSearchTypeList() {
List<SelectItem> list = new ArrayList<SelectItem>(5);
list.add(new SelectItem(RecipeSearchType.rst_BY_NAME,
"Title"));
list.add(new SelectItem(RecipeSearchType.rst_BY_CATEGORY,
"Category"));
list.add(new SelectItem(RecipeSearchType.rst_BY_CUISINE,
"Cuisine"));
list.add(
new SelectItem(RecipeSearchType.rst_BY_INGREDIENT,
"Ingredient"));
return list;
}
// ==== // ====
public String formatCategories(Recipe r) { public String formatCategories(Recipe r) {

View File

@ -28,11 +28,11 @@
/> />
<p:outputLabel for="@next" value="Search for " /> <p:outputLabel for="@next" value="Search for " />
<p:selectOneMenu id="ctlSearchType" <p:selectOneMenu id="ctlSearchType"
value="#{adminMainBean.searchType}" value="#{userSession.searchType}"
immediate="true" immediate="true"
> >
<f:selectItems <f:selectItems
value="#{adminMainBean.searchTypeList}" value="#{userSession.searchTypeList}"
/> />
<p:ajax/> <p:ajax/>
</p:selectOneMenu> </p:selectOneMenu>