/** This method is called AFTER the bean is constructed */ @PostConstruct private void init() { if (!FacesContext.getCurrentInstance().isPostback()) { // TODO: view counter ++ } // Get the recipe String value = qm.get("recipe"); if (value != null) { recipe = recipesEJB.findRecipe(Integer.parseInt(value)); } // init some fields tags = new DefaultTagCloudModel(); newComment = new Comment(); newRating = new RecipeRating(); tags = new DefaultTagCloudModel(); if (recipe != null) { this.commentModel = new LazyCommentDataModel(recipe, recipesEJB); this.totalRatings = ratingEJB.countTotalRatings(recipe); Users user = ui.getUser(); getNutritionixIngredientInfo(recipe); recipesEJB.incrementViews(recipe); // Get the recipe's tags ArrayList<Tag> tagList = new ArrayList<Tag>(recipe.getTags()); if (!tagList.isEmpty()) { ListIterator i = tagList.listIterator(); while (i.hasNext()) { Tag t = (Tag) i.next(); String url = "search.xhtml?searchArg=" + t.getTagName(); this.tags.addTag(new DefaultTagCloudItem(t.getTagName(), url, tagEJB.getWeight(t))); } } else { this.tags.addTag(new DefaultTagCloudItem("#tagMe", 1)); } // Get related recipes relatedRecipes = search.getSearchRecipes(recipe); } // end if recipe is null else { this.tags.addTag(new DefaultTagCloudItem("#tagMe", 1)); } if (ui.isIsUserAuthenticated()) { user = ui.getUser(); } }
/** * checks if the user is able to upload * * @return true if user is authorized false if the user is not */ public boolean shouldRenderUpload() { if (recipe != null && ui.isIsUserAuthenticated()) { return recipe.getCreator().getUserName().equals(ui.getUser().getUserName()); } else { return false; } }
/** For canceling a rating event */ public void oncancel() { Users user = ui.getUser(); newRating = ratingEJB.findByUserAndRecipe(user, recipe); if (newRating != null) { ratingEJB.removeRecipeRating(newRating); } }
/** * returns the rating of this recipe * * @return integer rating from 1-5 */ public Integer getRating() { if (recipe != null && ui.isIsUserAuthenticated()) { RecipeRating temp = ratingEJB.findByUserAndRecipe(ui.getUser(), recipe); if (temp != null) { rating = temp.getRatingValue().getValue(); } } return rating; }
public boolean isEditAuthorized() { Users user = ui.getUser(); boolean editAuthorized = false; if (user != null && recipe != null) { editAuthorized = recipe.getCreator().getUserName().equals(user.getUserName()) || ui.isIsAdmin(); } return editAuthorized; }
/** * Returns true if the user has already favorited this recipe, false otherwise * * @return true if already favorited, false otherwise */ public boolean isAlreadyFavorited() { if (ui.isIsUserAuthenticated()) { Users user = ui.getUser(); if (user.getFavorites() != null) { return user.getFavorites().contains(recipe); } else { return false; } } else { return false; } }
/** deletes a comment */ public void doDeleteComment() { if (ui.isIsUserAuthenticated()) { Users u = ui.getUser(); if (isEditAuthorized() || ui.isIsAdmin()) { try { recipesEJB.removeCommentFromRecipe(recipe, deleteComment); this.commentModel = new LazyCommentDataModel(recipe, recipesEJB); } catch (javax.ejb.EJBAccessException ejbae) { FacesContext.getCurrentInstance() .addMessage(null, new FacesMessage("Only registered users can post comments.")); } } } }
/** * returns true if it has been reviewed and false if it has not been reviewed by a professional; */ public boolean isHasAlreadyReviewed() { boolean result = false; if (ui.isIsUserAuthenticated() && professionalStatus.isIsProfessional()) { Users user = ui.getUser(); if (recipe != null) { for (Review rev : recipe.getReviews()) { if (rev.getReviewer().getUserName().equals(user.getUserName())) { result = true; break; } } // end for } } // end value != null return result; }
/** * Creates a comment and redirects back to the recipe page * * @return the string navigation outcome */ public String doCreateComment() { Users commenter = ui.getUser(); try { this.newComment.setRecipe(this.recipe); this.newComment.setCommenter(commenter); this.newComment.setDateCommented(new Date().getTime()); List<Comment> c = recipe.getComments(); c.add(newComment); this.recipe.setComments(c); recipesEJB.editRecipe(recipe); } catch (javax.ejb.EJBAccessException ejbae) { FacesContext.getCurrentInstance() .addMessage(null, new FacesMessage("Only registered users can post comments.")); } return "/recipe.xhtml?recipe=" + qm.get("recipe"); }
/** This method is intended to be used as an actionListener for rating */ public void handleRating() { Users rater = ui.getUser(); newRating = ratingEJB.findByUserAndRecipe(rater, recipe); boolean edit = false; // determine whether we need to edit // No rating for this user exists if (newRating == null && rating > 0 && rating <= 5) { newRating = new RecipeRating(); newRating.setRater(rater); newRating.setRecipe(recipe); newRating.setRatingDate(new Date().getTime()); } // A rating exists else { edit = true; } switch (rating) { case 1: this.newRating.setRatingValue(RatingValue.ONE_STAR); break; case 2: this.newRating.setRatingValue(RatingValue.TWO_STARS); break; case 3: this.newRating.setRatingValue(RatingValue.THREE_STARS); break; case 4: this.newRating.setRatingValue(RatingValue.FOUR_STARS); break; case 5: this.newRating.setRatingValue(RatingValue.FIVE_STARS); break; } // end switch if (edit) { this.newRating = ratingEJB.editRecipeRating(newRating); } else { this.newRating = ratingEJB.createRecipeRating(newRating); } }