Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convertendo para JSP #1

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .github/assets/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/assets/10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/assets/11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .github/assets/9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed .github/assets/tree.jpeg
Binary file not shown.
Binary file added .github/assets/tree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 12 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<div align="center">
<img src=".github/assets/tree.jpeg" />
<img src=".github/assets/tree.png" />
</div>

## Telas do projeto
Expand All @@ -30,8 +20,16 @@
<div align="center">
<img src=".github/assets/1.png" />
</div>

<div align="center">
<h2>Mesários</h2>
</div>
<div align="center">
<img src=".github/assets/10.png" />
<img src=".github/assets/11.png" />
</div>

<div align="center">
<h2>Candidatos</h2>
</div>
<div align="center">
Expand Down
22 changes: 22 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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:
# <Port exposed> : < 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
23 changes: 23 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,30 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.12.Final</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.12.Final</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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<Candidato> 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;
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/example/core/Canditatos/Model/Candidato.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Candidato> 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<Candidato> findAll(){
List<Candidato> candidatos = Collections.emptyList();
try {
candidatos = entityManager.createQuery("select candidato from Candidato candidato").getResultList();
}
catch (Exception e){
System.out.println(e);
}
return candidatos;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Loading