示例#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();
    }
  }
示例#2
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");
 }