<commit_before>package it.uniroma3.controller;

import java.util.List;
import java.util.Map;

import it.uniroma3.model.Customer;
import it.uniroma3.model.Product;
import it.uniroma3.model.Review;
import it.uniroma3.facade.CustomerFacade;
import it.uniroma3.facade.ProductFacade;
import it.uniroma3.facade.ReviewFacade;
import it.uniroma3.helper.FileHelper;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.Part;


@ManagedBean
public class ProductController {

	@ManagedProperty(value="#{param.id}")
	private Long id;
	private String name;
	private Float price;
	private String description;
	private String code;
	private Integer quantity;
	private Part picture;
	private String picturePath;

	private Product product;
	private List<Product> products;

	// review form data
	private String comment;
	private Integer stars;
	private Long idProduct;
	
	private Review review;

	private String page;

	@EJB
	private ProductFacade productFacade;
	@EJB
	private CustomerFacade customerFacade;

	private ReviewFacade reviewFacade;
	
	private Map<String, Object> currentSessionMap;
	
	
	public ProductController() {
		this.currentSessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		this.reviewFacade = new ReviewFacade();
	}

	public String createProduct() {
		// security check
		AdminController ac = new AdminController();

		//file path functions
		FileHelper fh = new FileHelper();

		if(ac.isLogged()){
			String filename = fh.getFileNameFromHeader(picture);
			ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
			String path = ctx.getRealPath("/");
			path = path + fh.makePath(path, "uploads");
			System.out.print("\n\nFile system context path (in TestServlet): " + path + "\n");

			try {
				System.out.print("\nTrying to upload "+ filename +" in "+ path +"\n");
				picture.write(path+filename);
				System.out.print("\nSuccesful! "+ filename + " is uploaded\n\n");

				this.picturePath = filename.toString();

				this.product = productFacade.createProduct(name, code, price, description, picturePath, quantity);
				return "product"; 
			} 
			catch (Exception e) {
				return "errorProduct"; 
			}
		}
		else {
			return "admin?faces-redirect=true";
		}
	}

	public String listProducts() {
		this.products = productFacade.getAllProducts();
		return "products?faces-redirect=true"; 
	}

	public List<Product> getListProducts() {<commit_msg>Fixato problema redirect dopo la creazione del prodotto. Utilizzava l'url vecchio senza l'id nella GET<commit_after>package it.uniroma3.controller;

import java.util.List;
import java.util.Map;

import it.uniroma3.model.Customer;
import it.uniroma3.model.Product;
import it.uniroma3.model.Review;
import it.uniroma3.facade.CustomerFacade;
import it.uniroma3.facade.ProductFacade;
import it.uniroma3.facade.ReviewFacade;
import it.uniroma3.helper.FileHelper;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.Part;


@ManagedBean
public class ProductController {

	@ManagedProperty(value="#{param.id}")
	private Long id;
	private String name;
	private Float price;
	private String description;
	private String code;
	private Integer quantity;
	private Part picture;
	private String picturePath;

	private Product product;
	private List<Product> products;

	// review form data
	private String comment;
	private Integer stars;
	private Long idProduct;
	
	private Review review;

	private String page;

	@EJB
	private ProductFacade productFacade;
	@EJB
	private CustomerFacade customerFacade;

	private ReviewFacade reviewFacade;
	
	private Map<String, Object> currentSessionMap;
	
	
	public ProductController() {
		this.currentSessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		this.reviewFacade = new ReviewFacade();
	}

	public String createProduct() {
		// security check
		AdminController ac = new AdminController();

		//file path functions
		FileHelper fh = new FileHelper();

		if(ac.isLogged()){
			String filename = fh.getFileNameFromHeader(picture);
			ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
			String path = ctx.getRealPath("/");
			path = path + fh.makePath(path, "uploads");
			System.out.print("\n\nFile system context path (in TestServlet): " + path + "\n");

			try {
				System.out.print("\nTrying to upload "+ filename +" in "+ path +"\n");
				picture.write(path+filename);
				System.out.print("\nSuccesful! "+ filename + " is uploaded\n\n");

				this.picturePath = filename.toString();

				this.product = productFacade.createProduct(name, code, price, description, picturePath, quantity);
				
				return "product?id="+this.product.getId()+"&faces-redirect=true&includeViewParams=true"; 
			} 
			catch (Exception e) {
				return "errorProduct"; 
			}
		}
		else {
			return "admin?faces-redirect=true";
		}
	}

	public String listProducts() {
		this.products = productFacade.getAllProducts();
		return "products?faces-redirect=true"; 
	}

	public List<Product> getListProducts() {
{'fraction_non_alphanumeric': 0.11274967332462199, 'fraction_numerical': 0.0029867463132350197, 'mean_word_length': 4.74892703862661, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3219893', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1595, 'n_words': 390}