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.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 searchResults; /** * @return the searchResults */ public DataModel getSearchResults() { if (searchResults == null) { searchResults = new ListDataModel(); init(); // @PostConstruct is broken } 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(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 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(); // 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(); } }