diff --git a/.github/assets/1.png b/.github/assets/1.png index d2e853b..90f7ba8 100644 Binary files a/.github/assets/1.png and b/.github/assets/1.png differ diff --git a/.github/assets/10.png b/.github/assets/10.png new file mode 100644 index 0000000..d09c8a7 Binary files /dev/null and b/.github/assets/10.png differ diff --git a/.github/assets/11.png b/.github/assets/11.png new file mode 100644 index 0000000..72c5dfa Binary files /dev/null and b/.github/assets/11.png differ diff --git a/.github/assets/9.png b/.github/assets/9.png index c0f39cb..7d592cb 100644 Binary files a/.github/assets/9.png and b/.github/assets/9.png differ diff --git a/.github/assets/tree.jpeg b/.github/assets/tree.jpeg deleted file mode 100644 index b07148d..0000000 Binary files a/.github/assets/tree.jpeg and /dev/null differ diff --git a/.github/assets/tree.png b/.github/assets/tree.png new file mode 100644 index 0000000..1718672 Binary files /dev/null and b/.github/assets/tree.png differ diff --git a/README.md b/README.md index 3843dd5..b455312 100644 --- a/README.md +++ b/README.md @@ -3,24 +3,14 @@ ## Requisitos de aceitação -- [X] Ao menos duas classes de modelo (com seus cadastros, páginas para editar e - remover); -- [X] Controller´s para manipular as requisições e armazenar os objetos na sessão - ou cookie a escolha sobre qual utilizar, fica a cargo do aluno/equipe; -- [X] Um servlet que exiba (em HTML) todos os objetos armazenados em memória - (relatório); -- [X] O site/sistema deve permitir o cadastro e listagem de múltiplos elementos (do - item 1) em memória (utilizando sessão ou cookies); -- [X] Os objetos devem possuir páginas separadas de cadastro; -- [X] Uma página (servlet) que exiba a quantidade de cada um dos itens - armazenados na memória (pode ser a mesma página do requisito 3). -- [X] Não devem ser utilizadas TAG´s JSP, arquivos - JSP devem ser utilizados como HTML) +- [X] Ao menos 3 cadastros completos (Listagem/Relatório, Cadastro, Edição, Remoção); +- [X] Deve-se utilizar banco de dados para a persistência dos dados, a escolha de qual banco, fica com a equipe, lembrando apenas que a mesma deve conhecer o banco utilizado, para responder eventuais questionamentos. +- [X] utilizar o padrão MVC ## Árvore do projeto
- +
## Telas do projeto @@ -30,8 +20,16 @@
+ +
+

Mesários

+
+ + +
+

Candidatos

diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..86abccb --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,22 @@ +version: '3.3' +services: + db: + image: mysql:5.7 + restart: always + environment: + MYSQL_DATABASE: 'votacao' + # So you don't have to use root, but you can if you like + MYSQL_USER: 'user' + # You can use whatever password you like + MYSQL_PASSWORD: 'password' + # Password for root access + MYSQL_ROOT_PASSWORD: 'password' + ports: + # : < MySQL Port running inside container> + - '3306:3306' + expose: + # Opens port 3306 on the container + - '3306' + # Where our data will be persisted +# volumes: +# - my-db:/var/lib/mysql \ No newline at end of file diff --git a/pom.xml b/pom.xml index 3ba950f..b4ba949 100644 --- a/pom.xml +++ b/pom.xml @@ -36,7 +36,30 @@ ${junit.version} test + + org.projectlombok + lombok + 1.18.22 + provided + + + + org.hibernate + hibernate-core + 5.4.12.Final + + + org.hibernate + hibernate-entitymanager + 5.4.12.Final + + + + mysql + mysql-connector-java + 8.0.19 + diff --git a/src/main/java/com/example/core/Canditatos/Controller/CandidatoEditServlet.java b/src/main/java/com/example/core/Canditatos/Controller/CandidatoEditServlet.java new file mode 100644 index 0000000..88b8f2c --- /dev/null +++ b/src/main/java/com/example/core/Canditatos/Controller/CandidatoEditServlet.java @@ -0,0 +1,31 @@ +package com.example.core.Canditatos.Controller; + +import com.example.core.Utils.Utils; +import com.example.core.Canditatos.Model.Candidato; +import com.example.core.Canditatos.Repository.CandidatoRepository; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@WebServlet( value = "/candidato-edit") +public class CandidatoEditServlet extends HttpServlet { + + private CandidatoRepository candidatoRepository = new CandidatoRepository(); + private Utils utils = new Utils(); + + // EDIT CANDIDATO + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + Long id = utils.getId(request.getParameter("id")); + + Candidato candidato = candidatoRepository.findOne(id).orElse(new Candidato()); + + request.setAttribute("candidato", candidato); + RequestDispatcher requestDispatcher = request.getRequestDispatcher("candidatos/candidato.jsp"); + requestDispatcher.forward(request,response); + } +} diff --git a/src/main/java/com/example/core/Canditatos/Controller/CandidatoRemoveServlet.java b/src/main/java/com/example/core/Canditatos/Controller/CandidatoRemoveServlet.java new file mode 100644 index 0000000..0f572d7 --- /dev/null +++ b/src/main/java/com/example/core/Canditatos/Controller/CandidatoRemoveServlet.java @@ -0,0 +1,30 @@ +package com.example.core.Canditatos.Controller; + +import com.example.core.Canditatos.Repository.CandidatoRepository; +import com.example.core.Utils.Utils; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@WebServlet( value = "/candidato-remove") +public class CandidatoRemoveServlet extends HttpServlet { + + CandidatoRepository candidatoRepository = new CandidatoRepository(); + Utils utils = new Utils(); + + // DELETE CANDIDATO + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + Long id = utils.getId(request.getParameter("id")); + System.out.println("Delete: "+ id); + + if (utils.isZero(id)) response.sendRedirect("/candidatos"); + + candidatoRepository.remove(id); + + response.sendRedirect(request.getContextPath() + "/candidatos"); + } +} diff --git a/src/main/java/com/example/core/Canditatos/Controller/CandidatosServlet.java b/src/main/java/com/example/core/Canditatos/Controller/CandidatosServlet.java new file mode 100644 index 0000000..ec061fc --- /dev/null +++ b/src/main/java/com/example/core/Canditatos/Controller/CandidatosServlet.java @@ -0,0 +1,69 @@ +package com.example.core.Canditatos.Controller; + +import com.example.core.Canditatos.Model.Candidato; +import com.example.core.Canditatos.Repository.CandidatoRepository; +import com.example.core.Utils.Utils; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + + +@WebServlet(value = "/candidatos") +public class CandidatosServlet extends HttpServlet { + + private List candidatos = new ArrayList<>(); + private CandidatoRepository candidatoRepository = new CandidatoRepository(); + private Utils utils = new Utils(); + + // LISTAGEM CANDIDATOS + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + this.candidatos = candidatoRepository.findAll(); + + request.setAttribute("candidatos", this.candidatos); + RequestDispatcher requestDispatcher = request.getRequestDispatcher("candidatos/candidatos.jsp"); + requestDispatcher.forward(request,response); + } + + // SALVAR CANDIDATO + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + Long id = utils.getId(request.getParameter("id")); + String nome = request.getParameter("nome"); + String numero = request.getParameter("numero"); + + if (validaDuplicado(id, nome, numero)){ + response.sendRedirect("candidatos/candidato-duplicado.html"); + }else { + if (utils.isZero(id)) { + System.out.println("doPost: new candidato"); + Candidato candidato = new Candidato(nome, Integer.valueOf(numero)); + candidatoRepository.save(candidato); + } else { + System.out.println("doPost: candidato update: " + id); + Candidato candidato = candidatoRepository.findOne(id).orElse(new Candidato()); + candidato.setNome(nome); + candidato.setNumeroCandidato(Integer.valueOf(numero)); + candidatoRepository.save(candidato); + } + response.sendRedirect(request.getRequestURI()); + } + } + + private boolean validaDuplicado(Long id, String nome, String numero){ + for (Candidato candidato: this.candidatos){ + if (candidato.getNome().equals(nome) && candidato.getNumeroCandidato().equals(Integer.valueOf(numero)) && !candidato.getId().equals(id)){ + return true; + } + } + return false; + } +} diff --git a/src/main/java/com/example/core/Canditatos/Model/Candidato.java b/src/main/java/com/example/core/Canditatos/Model/Candidato.java new file mode 100644 index 0000000..ba0575c --- /dev/null +++ b/src/main/java/com/example/core/Canditatos/Model/Candidato.java @@ -0,0 +1,32 @@ +package com.example.core.Canditatos.Model; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +@Entity +public class Candidato { + + @Id + @GeneratedValue + private Long id; + @Column + private String nome; + @Column + private Integer numeroCandidato; + + public Candidato(String nome, Integer numeroCandidato){ + this.nome = nome; + this.numeroCandidato = numeroCandidato; + } +} diff --git a/src/main/java/com/example/core/Canditatos/Repository/CandidatoRepository.java b/src/main/java/com/example/core/Canditatos/Repository/CandidatoRepository.java new file mode 100644 index 0000000..9b2a909 --- /dev/null +++ b/src/main/java/com/example/core/Canditatos/Repository/CandidatoRepository.java @@ -0,0 +1,65 @@ +package com.example.core.Canditatos.Repository; + +import com.example.core.Canditatos.Model.Candidato; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +public class CandidatoRepository { + EntityManagerFactory entityManagerFactory; + EntityManager entityManager; + + public CandidatoRepository() { + entityManagerFactory = Persistence.createEntityManagerFactory("votacao-jpa"); + entityManager = entityManagerFactory.createEntityManager(); + } + + public void save(Candidato candidato) { + try { + entityManager.getTransaction().begin(); + entityManager.persist(candidato); + entityManager.getTransaction().commit(); + entityManager.getTransaction(); + }catch (Exception e){ + System.out.println(e); + } + } + + public void remove(Long id) { + try { + Candidato candidato = findOne(id).orElse(null); + entityManager.getTransaction().begin(); + entityManager.remove(candidato); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + System.out.println(e); + } + } + + public Optional findOne(Long id){ + Candidato candidato = new Candidato(); + try { + candidato = entityManager.find(Candidato.class, id); + }catch (Exception e){ + System.out.println(e); + } + return Optional.ofNullable(candidato); + } + + @SuppressWarnings("unchecked") + public List findAll(){ + List candidatos = Collections.emptyList(); + try { + candidatos = entityManager.createQuery("select candidato from Candidato candidato").getResultList(); + } + catch (Exception e){ + System.out.println(e); + } + return candidatos; + } +} diff --git a/src/main/java/com/example/core/Mesarios/Controller/MesarioEditServlet.java b/src/main/java/com/example/core/Mesarios/Controller/MesarioEditServlet.java new file mode 100644 index 0000000..a8d78e3 --- /dev/null +++ b/src/main/java/com/example/core/Mesarios/Controller/MesarioEditServlet.java @@ -0,0 +1,32 @@ +package com.example.core.Mesarios.Controller; + +import com.example.core.Mesarios.Model.Mesario; +import com.example.core.Mesarios.Repository.MesarioRepository; +import com.example.core.Utils.Utils; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@WebServlet( value = "/mesario-edit") +public class MesarioEditServlet extends HttpServlet { + + private MesarioRepository candidatoRepository = new MesarioRepository(); + private Utils utils = new Utils(); + + // EDIT CANDIDATO + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + Long id = utils.getId(request.getParameter("id")); + + Mesario mesario = candidatoRepository.findOne(id).orElse(new Mesario()); + + request.setAttribute("mesario", mesario); + RequestDispatcher requestDispatcher = request.getRequestDispatcher("mesarios/mesario.jsp"); + requestDispatcher.forward(request,response); + } +} diff --git a/src/main/java/com/example/core/Mesarios/Controller/MesarioRemoveServlet.java b/src/main/java/com/example/core/Mesarios/Controller/MesarioRemoveServlet.java new file mode 100644 index 0000000..70f86ab --- /dev/null +++ b/src/main/java/com/example/core/Mesarios/Controller/MesarioRemoveServlet.java @@ -0,0 +1,31 @@ +package com.example.core.Mesarios.Controller; + +import com.example.core.Mesarios.Repository.MesarioRepository; +import com.example.core.Utils.Utils; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@WebServlet( value = "/mesario-remove") +public class MesarioRemoveServlet extends HttpServlet { + + MesarioRepository mesarioRepository = new MesarioRepository(); + Utils utils = new Utils(); + + // DELETE MESARIO + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + Long id = utils.getId(request.getParameter("id")); + System.out.println("Delete: "+ id); + + if (utils.isZero(id)) response.sendRedirect("/mesarios"); + + mesarioRepository.remove(id); + + response.sendRedirect(request.getContextPath() + "/mesarios"); + } +} diff --git a/src/main/java/com/example/core/Mesarios/Controller/MesariosServlet.java b/src/main/java/com/example/core/Mesarios/Controller/MesariosServlet.java new file mode 100644 index 0000000..8d425e7 --- /dev/null +++ b/src/main/java/com/example/core/Mesarios/Controller/MesariosServlet.java @@ -0,0 +1,68 @@ +package com.example.core.Mesarios.Controller; + +import com.example.core.Mesarios.Model.Mesario; +import com.example.core.Mesarios.Repository.MesarioRepository; +import com.example.core.Utils.Utils; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.List; + +@WebServlet(value = "/mesarios") +public class MesariosServlet extends HttpServlet { + + private MesarioRepository mesarioRepository = new MesarioRepository(); + private Utils utils = new Utils(); + + // LISTAGEM MESARIOS + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + List mesarios = mesarioRepository.findAll(); + + request.setAttribute("mesarios", mesarios); + RequestDispatcher requestDispatcher = request.getRequestDispatcher("mesarios/mesarios.jsp"); + requestDispatcher.forward(request,response); + } + + // SALVAR MESARIO + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + Long id = utils.getId(request.getParameter("id")); + String nome = request.getParameter("nome"); + String cpf = request.getParameter("cpf"); + String numeroTelefone = request.getParameter("numeroTelefone"); + + if (validaDuplicado(id,cpf)){ + response.sendRedirect("mesarios/mesario-duplicado.html"); + }else { + if (utils.isZero(id)) { + System.out.println("doPost: new mesario"); + Mesario candidato = new Mesario( nome, cpf, numeroTelefone); + mesarioRepository.save(candidato); + } else { + System.out.println("doPost mesario: update: " + id); + Mesario candidato = mesarioRepository.findOne(id).orElse(new Mesario()); + candidato.setNome(nome); + candidato.setCpf(cpf); + candidato.setNumeroTelefone(numeroTelefone); + mesarioRepository.save(candidato); + } + response.sendRedirect(request.getRequestURI()); + } + } + + private boolean validaDuplicado(Long id, String cpf){ + List mesarios = mesarioRepository.findAll(); + for (Mesario mesario: mesarios){ + if (mesario.getCpf().equals(cpf) && !mesario.getId().equals(id)){ + return true; + } + } + return false; + } +} diff --git a/src/main/java/com/example/core/Mesarios/Model/Mesario.java b/src/main/java/com/example/core/Mesarios/Model/Mesario.java new file mode 100644 index 0000000..09155a0 --- /dev/null +++ b/src/main/java/com/example/core/Mesarios/Model/Mesario.java @@ -0,0 +1,34 @@ +package com.example.core.Mesarios.Model; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +@Entity +public class Mesario { + + @Id + @GeneratedValue + private Long id; + @Column + private String nome; + @Column + private String cpf; + @Column + private String numeroTelefone; + + public Mesario(String nome, String cpf, String numeroTelefone) { + this.nome = nome; + this.cpf = cpf; + this.numeroTelefone = numeroTelefone; + } +} diff --git a/src/main/java/com/example/core/Mesarios/Repository/MesarioRepository.java b/src/main/java/com/example/core/Mesarios/Repository/MesarioRepository.java new file mode 100644 index 0000000..9336d7c --- /dev/null +++ b/src/main/java/com/example/core/Mesarios/Repository/MesarioRepository.java @@ -0,0 +1,63 @@ +package com.example.core.Mesarios.Repository; + +import com.example.core.Mesarios.Model.Mesario; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +public class MesarioRepository { + EntityManagerFactory entityManagerFactory; + EntityManager entityManager; + + public MesarioRepository() { + entityManagerFactory = Persistence.createEntityManagerFactory("votacao-jpa"); + entityManager = entityManagerFactory.createEntityManager(); + } + + public void save(Mesario mesario) { + try { + entityManager.getTransaction().begin(); + entityManager.persist(mesario); + entityManager.getTransaction().commit(); + }catch (Exception e){ + System.out.println(e); + } + } + + public void remove(Long id) { + try { + Mesario mesario = findOne(id).orElse(null); + entityManager.getTransaction().begin(); + entityManager.remove(mesario); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + System.out.println(e); + } + } + + public Optional findOne(Long id){ + Mesario mesario = new Mesario(); + try { + mesario = entityManager.find(Mesario.class, id); + }catch (Exception e){ + System.out.println(e); + } + return Optional.ofNullable(mesario); + } + + @SuppressWarnings("unchecked") + public List findAll(){ + List mesarios = Collections.emptyList(); + try { + mesarios = entityManager.createQuery("select mesario from Mesario mesario").getResultList(); + } + catch (Exception e){ + System.out.println(e); + } + return mesarios; + } +} diff --git a/src/main/java/com/example/core/Model/Candidato.java b/src/main/java/com/example/core/Model/Candidato.java deleted file mode 100644 index c232108..0000000 --- a/src/main/java/com/example/core/Model/Candidato.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.example.core.Model; - -public class Candidato { - private String id; - private String nome; - private Integer numeroCandidato; - - public Candidato(){} - - public Candidato(String id, String nome, Integer numeroCandidato) { - this.id = id; - this.nome = nome; - this.numeroCandidato = numeroCandidato; - } - - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getNome() { - return nome; - } - - public void setNome(String nome) { - this.nome = nome; - } - - public Integer getNumeroCandidato() { - return numeroCandidato; - } - - public void setNumeroCandidato(Integer numeroCandidato) { - this.numeroCandidato = numeroCandidato; - } -} diff --git a/src/main/java/com/example/core/Model/Voto.java b/src/main/java/com/example/core/Model/Voto.java deleted file mode 100644 index 839dd6e..0000000 --- a/src/main/java/com/example/core/Model/Voto.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.example.core.Model; - -public class Voto { - - private String id; - private Candidato candidato; - - public Voto(){} - - public Voto(String id, Candidato candidato) { - this.id = id; - this.candidato = candidato; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Candidato getCandidato() { - return candidato; - } - - public void setCandidato(Candidato candidato) { - this.candidato = candidato; - } -} diff --git a/src/main/java/com/example/core/Relatorios/Controller/relatorioServlet.java b/src/main/java/com/example/core/Relatorios/Controller/relatorioServlet.java new file mode 100644 index 0000000..ca3712d --- /dev/null +++ b/src/main/java/com/example/core/Relatorios/Controller/relatorioServlet.java @@ -0,0 +1,45 @@ +package com.example.core.Relatorios.Controller; + +import com.example.core.Canditatos.Model.Candidato; +import com.example.core.Mesarios.Model.Mesario; +import com.example.core.Mesarios.Repository.MesarioRepository; +import com.example.core.Votos.Model.Voto; +import com.example.core.Canditatos.Repository.CandidatoRepository; +import com.example.core.Votos.Repository.VotoRepository; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +@WebServlet( value = "/relatorios") +public class relatorioServlet extends HttpServlet { + + private List mesarios = new ArrayList<>(); + private List votos = new ArrayList<>(); + private List candidatos = new ArrayList<>(); + + private MesarioRepository mesarioRepository = new MesarioRepository(); + private VotoRepository votoRepository = new VotoRepository(); + private CandidatoRepository candidatoRepository = new CandidatoRepository(); + + // PAGE VOTOS + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + System.out.println("doGet: relatorios"); + + this.mesarios = mesarioRepository.findAll(); + this.votos = votoRepository.findAll(); + this.candidatos = candidatoRepository.findAll(); + + request.setAttribute("mesarios", this.mesarios); + request.setAttribute("candidatos", this.candidatos); + request.setAttribute("votos", this.votos); + RequestDispatcher requestDispatcher = request.getRequestDispatcher("relatorios/relatorio.jsp"); + requestDispatcher.forward(request,response); + } +} diff --git a/src/main/java/com/example/core/Servlet/candidatosServlet/CandidatoEditServlet.java b/src/main/java/com/example/core/Servlet/candidatosServlet/CandidatoEditServlet.java deleted file mode 100644 index e8912a1..0000000 --- a/src/main/java/com/example/core/Servlet/candidatosServlet/CandidatoEditServlet.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.example.core.Servlet.candidatosServlet; - -import com.example.core.Model.Candidato; -import com.example.core.View.CandidatosViews.CandidatoHTMLCreator; - -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; -import java.io.IOException; -import java.io.PrintWriter; -import java.util.List; -import java.util.Objects; -import java.util.regex.Pattern; - -@WebServlet( value = "/candidato-edit") -public class CandidatoEditServlet extends HttpServlet { - - private CandidatoHTMLCreator candidatoHTMLCreator = new CandidatoHTMLCreator(); - private static Pattern pattern = Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"); - - // EDIT CANDIDATO - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - HttpSession session = request.getSession(); - PrintWriter out = response.getWriter(); - String id = String.valueOf(request.getParameter("id")); - Candidato candidato = new Candidato(); - - List candidatos = (List)session.getAttribute("candidatos"); - if(pattern.matcher(id).matches() && Objects.nonNull(candidatos)) { - candidato = candidatos.stream().filter(cand -> cand.getId().equals(id)).findFirst().orElse(null); - } - out.println(candidatoHTMLCreator.getPageHtml(candidato)); - } -} diff --git a/src/main/java/com/example/core/Servlet/candidatosServlet/CandidatoRemoveServlet.java b/src/main/java/com/example/core/Servlet/candidatosServlet/CandidatoRemoveServlet.java deleted file mode 100644 index fe0fe66..0000000 --- a/src/main/java/com/example/core/Servlet/candidatosServlet/CandidatoRemoveServlet.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.example.core.Servlet.candidatosServlet; - -import com.example.core.Model.Candidato; - -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; -import java.io.IOException; -import java.util.*; -import java.util.stream.Collectors; - -@WebServlet( value = "/candidato-remove") -public class CandidatoRemoveServlet extends HttpServlet { - - private List candidatos = new ArrayList<>(); - - // DELETE CANDIDATO - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - String id = String.valueOf(request.getParameter("id")); - HttpSession session=request.getSession(); - - if (Objects.nonNull(session.getAttribute("candidatos"))){ - candidatos = (List)session.getAttribute("candidatos"); - } - - System.out.println("Delete: "+ id); - - if (Objects.isNull(id)) response.sendRedirect("/candidatos"); - - candidatos = candidatos.stream().filter(candidato -> !candidato.getId().equals(id)).collect(Collectors.toList()); - - session.setAttribute("candidatos",candidatos); - response.sendRedirect(request.getContextPath() + "/candidatos"); - } -} diff --git a/src/main/java/com/example/core/Servlet/candidatosServlet/CandidatosServlet.java b/src/main/java/com/example/core/Servlet/candidatosServlet/CandidatosServlet.java deleted file mode 100644 index 7bdf7bf..0000000 --- a/src/main/java/com/example/core/Servlet/candidatosServlet/CandidatosServlet.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.example.core.Servlet.candidatosServlet; - -import com.example.core.Model.Candidato; -import com.example.core.View.CandidatosViews.CandidatosHTMLCreator; - -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; -import java.io.IOException; -import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.UUID; - - -@WebServlet(value = "/candidatos") -public class CandidatosServlet extends HttpServlet { - private CandidatosHTMLCreator candidatosHTMLCreator = new CandidatosHTMLCreator(); - private List candidatos = new ArrayList<>(); - - // LISTAGEM CANDIDATOS - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - HttpSession session = request.getSession(); - PrintWriter out = response.getWriter(); - - if (session.isNew()) { - out.println(candidatosHTMLCreator.getTableHtml(candidatos)); - } - else { - List candidatosSession = (List) session.getAttribute("candidatos"); - this.candidatos = Objects.isNull(candidatosSession) ? this.candidatos : candidatosSession; - out.println(candidatosHTMLCreator.getTableHtml(this.candidatos)); - } - } - - // SALVAR CANDIDATO - @Override - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - HttpSession session=request.getSession(); - String id = request.getParameter("id"); - String nome = request.getParameter("nome"); - String numero = request.getParameter("numero"); - - if (validaDuplicado(nome,numero, response)){ - response.sendRedirect("candidato-duplicado.html"); - }else { - if (Objects.isNull(id) || id.length() == 0) { - System.out.println("doPost: new candidato"); - Candidato candidato = new Candidato(UUID.randomUUID().toString(), nome, Integer.valueOf(numero)); - candidatos.add(candidato); - session.setAttribute("candidatos", candidatos); - } else { - System.out.println("doPost: " + id); - for (Candidato candidato : candidatos) { - if (candidato.getId().equals(id)) { - candidato.setNome(nome); - candidato.setNumeroCandidato(Integer.valueOf(numero)); - } - } - } - response.sendRedirect(request.getRequestURI()); - } - } - - private boolean validaDuplicado(String nome, String numero, HttpServletResponse response){ - for (Candidato candidato: candidatos){ - if (candidato.getNome().equals(nome) && candidato.getNumeroCandidato().equals(Integer.valueOf(numero))){ - return true; - } - } - return false; - } -} diff --git a/src/main/java/com/example/core/Servlet/relatoriosServlet/relatorioServlet.java b/src/main/java/com/example/core/Servlet/relatoriosServlet/relatorioServlet.java deleted file mode 100644 index a4b8794..0000000 --- a/src/main/java/com/example/core/Servlet/relatoriosServlet/relatorioServlet.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.example.core.Servlet.relatoriosServlet; - -import com.example.core.Model.Candidato; -import com.example.core.Model.Voto; -import com.example.core.View.Relatorios.RelatorioViewCreator; - -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; -import java.io.IOException; -import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; - -@WebServlet( value = "/relatorios") -public class relatorioServlet extends HttpServlet { - - private List votos = new ArrayList<>(); - private List candidatos = new ArrayList<>(); - private RelatorioViewCreator relatorioViewCreator = new RelatorioViewCreator(); - - // PAGE VOTOS - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - System.out.println("doGet: relatorios"); - - HttpSession session = request.getSession(); - PrintWriter out = response.getWriter(); - - List votosSession = (List) session.getAttribute("votos"); - this.votos = Objects.isNull(votosSession) ? this.votos : votosSession; - - List candidatosSession = (List) session.getAttribute("candidatos"); - this.candidatos = Objects.isNull(candidatosSession) ? this.candidatos : candidatosSession; - - out.println(relatorioViewCreator.getTableHtml(candidatos,votos)); - } -} diff --git a/src/main/java/com/example/core/Servlet/votosServlet/VotoEditServlet.java b/src/main/java/com/example/core/Servlet/votosServlet/VotoEditServlet.java deleted file mode 100644 index 2a1f721..0000000 --- a/src/main/java/com/example/core/Servlet/votosServlet/VotoEditServlet.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.example.core.Servlet.votosServlet; - -import com.example.core.Model.Candidato; -import com.example.core.Model.Voto; -import com.example.core.View.VotosViews.VotoHTMLCreator; -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; -import java.io.IOException; -import java.io.PrintWriter; -import java.util.List; -import java.util.Objects; -import java.util.regex.Pattern; - -@WebServlet( value = "/voto-edit") -public class VotoEditServlet extends HttpServlet { - - private VotoHTMLCreator votoHTMLCreator = new VotoHTMLCreator(); - private static Pattern pattern = Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"); - - // EDIT VOTO - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - HttpSession session = request.getSession(); - PrintWriter out = response.getWriter(); - String id = String.valueOf(request.getParameter("id")); - Voto voto = new Voto(); - - List candidatos = (List)session.getAttribute("candidatos"); - List votosSession = (List) session.getAttribute("votos"); - - if(pattern.matcher(id).matches() && Objects.nonNull(votosSession)) { - voto = votosSession.stream().filter(vot -> vot.getId().equals(id)).findFirst().orElse(null); - } - out.println(votoHTMLCreator.getPageHtml(voto,candidatos)); - } -} diff --git a/src/main/java/com/example/core/Servlet/votosServlet/VotoRemoveServlet.java b/src/main/java/com/example/core/Servlet/votosServlet/VotoRemoveServlet.java deleted file mode 100644 index e835985..0000000 --- a/src/main/java/com/example/core/Servlet/votosServlet/VotoRemoveServlet.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.example.core.Servlet.votosServlet; - -import com.example.core.Model.Voto; - -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.stream.Collectors; - -@WebServlet(value = "/voto-remove") -public class VotoRemoveServlet extends HttpServlet { - - private List votos = new ArrayList<>(); - - // DELETE CANDIDATO - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - String id = String.valueOf(request.getParameter("id")); - HttpSession session=request.getSession(); - - if (Objects.nonNull(session.getAttribute("votos"))){ - votos = (List)session.getAttribute("votos"); - } - - System.out.println("Delete: "+ id); - - if (Objects.isNull(id)) response.sendRedirect("/votos"); - - votos = votos.stream().filter(voto -> !voto.getId().equals(id)).collect(Collectors.toList()); - - session.setAttribute("votos",votos); - response.sendRedirect(request.getContextPath() + "/votos"); - } -} diff --git a/src/main/java/com/example/core/Servlet/votosServlet/VotoServlet.java b/src/main/java/com/example/core/Servlet/votosServlet/VotoServlet.java deleted file mode 100644 index 908f92b..0000000 --- a/src/main/java/com/example/core/Servlet/votosServlet/VotoServlet.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.example.core.Servlet.votosServlet; - -import com.example.core.Model.Candidato; -import com.example.core.Model.Voto; -import com.example.core.View.VotosViews.VotoHTMLCreator; - -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; -import java.io.IOException; -import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.UUID; - -@WebServlet( value = "/voto") -public class VotoServlet extends HttpServlet { - - private List votos = new ArrayList<>(); - private List candidatos = new ArrayList<>(); - private VotoHTMLCreator votoHTMLCreator = new VotoHTMLCreator(); - -// PAGE VOTACAO - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - System.out.println("doGet: voto"); - - HttpSession session = request.getSession(); - PrintWriter out = response.getWriter(); - - List candidatosSession = (List) session.getAttribute("candidatos"); - this.candidatos = Objects.isNull(candidatosSession) ? this.candidatos : candidatosSession; - - if(candidatos.isEmpty()) response.sendRedirect("sem-candidatos.html"); - - out.println(votoHTMLCreator.getPageHtml(new Voto(),candidatos)); - } - -// SALVAR VOTO - @Override - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - HttpSession session=request.getSession(); - String id = request.getParameter("id"); - String candidatoID = request.getParameter("candidato"); - if (candidatoID.isEmpty()) response.sendRedirect("candidatos"); - - try { - if (Objects.nonNull(session.getAttribute("votos"))){ - votos = (List)session.getAttribute("votos"); - } - - Candidato _candidato = candidatos.stream().filter(can -> can.getId().equals(candidatoID)).findFirst().orElse(null); - if (Objects.isNull(_candidato)) response.sendRedirect("candidatos"); - - if (Objects.isNull(id) || id.isEmpty()) { - System.out.println("doPost: new Voto"); - Voto voto = new Voto(UUID.randomUUID().toString(), _candidato); - votos.add(voto); - }else{ - System.out.println("doPost: "+ id); - for(Voto voto : votos){ - if (voto.getId().equals(id)){ - voto.setCandidato(_candidato); - } - } - } - session.setAttribute("votos", votos); - }catch (Exception e){ - response.sendRedirect("voto-error.html"); - } - response.sendRedirect("voto-success.html"); - } -} diff --git a/src/main/java/com/example/core/Servlet/votosServlet/VotosServlet.java b/src/main/java/com/example/core/Servlet/votosServlet/VotosServlet.java deleted file mode 100644 index c76c768..0000000 --- a/src/main/java/com/example/core/Servlet/votosServlet/VotosServlet.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.example.core.Servlet.votosServlet; - -import com.example.core.Model.Voto; -import com.example.core.View.VotosViews.VotosHTMLCreator; -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; -import java.io.IOException; -import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; - -@WebServlet( value = "/votos") -public class VotosServlet extends HttpServlet { - - private List votos = new ArrayList<>(); - private VotosHTMLCreator votosHTMLCreator = new VotosHTMLCreator(); - -// PAGE VOTOS - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - System.out.println("doGet: votos"); - - HttpSession session = request.getSession(); - PrintWriter out = response.getWriter(); - - List votosSession = (List) session.getAttribute("votos"); - this.votos = Objects.isNull(votosSession) ? this.votos : votosSession; - out.println(votosHTMLCreator.getTableHtml(votos)); - } -} diff --git a/src/main/java/com/example/core/Utils/Utils.java b/src/main/java/com/example/core/Utils/Utils.java new file mode 100644 index 0000000..f519d25 --- /dev/null +++ b/src/main/java/com/example/core/Utils/Utils.java @@ -0,0 +1,20 @@ +package com.example.core.Utils; + +import java.util.Objects; + +public class Utils{ + private String NULL = "null"; + private Long ZERO = 0L; + + public Long getId(String value){ + return Objects.isNull(value) || NULL.equals(value) || value.isEmpty() ? 0L : Long.parseLong(value); + } + + public Long getZERO() { + return ZERO; + } + + public Boolean isZero(Long value) { + return ZERO.equals(value); + } +} diff --git a/src/main/java/com/example/core/View/CandidatosViews/CandidatoHTMLCreator.java b/src/main/java/com/example/core/View/CandidatosViews/CandidatoHTMLCreator.java deleted file mode 100644 index ea4f832..0000000 --- a/src/main/java/com/example/core/View/CandidatosViews/CandidatoHTMLCreator.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.example.core.View.CandidatosViews; - -import com.example.core.Model.Candidato; - -import java.util.Objects; - -public class CandidatoHTMLCreator { - - public String getPageHtml(Candidato candidato){ - String page = " " + - " " + - "Novo Candidato " + - "" + - " " + - "" + - "
" + - "
" + - "
" + - "
" + - "
" + - "
" + - "
" + - " " + - " " + - "
" + - "
" + - " " + - " " + - "
" + - "
" + - " " + - " " + - "
" + - "
" + - " " + - "
" + - "
" + - "
" + - "
" + - "
" + - "
" + - "
" + - " " + - " "; - return page; - } - private String formatId(String uuid){ - String value = Objects.isNull(uuid) ? "\"\"" : uuid; - return "value=".concat(value); - } - - private String formatNome(String nome){ - String value = Objects.isNull(nome) ? "\"\"" : nome; - return "value=".concat(value); - } - - private String formatNumeroCandidato(Integer numeroCandidato){ - String value = Objects.isNull(numeroCandidato) ? "\"\"" : String.valueOf(numeroCandidato); - return "value=".concat(value); - } -} diff --git a/src/main/java/com/example/core/View/CandidatosViews/CandidatosHTMLCreator.java b/src/main/java/com/example/core/View/CandidatosViews/CandidatosHTMLCreator.java deleted file mode 100644 index 59dd2bc..0000000 --- a/src/main/java/com/example/core/View/CandidatosViews/CandidatosHTMLCreator.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.example.core.View.CandidatosViews; - -import com.example.core.Model.Candidato; - -import java.util.List; - -public class CandidatosHTMLCreator { - - public String getTableHtml(List candidatos){ - String page = "" + - " " + - " " + - "Candidatos " + - "" + - "" + - "" + - "
" + - "Cadastrar candidato" + - "Home" + - "
" + - - "
" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - ""; - - String dados = ""; - for (Candidato candidato : candidatos) { - dados += "" + - "" + - "" + - "" + - "" + - "" + - "" ; - } - page += dados; - page += ""; - page += " \n" + - " \n" + - " \n" + - " \n" + - " "; - page += "
IdNomeNumero Candidato
" + candidato.getId() + "" + candidato.getNome() + "" + candidato.getNumeroCandidato() + "
Total registros: "+ candidatos.size() +"
"; - page += "
"; - page += ""; - page += ""; - - return page; - } -} diff --git a/src/main/java/com/example/core/View/Relatorios/RelatorioViewCreator.java b/src/main/java/com/example/core/View/Relatorios/RelatorioViewCreator.java deleted file mode 100644 index 1b43df8..0000000 --- a/src/main/java/com/example/core/View/Relatorios/RelatorioViewCreator.java +++ /dev/null @@ -1,103 +0,0 @@ -package com.example.core.View.Relatorios; - -import com.example.core.Model.Candidato; -import com.example.core.Model.Voto; - -import java.util.List; -import java.util.stream.Collectors; - -public class RelatorioViewCreator { - - public String getTableHtml(List candidatos, List votos){ - String page = "" + - " " + - " " + - "Candidatos " + - "" + - "" + - "" + - "
" + - "Home" + - "
" + - - "
" + - "

\n" + - " Candidatos\n" + - "

" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - ""; - - String dados = ""; - for (Candidato candidato : candidatos) { - dados += "" + - "" + - "" + - "" + - "" + - "" ; - } - page += dados; - page += ""; - page += " \n" + - " \n" + - " \n" + - " \n" + - " "; - page += "
IdNomeNumero CandidatoTotal de votos recebidos
" + candidato.getId() + "" + candidato.getNome() + "" + candidato.getNumeroCandidato() + "" + totalVotosByCandidato(votos, candidato) + "
Total registros: "+ candidatos.size() +"
"; - - page += getTableVoto(votos); - - page += "
"; - page += ""; - page += ""; - - return page; - } - - public String getTableVoto(List votos){ - String page = - "

\n" + - " Votos\n" + - "

" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - ""; - - String dados = ""; - for (Voto voto : votos) { - dados += "" + - "" + - "" + - "" + - "" ; - } - page += dados; - page += ""; - page += " \n" + - " \n" + - " \n" + - " \n" + - " "; - page += "
Id votoNome CandidatoNumero Candidato
" + voto.getId() + "" + voto.getCandidato().getNome() + "" + voto.getCandidato().getNumeroCandidato() + "
Total registros: "+ votos.size() +"
"; - - return page; - } - - private Integer totalVotosByCandidato(List votos, Candidato candidato) { - return votos.stream().filter(voto -> voto.getCandidato().equals(candidato)).collect(Collectors.toList()).size(); - } -} diff --git a/src/main/java/com/example/core/View/VotosViews/VotoHTMLCreator.java b/src/main/java/com/example/core/View/VotosViews/VotoHTMLCreator.java deleted file mode 100644 index 199f18b..0000000 --- a/src/main/java/com/example/core/View/VotosViews/VotoHTMLCreator.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.example.core.View.VotosViews; - -import com.example.core.Model.Candidato; -import com.example.core.Model.Voto; - -import java.util.List; -import java.util.Objects; - -public class VotoHTMLCreator { - - public String getPageHtml(Voto voto, List candidatos){ - String page = "\n" + - "\n" + - " \n" + - " Novo Candidato\n" + - " \n" + - "\n" + - "\n" + - "
\n" + - "
\n" + - "
\n" + - "
\n" + - "
\n" + - "
\n" + - "
\n" + - " \n" + - " \n" + - "
\n" + - "
\n" + - " \n" + - " \n" + - "
\n" + - "
\n" + - " \n" + - " Home\n" + - " \n" + - "
\n" + - "
\n" + - "
\n" + - "
\n" + - "
\n" + - "
\n" + - "
\n" + - "\n" + - ""; - return page; - } - private String formatId(String uuid){ - String value = Objects.isNull(uuid) ? "\"\"" : uuid; - return "value=".concat(value); - } - - private String generateOption(Voto voto,List candidatos){ - String dados = ""; - for (Candidato candidato : candidatos) { - dados += "\n"; - } - return dados; - } - - private String selectedCandidato(Voto voto, Candidato candidato){ - if (Objects.nonNull(voto.getCandidato())) { - if (voto.getCandidato().equals(candidato)) { - return "selected"; - } - } - return null; - } -} diff --git a/src/main/java/com/example/core/View/VotosViews/VotosHTMLCreator.java b/src/main/java/com/example/core/View/VotosViews/VotosHTMLCreator.java deleted file mode 100644 index 77468e0..0000000 --- a/src/main/java/com/example/core/View/VotosViews/VotosHTMLCreator.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.example.core.View.VotosViews; - -import com.example.core.Model.Voto; - -import java.util.List; - -public class VotosHTMLCreator { - - public String getTableHtml(List votos){ - String page = "" + - " " + - " " + - "Candidatos " + - "" + - "" + - "" + - "
" + - "Home" + - "
" + - - "
" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - ""; - - String dados = ""; - for (Voto voto : votos) { - dados += "" + - "" + - "" + - "" + - "" + - "" + - "" ; - } - page += dados; - page += ""; - page += " \n" + - " \n" + - " \n" + - " \n" + - " "; - page += "
Id votoNome CandidatoNumero Candidato
" + voto.getId() + "" + voto.getCandidato().getNome() + "" + voto.getCandidato().getNumeroCandidato() + "
Total registros: "+ votos.size() +"
"; - page += "
"; - page += ""; - page += ""; - - return page; - } -} diff --git a/src/main/java/com/example/core/Votos/Controller/VotoEditServlet.java b/src/main/java/com/example/core/Votos/Controller/VotoEditServlet.java new file mode 100644 index 0000000..ce164be --- /dev/null +++ b/src/main/java/com/example/core/Votos/Controller/VotoEditServlet.java @@ -0,0 +1,37 @@ +package com.example.core.Votos.Controller; + +import com.example.core.Canditatos.Model.Candidato; +import com.example.core.Utils.Utils; +import com.example.core.Votos.Model.Voto; +import com.example.core.Canditatos.Repository.CandidatoRepository; +import com.example.core.Votos.Repository.VotoRepository; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.List; + +@WebServlet( value = "/voto-edit") +public class VotoEditServlet extends HttpServlet { + + private VotoRepository votoRepository = new VotoRepository(); + private CandidatoRepository candidatoRepository = new CandidatoRepository(); + private Utils utils = new Utils(); + + // EDIT VOTO + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + Long id = utils.getId(request.getParameter("id")); + + List candidatos = candidatoRepository.findAll(); + Voto voto = votoRepository.findOne(id).orElse(new Voto()); + + request.setAttribute("candidatos", candidatos); + request.setAttribute("voto", voto); + RequestDispatcher requestDispatcher = request.getRequestDispatcher("votos/voto.jsp"); + requestDispatcher.forward(request,response); + } +} diff --git a/src/main/java/com/example/core/Votos/Controller/VotoRemoveServlet.java b/src/main/java/com/example/core/Votos/Controller/VotoRemoveServlet.java new file mode 100644 index 0000000..53e4f5e --- /dev/null +++ b/src/main/java/com/example/core/Votos/Controller/VotoRemoveServlet.java @@ -0,0 +1,28 @@ +package com.example.core.Votos.Controller; + +import com.example.core.Utils.Utils; +import com.example.core.Votos.Repository.VotoRepository; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Objects; + +@WebServlet(value = "/voto-remove") +public class VotoRemoveServlet extends HttpServlet { + + private VotoRepository votoRepository = new VotoRepository(); + private Utils utils = new Utils(); + + // DELETE CANDIDATO + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + Long id = utils.getId(request.getParameter("id")); + System.out.println("Delete: "+ id); + if (utils.isZero(id)) response.sendRedirect("/votos"); + votoRepository.remove(id); + response.sendRedirect(request.getContextPath() + "/votos"); + } +} diff --git a/src/main/java/com/example/core/Votos/Controller/VotoServlet.java b/src/main/java/com/example/core/Votos/Controller/VotoServlet.java new file mode 100644 index 0000000..d21104c --- /dev/null +++ b/src/main/java/com/example/core/Votos/Controller/VotoServlet.java @@ -0,0 +1,67 @@ +package com.example.core.Votos.Controller; + +import com.example.core.Canditatos.Model.Candidato; +import com.example.core.Utils.Utils; +import com.example.core.Votos.Model.Voto; +import com.example.core.Canditatos.Repository.CandidatoRepository; +import com.example.core.Votos.Repository.VotoRepository; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.List; +import java.util.Objects; + +@WebServlet( value = "/voto") +public class VotoServlet extends HttpServlet { + + private CandidatoRepository candidatoRepository = new CandidatoRepository(); + private VotoRepository votoRepository = new VotoRepository(); + private Utils utils = new Utils(); + +// PAGE VOTACAO + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + List candidatos = candidatoRepository.findAll(); + + System.out.println("doGet: voto"); + + if(candidatos.isEmpty()){ + response.sendRedirect("candidatos/sem-candidatos.html"); + } + else { + request.setAttribute("candidatos", candidatos); + request.setAttribute("voto", new Voto()); + RequestDispatcher requestDispatcher = request.getRequestDispatcher("votos/voto.jsp"); + requestDispatcher.forward(request, response); + } + } + +// SALVAR VOTO + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + Long id = utils.getId(request.getParameter("id")); + Long candidatoID = utils.getId(request.getParameter("candidato")); + if (utils.isZero(candidatoID)) response.sendRedirect("candidatos"); + try { + Candidato _candidato = candidatoRepository.findOne(candidatoID).orElse(null); + if (Objects.isNull(_candidato)) response.sendRedirect("candidatos"); + if (utils.isZero(id)) { + System.out.println("doPost: new Voto"); + Voto voto = new Voto(_candidato); + votoRepository.save(voto); + }else{ + System.out.println("doPost: update: "+ id); + Voto voto = votoRepository.findOne(id).orElse(new Voto()); + voto.setCandidato(_candidato); + votoRepository.save(voto); + } + }catch (Exception e){ + response.sendRedirect("votos/voto-error.html"); + } + response.sendRedirect("votos/voto-success.html"); + } +} diff --git a/src/main/java/com/example/core/Votos/Controller/VotosServlet.java b/src/main/java/com/example/core/Votos/Controller/VotosServlet.java new file mode 100644 index 0000000..6072912 --- /dev/null +++ b/src/main/java/com/example/core/Votos/Controller/VotosServlet.java @@ -0,0 +1,29 @@ +package com.example.core.Votos.Controller; + +import com.example.core.Votos.Model.Voto; +import com.example.core.Votos.Repository.VotoRepository; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.List; + +@WebServlet( value = "/votos") +public class VotosServlet extends HttpServlet { + + private VotoRepository votoRepository = new VotoRepository(); + +// PAGE VOTOS + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + System.out.println("doGet: votos"); + List votos = votoRepository.findAll(); + + request.setAttribute("votos", votos); + RequestDispatcher requestDispatcher = request.getRequestDispatcher("votos/votos.jsp"); + requestDispatcher.forward(request,response); + } +} diff --git a/src/main/java/com/example/core/Votos/Model/Voto.java b/src/main/java/com/example/core/Votos/Model/Voto.java new file mode 100644 index 0000000..5e4462d --- /dev/null +++ b/src/main/java/com/example/core/Votos/Model/Voto.java @@ -0,0 +1,29 @@ +package com.example.core.Votos.Model; + +import com.example.core.Canditatos.Model.Candidato; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import javax.persistence.*; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +@Entity +public class Voto { + + @Id + @GeneratedValue + private Long id; + + @ManyToOne + @JoinColumn(name = "candidato_id") + private Candidato candidato; + + public Voto(Candidato candidato) { + this.candidato = candidato; + } +} diff --git a/src/main/java/com/example/core/Votos/Repository/VotoRepository.java b/src/main/java/com/example/core/Votos/Repository/VotoRepository.java new file mode 100644 index 0000000..80e705e --- /dev/null +++ b/src/main/java/com/example/core/Votos/Repository/VotoRepository.java @@ -0,0 +1,64 @@ +package com.example.core.Votos.Repository; + +import com.example.core.Votos.Model.Voto; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +public class VotoRepository { + EntityManagerFactory entityManagerFactory; + EntityManager entityManager; + + public VotoRepository() { + entityManagerFactory = Persistence.createEntityManagerFactory("votacao-jpa"); + entityManager = entityManagerFactory.createEntityManager(); + } + + public void save(Voto voto) { + try { + entityManager.getTransaction().begin(); + entityManager.persist(voto); + entityManager.getTransaction().commit(); + }catch (Exception e){ + System.out.println(e); + } + } + + public void remove(Long id) { + try { + Voto voto = findOne(id).orElse(null); + entityManager.getTransaction().begin(); + entityManager.remove(voto); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + System.out.println(e); + } + } + + public Optional findOne(Long id){ + Voto voto = new Voto(); + try { + voto = entityManager.find(Voto.class, id); + }catch (Exception e){ + System.out.println(e); + } + return Optional.ofNullable(voto); + } + + @SuppressWarnings("unchecked") + public List findAll(){ + List votos = Collections.emptyList(); + try { + votos = entityManager.createQuery("select voto from Voto voto").getResultList(); + } + catch (Exception e){ + System.out.println(e); + } + return votos; + } +} diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000..53a05b6 --- /dev/null +++ b/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + diff --git a/src/main/webapp/candidato-duplicado.html b/src/main/webapp/candidatos/candidato-duplicado.html similarity index 100% rename from src/main/webapp/candidato-duplicado.html rename to src/main/webapp/candidatos/candidato-duplicado.html diff --git a/src/main/webapp/candidatos/candidato.jsp b/src/main/webapp/candidatos/candidato.jsp new file mode 100644 index 0000000..c9885c9 --- /dev/null +++ b/src/main/webapp/candidatos/candidato.jsp @@ -0,0 +1,49 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + + + Novo Candidato + + + + +
+
+
+
+
+
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+
+
+
+
+
+ + + \ No newline at end of file diff --git a/src/main/webapp/candidatos/candidatos.jsp b/src/main/webapp/candidatos/candidatos.jsp new file mode 100644 index 0000000..cb6f873 --- /dev/null +++ b/src/main/webapp/candidatos/candidatos.jsp @@ -0,0 +1,54 @@ +<%@ page import="com.example.core.Canditatos.Model.Candidato" %> +<%@ page import="java.util.List" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + + + Candidatos + + + + + + + +
+ + + + + + + + + + + + + <% List candidatos = (List) request.getAttribute("candidatos"); + for (Candidato candidato : candidatos) { %> + + + + + + + + <% } %> + + + + + + + +
IdNomeNumero Candidato
<%= candidato.getId() %> <%= candidato.getNome() %> <%= candidato.getNumeroCandidato() %> class="fas fa-pencil-alt"> class="fas fa-trash-alt">
Total registros:${ candidatos.size() }
+
+ + + diff --git a/src/main/webapp/sem-candidatos.html b/src/main/webapp/candidatos/sem-candidatos.html similarity index 100% rename from src/main/webapp/sem-candidatos.html rename to src/main/webapp/candidatos/sem-candidatos.html diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index 3ac5497..55f6fa2 100644 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -12,8 +12,10 @@
- + + + + \ No newline at end of file diff --git a/src/main/webapp/mesarios/mesario.jsp b/src/main/webapp/mesarios/mesario.jsp new file mode 100644 index 0000000..f97c80b --- /dev/null +++ b/src/main/webapp/mesarios/mesario.jsp @@ -0,0 +1,55 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + + + Novo Candidato + + + + +
+
+
+
+
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+
+
+
+
+
+ + + \ No newline at end of file diff --git a/src/main/webapp/mesarios/mesarios.jsp b/src/main/webapp/mesarios/mesarios.jsp new file mode 100644 index 0000000..9bffb87 --- /dev/null +++ b/src/main/webapp/mesarios/mesarios.jsp @@ -0,0 +1,56 @@ +<%@ page import="java.util.List" %> +<%@ page import="com.example.core.Mesarios.Model.Mesario" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + + + Candidatos + + + + + + + +
+ + + + + + + + + + + + + + <% List mesarios = (List) request.getAttribute("mesarios"); + for (Mesario mesario : mesarios) { %> + + + + + + + + + <% } %> + + + + + + + +
IdNomeCpfNumero telefone
<%= mesario.getId() %> <%= mesario.getNome() %> <%= mesario.getCpf() %> <%= mesario.getNumeroTelefone() %> class="fas fa-pencil-alt"> class="fas fa-trash-alt">
Total registros:${ mesarios.size() }
+
+ + + diff --git a/src/main/webapp/relatorios/relatorio.jsp b/src/main/webapp/relatorios/relatorio.jsp new file mode 100644 index 0000000..a86ca92 --- /dev/null +++ b/src/main/webapp/relatorios/relatorio.jsp @@ -0,0 +1,119 @@ +<%@ page import="com.example.core.Canditatos.Model.Candidato" %> +<%@ page import="java.util.List" %> +<%@ page import="com.example.core.Votos.Model.Voto" %> +<%@ page import="com.example.core.Mesarios.Model.Mesario" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + + + Candidatos + + + + + +
+ Home +
+ +
+ +

+ Mesários +

+ + + + + + + + + + + + <% List mesarios = (List) request.getAttribute("mesarios"); + for (Mesario mesario : mesarios) { %> + + + + + + + <% } %> + + + + + + + +
IdNomeCpfNumero telefone
<%= mesario.getId() %> <%= mesario.getNome() %> <%= mesario.getCpf() %> <%= mesario.getNumeroTelefone() %>
Total registros:${ mesarios.size() }
+ +

+ Candidatos +

+ + + + + + + + + + + <% List candidatos = (List) request.getAttribute("candidatos"); + List votos = (List) request.getAttribute("votos"); + for (Candidato candidato : candidatos) { %> + + + + + + + <% } %> + + + + + + + +
IdNomeNumero CandidatoTotal de votos recebidos
<%= candidato.getId() %> <%= candidato.getNome() %> <%= candidato.getNumeroCandidato() %> <%= votos.stream().filter(voto -> voto.getCandidato().getId().equals(candidato.getId())).count() %>
Total registros: ${candidatos.size()}
+ +

+ Votos +

+ + + + + + + + + + + <%for (Voto voto : votos) {%> + + + + + + <% } %> + + + + + + + +
Id votoNome CandidatoNumero Candidato
<%= voto.getId()%> <%= voto.getCandidato().getNome()%> <%= voto.getCandidato().getNumeroCandidato()%>
Total registros:${votos.size()}
+ +
+ + + diff --git a/src/main/webapp/voto-error.html b/src/main/webapp/votos/voto-error.html similarity index 100% rename from src/main/webapp/voto-error.html rename to src/main/webapp/votos/voto-error.html diff --git a/src/main/webapp/voto-success.html b/src/main/webapp/votos/voto-success.html similarity index 100% rename from src/main/webapp/voto-success.html rename to src/main/webapp/votos/voto-success.html diff --git a/src/main/webapp/votos/voto.jsp b/src/main/webapp/votos/voto.jsp new file mode 100644 index 0000000..9b2308c --- /dev/null +++ b/src/main/webapp/votos/voto.jsp @@ -0,0 +1,65 @@ +<%@ page import="com.example.core.Canditatos.Model.Candidato" %> +<%@ page import="java.util.List" %> +<%@ page import="com.example.core.Votos.Model.Voto" %> +<%@ page import="java.util.Objects" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + + + Novo Candidato + + + + +
+
+
+
+
+
+
+ + +
+
+ + +
+
+ + +
+
+
+
+
+
+
+ + +<%! + private String selectedCandidato(Voto voto, Candidato candidato){ + if (Objects.nonNull(voto.getCandidato())) { + if (voto.getCandidato().getId().equals(candidato.getId())) { + return "selected"; + } + } + return null; + } +%> \ No newline at end of file diff --git a/src/main/webapp/votos/votos.jsp b/src/main/webapp/votos/votos.jsp new file mode 100644 index 0000000..a34395f --- /dev/null +++ b/src/main/webapp/votos/votos.jsp @@ -0,0 +1,55 @@ +<%@ page import="com.example.core.Votos.Model.Voto" %> +<%@ page import="java.util.List" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + + + Candidatos + + + + + +
+ Home +
+ +
+ + + + + + + + + + + + + <% List votos = (List) request.getAttribute("votos"); + for (Voto voto : votos) {%> + + + + + + + + <% } %> + + + + + + +
Id votoNome CandidatoNumero Candidato
<%= voto.getId()%> + <%= voto.getCandidato().getNome()%> + <%= voto.getCandidato().getNumeroCandidato()%> + class="fas fa-pencil-alt"> class="fas fa-trash-alt">
Total registros:${votos.size()}
+
+ + +