Exemplo n.º 1
0
  /**
   * creates the folder and uploads the image file to the local files
   *
   * @param event
   * @return
   */
  public void createRecipeImage(FileUploadEvent event) {
    Image newImage = new Image();
    String destination = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/");
    String getParam = qm.get("recipeId");
    Integer recipeID = Integer.parseInt(getParam);
    this.recipe = recipesEJB.findRecipe(recipeID);
    File file =
        new File(destination + "uploads" + File.separator + "recipe" + File.separator + recipeID);
    String abspath = file.getAbsolutePath() + File.separator;
    if (!file.exists()) {
      if (file.mkdirs()) ;
    }
    // new name of the image
    List<Image> recipeImages = this.recipe.getImageGallery();
    Integer count = recipeImages.size();

    if (this.recipe
            .getImageGallery()
            .get(0)
            .getImagePath()
            .equalsIgnoreCase("/resources/images/recipe_placeholder.png")
        && count == 1) {
      imageEJB.removeImage(this.recipe.getImageGallery().get(0));
      this.recipe.getImageGallery().remove(0);
    } else {
      count++;
    }
    String newImageName;
    if (event.getFile().getContentType().equalsIgnoreCase("image/jpeg")) {
      newImageName = count + ".jpeg";
    } else if (event.getFile().getContentType().equalsIgnoreCase("image/gif")) {
      newImageName = count + ".gif";
    } else {
      newImageName = count + ".png";
    }
    // Do what you want with the file
    String newImagePath = "/uploads/recipe/" + recipeID + "/" + newImageName;
    try {
      copyFile(abspath, newImageName, event.getFile().getInputstream());
      newImage = this.imageEJB.createImage(newImage);
      newImage.setCaption("This is the " + this.recipe.getRecipeName() + "'s recipe picture.");
      newImage.setDescription(newImageName);
      newImage.setRecipe(this.recipe);
      newImage.setImagePath(newImagePath);
      newImage.setImageName(newImageName);
      newImage = this.imageEJB.editImage(newImage);
      this.recipe.getImageGallery().add(newImage);
      this.recipe = recipesEJB.editRecipe(recipe);
      FacesContext.getCurrentInstance()
          .addMessage(null, new FacesMessage("Success!", "Your image was uploaded successfully."));
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 2
0
  /** 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();
    }
  }
Exemplo n.º 3
0
 /**
  * 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");
 }