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.
209 lines
4.2 KiB
209 lines
4.2 KiB
2 years ago
|
package com.mousetech.gourmetj;
|
||
|
|
||
|
import java.io.Serializable;
|
||
|
|
||
|
import javax.annotation.PostConstruct;
|
||
|
|
||
|
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.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.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;
|
||
|
}
|
||
|
|
||
|
/**/
|
||
|
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
|
||
|
*/
|
||
|
@PostConstruct
|
||
|
void init() {
|
||
|
this.setSearchText(userSession.getLastSearch());
|
||
|
// Clean up from any previous operations.
|
||
|
this.userSession.setRecipe(null);
|
||
|
doFind();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Finder
|
||
|
*
|
||
|
* @return Navigation string
|
||
|
*/
|
||
|
public String doFind() {
|
||
|
List<Recipe> recipes =
|
||
|
recipeService.findByTitle(searchText);
|
||
|
|
||
|
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";
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Show selected recipe
|
||
|
*
|
||
|
* @return
|
||
|
*/
|
||
|
public String showRecipe() {
|
||
|
long recipeId = getSearchResults().getRowData().getId();
|
||
|
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.
|
||
|
*
|
||
|
* @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();
|
||
|
}
|
||
|
|
||
|
public String formatCategories(Recipe r) {
|
||
|
StringBuffer sb = new StringBuffer(30);
|
||
|
boolean first = true;
|
||
|
for (Category cat : r.getCategories()) {
|
||
|
if (first) {
|
||
|
first = false;
|
||
|
} else {
|
||
|
sb.append(", ");
|
||
|
}
|
||
|
sb.append(cat.getCategory());
|
||
|
}
|
||
|
return sb.toString();
|
||
|
}
|
||
|
}
|