Skip to content

Commit

Permalink
javaee-projets-movie-db - nombre de produits par cat et fabr
Browse files Browse the repository at this point in the history
  • Loading branch information
ltearno committed Nov 10, 2017
1 parent 327c24f commit ad2c9a6
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
import fr.lteconsulting.training.moviedb.model.Categorie;

import javax.ejb.Stateless;
import javax.persistence.TypedQuery;

@Stateless
public class GestionCategories extends GestionGenerique<Categorie> {
public GestionCategories() {
super(Categorie.class);
}

public Long getNbProduitParCategorieId(Integer id) {
TypedQuery<Long> query = em.createQuery("select count(p) from Produit p where p.categorie.id=:id", Long.class);
query.setParameter("id", id);
return query.getSingleResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
import fr.lteconsulting.training.moviedb.model.Fabricant;

import javax.ejb.Stateless;
import javax.persistence.TypedQuery;

@Stateless
public class GestionFabricants extends GestionGenerique<Fabricant> {
public GestionFabricants() {
super(Fabricant.class);
}

public Long getNbProduitParFabricantId(Integer id) {
TypedQuery<Long> query = em.createQuery("select count(p) from Produit p where p.fabricant.id=:id", Long.class);
query.setParameter("id", id);
return query.getSingleResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
import javax.persistence.PersistenceContext;
import java.util.List;

/**
* Ceci est une implémentation générique d'un EJB d'accès aux données (souvent appelé DAO).
*
* Il apporte les opérations de base communes à toutes nos entités (Categorie, Fabricant et Produit).
*
* Il stocke la classe sur laquelle il travaille dans son attribut 'managedClass'.
*
* Les sous-classes concrètes pourront ajouter des méthodes selon les besoins spécifiques.
*
* @param <T> Le type que l'EJB gère
*/
@Stateless
public abstract class GestionGenerique<T> {
@PersistenceContext(unitName = "Catalogue", name = "Catalogue")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package fr.lteconsulting.training.moviedb.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.*;
import java.util.List;

@Entity
public class Categorie {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@OneToMany(mappedBy = "categorie")
private List<Produit> produits;

private String nom;

public Integer getId() {
Expand All @@ -21,6 +22,14 @@ public void setId(Integer id) {
this.id = id;
}

public List<Produit> getProduits() {
return produits;
}

public void setProduits(List<Produit> produits) {
this.produits = produits;
}

public String getNom() {
return nom;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class Vues {
public static void afficherCategories(HttpServletRequest req, HttpServletResponse resp, List<Categorie> categories) throws ServletException, IOException {
public static void afficherCategories(HttpServletRequest req, HttpServletResponse resp, List<Categorie> categories, Map<Integer, Long> nbProduitsParCategorie) throws ServletException, IOException {
req.setAttribute("categories", categories);
req.setAttribute("nbProduitsParCategorie", nbProduitsParCategorie);

afficherPage(req, resp, "Visualisation des catégories", "categories");
}
Expand All @@ -23,8 +25,9 @@ public static void afficherEditionCategorie(HttpServletRequest req, HttpServletR
afficherPage(req, resp, "Edition catégorie", "editionCategorie");
}

public static void afficherFabricants(HttpServletRequest req, HttpServletResponse resp, List<Fabricant> fabricants) throws ServletException, IOException {
public static void afficherFabricants(HttpServletRequest req, HttpServletResponse resp, List<Fabricant> fabricants, Map<Integer, Long> nbProduitsParFabricant) throws ServletException, IOException {
req.setAttribute("fabricants", fabricants);
req.setAttribute("nbProduitsParFabricant", nbProduitsParFabricant);

afficherPage(req, resp, "Visualisation fabricants", "fabricants");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package fr.lteconsulting.training.moviedb.servlet;

import fr.lteconsulting.training.moviedb.ejb.GestionCategories;
import fr.lteconsulting.training.moviedb.outil.Vues;
import fr.lteconsulting.training.moviedb.model.Categorie;
import fr.lteconsulting.training.moviedb.outil.Vues;

import javax.ejb.EJB;
import javax.servlet.ServletException;
Expand All @@ -11,7 +11,9 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/categories")
public class CategoriesServlet extends HttpServlet {
Expand All @@ -22,6 +24,11 @@ public class CategoriesServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Categorie> categories = gestionCategories.findAll();

Vues.afficherCategories(req, resp, categories);
Map<Integer, Long> nbProduitsParCategorie = new HashMap<>();
for (Categorie categorie : categories) {
nbProduitsParCategorie.put(categorie.getId(), gestionCategories.getNbProduitParCategorieId(categorie.getId()));
}

Vues.afficherCategories(req, resp, categories, nbProduitsParCategorie);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/fabricants")
public class FabricantsServlet extends HttpServlet {
Expand All @@ -22,6 +24,11 @@ public class FabricantsServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Fabricant> fabricants = gestionFabricants.findAll();

Vues.afficherFabricants(req, resp, fabricants);
Map<Integer, Long> nbProduitsParFabricant = new HashMap<>();
for (Fabricant fabricant : fabricants) {
nbProduitsParFabricant.put(fabricant.getId(), gestionFabricants.getNbProduitParFabricantId(fabricant.getId()));
}

Vues.afficherFabricants(req, resp, fabricants, nbProduitsParFabricant);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<%@ page import="fr.lteconsulting.training.moviedb.model.Categorie" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="utf-8" />
<meta charset="utf-8"/>
<title>Categories</title>
<link rel="stylesheet" href="Skeleton-2.0.4/css/normalize.css">
<link rel="stylesheet" href="Skeleton-2.0.4/css/skeleton.css">
Expand All @@ -12,13 +13,15 @@

<%
List<Categorie> categories = (List<Categorie>) request.getAttribute("categories");
Map<Integer, Long> nbProduitsParCategorie = (Map<Integer, Long>) request.getAttribute("nbProduitsParCategorie");
%>

<h1>Catégories</h1>
<table>
<tr>
<th>ID</th>
<th>Nom</th>
<th>Nombre de produits</th>
<th>Actions</th>
</tr>
<%
Expand All @@ -29,6 +32,8 @@
</td>
<td><%= categorie.getNom()%>
</td>
<td><%= nbProduitsParCategorie.get(categorie.getId())%>
</td>
<td>
<form method="get" action="editionCategorie" style="display: inline-block;">
<input type="hidden" name="id" value="<%= categorie.getId()%>">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<%@ page import="fr.lteconsulting.training.moviedb.model.Fabricant" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
Expand All @@ -12,6 +13,7 @@

<%
List<Fabricant> fabricants = (List<Fabricant>) request.getAttribute("fabricants");
Map<Integer, Long> nbProduitsParFabricant = (Map<Integer, Long>) request.getAttribute("nbProduitsParFabricant");
%>

<h1>Fabricants</h1>
Expand All @@ -20,6 +22,7 @@
<th>ID</th>
<th>Nom</th>
<th>Adresse</th>
<th>Nombre de produit</th>
<th>Actions</th>
</tr>
<%
Expand All @@ -32,6 +35,8 @@
</td>
<td><%= fabricant.getAdresse()%>
</td>
<td><%= nbProduitsParFabricant.get(fabricant.getId())%>
</td>
<td>
<form method="get" action="editionFabricant" style="display: inline-block;">
<input type="hidden" name="id" value="<%= fabricant.getId()%>">
Expand Down

0 comments on commit ad2c9a6

Please sign in to comment.