diff --git a/pom.xml b/pom.xml index 9f45ad2..2e03341 100644 --- a/pom.xml +++ b/pom.xml @@ -74,6 +74,11 @@ h2 runtime + + org.springframework.boot + spring-boot-starter-mail + + diff --git a/src/main/java/com/example/proyectofinal/controllers/EmailController.java b/src/main/java/com/example/proyectofinal/controllers/EmailController.java new file mode 100644 index 0000000..c9b1d87 --- /dev/null +++ b/src/main/java/com/example/proyectofinal/controllers/EmailController.java @@ -0,0 +1,7 @@ +package com.example.proyectofinal.controllers; + +import org.springframework.stereotype.Controller; + +@Controller +public class EmailController { +} diff --git a/src/main/java/com/example/proyectofinal/controllers/modelControllers/OfertaController.java b/src/main/java/com/example/proyectofinal/controllers/modelControllers/OfertaController.java index eca6414..60955c9 100644 --- a/src/main/java/com/example/proyectofinal/controllers/modelControllers/OfertaController.java +++ b/src/main/java/com/example/proyectofinal/controllers/modelControllers/OfertaController.java @@ -1,6 +1,7 @@ package com.example.proyectofinal.controllers.modelControllers; import com.example.proyectofinal.models.empresas.*; +import com.example.proyectofinal.servicios.MailService; import com.example.proyectofinal.servicios.empresa.CicloService; import com.example.proyectofinal.servicios.empresa.OfertaService; import com.example.proyectofinal.servicios.empresa.SkillService; @@ -35,7 +36,6 @@ public class OfertaController { @Autowired private CicloService cicloService; - @GetMapping("/admin/oferta/create") public String showCreateForm(Model model, Authentication authentication) { Collection authorities = authentication.getAuthorities(); @@ -70,6 +70,7 @@ public class OfertaController { return new ResponseEntity<>("Esta oferta ya existe en la base de datos", HttpStatus.BAD_REQUEST); }else { ofertaService.save(oferta); + ofertaService.mailOferta(oferta); return new ResponseEntity<>("La oferta fue guadada con exito", HttpStatus.OK); } }catch (Exception e) { @@ -100,7 +101,6 @@ public class OfertaController { @PostMapping("/oferta/update") public ResponseEntity updateOferta(Oferta oferta, @RequestParam("ciclo") Long ciclo, @RequestParam("sucursal") Long sucursal, @RequestParam("skills") List skills){ - System.out.println("UPDATE TEST"); try{ Ciclo cicloEntity = cicloService.findById(ciclo); Sucursal sucursalEntity = sucursalService.findById(sucursal); @@ -126,7 +126,7 @@ public class OfertaController { } try{ ofertaService.deleteById(id); - return new ResponseEntity<>("La oferta ha sido eliminado", HttpStatus.OK); + return new ResponseEntity<>("La oferta ha sido eliminada", HttpStatus.OK); }catch (Exception e){ return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } diff --git a/src/main/java/com/example/proyectofinal/database b/src/main/java/com/example/proyectofinal/database index a9bab67..d894b15 100644 --- a/src/main/java/com/example/proyectofinal/database +++ b/src/main/java/com/example/proyectofinal/database @@ -135,7 +135,6 @@ INSERT INTO Ofertas (nombre, descripcion, fecha, fk_sucursal,fk_ciclo) VALUES (' INSERT INTO Alumno_Skill (fk_alumno, fk_skills) VALUES (1, 1); -- Jorge with Java -INSERT INTO Alumno_Skill (fk_alumno, fk_skills) VALUES (2, 10); -- Jane with ... INSERT INTO Alumno_Skill (fk_alumno, fk_skills) VALUES (3, 2); -- Robert with Microsoft XL INSERT INTO Alumno_Skill (fk_alumno, fk_skills) VALUES (4, 14); -- Emily with Marketing INSERT INTO Alumno_Skill (fk_alumno, fk_skills) VALUES (5, 13); -- James with SQL @@ -149,6 +148,7 @@ INSERT INTO Alumno_Skill (fk_alumno, fk_skills) VALUES (12, 1); -- Angelina with INSERT INTO Alumno_Skill (fk_alumno, fk_skills) VALUES (13, 7); -- Leonardo with C++ INSERT INTO Alumno_Skill (fk_alumno, fk_skills) VALUES (14, 11); -- Scarlett with PHP INSERT INTO Alumno_Skill (fk_alumno, fk_skills) VALUES (15, 10); -- Hugh with JavaScript +INSERT INTO Alumno_Skill (fk_alumno, fk_skills) VALUES (2, 10); INSERT INTO Oferta_Skill (fk_oferta, fk_skills) VALUES (1, 1); INSERT INTO Oferta_Skill (fk_oferta, fk_skills) VALUES (2, 2); diff --git a/src/main/java/com/example/proyectofinal/repositories/empresas/AlumnoRepository.java b/src/main/java/com/example/proyectofinal/repositories/empresas/AlumnoRepository.java index 4cc08b6..663a95c 100644 --- a/src/main/java/com/example/proyectofinal/repositories/empresas/AlumnoRepository.java +++ b/src/main/java/com/example/proyectofinal/repositories/empresas/AlumnoRepository.java @@ -2,6 +2,7 @@ package com.example.proyectofinal.repositories.empresas; import com.example.proyectofinal.models.empresas.Alumno; +import com.example.proyectofinal.models.empresas.Skill; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; @@ -89,5 +90,8 @@ public interface AlumnoRepository extends JpaRepository { @Query("SELECT a FROM Alumno a WHERE lower(a.correo) LIKE %:query%") Alumno findByCorreo(String query); + + @Query("SELECT a FROM Alumno a JOIN a.skills s WHERE s IN :skills GROUP BY a HAVING COUNT(s) >= :count") + List findBySkillsIn(@Param("skills") Collection skills, @Param("count") long count); } diff --git a/src/main/java/com/example/proyectofinal/servicios/MailService.java b/src/main/java/com/example/proyectofinal/servicios/MailService.java new file mode 100644 index 0000000..46a04bc --- /dev/null +++ b/src/main/java/com/example/proyectofinal/servicios/MailService.java @@ -0,0 +1,45 @@ +package com.example.proyectofinal.servicios; + +import jakarta.mail.MessagingException; +import jakarta.mail.internet.MimeMessage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.stereotype.Service; + +@Service +public class MailService { + + private static final Logger LOGGER = LoggerFactory.getLogger(MailService.class); + + private final JavaMailSender emailSender; + + public MailService(JavaMailSender emailSender) { + this.emailSender = emailSender; + } + + public boolean sendEmail(String to, String subject, String text) { + LOGGER.info("Sending email..."); + return sendEmailTool(text, to, subject); + } + + private boolean sendEmailTool(String textMessage, String email, String subject ) { + String cc = "empresas@ies.edu.es"; + boolean send = false; + MimeMessage message = emailSender.createMimeMessage(); + MimeMessageHelper helper = new MimeMessageHelper(message); + try { + helper.setTo(email); + helper.setText(textMessage, true); + helper.setSubject(subject); + helper.setCc(cc); + emailSender.send(message); + send = true; + LOGGER.info("Mail sent!"); + } catch (MessagingException e) { + LOGGER.error("There was an error while sending the mail: {}", e.getMessage()); + } + return send; + } +} diff --git a/src/main/java/com/example/proyectofinal/servicios/empresa/AlumnoService.java b/src/main/java/com/example/proyectofinal/servicios/empresa/AlumnoService.java index b46ba2e..739e18d 100644 --- a/src/main/java/com/example/proyectofinal/servicios/empresa/AlumnoService.java +++ b/src/main/java/com/example/proyectofinal/servicios/empresa/AlumnoService.java @@ -1,6 +1,7 @@ package com.example.proyectofinal.servicios.empresa; import com.example.proyectofinal.models.empresas.Alumno; +import com.example.proyectofinal.models.empresas.Skill; import com.example.proyectofinal.repositories.empresas.AlumnoRepository; import com.example.proyectofinal.servicios.implemetations.empresas.IAlumno; import org.springframework.beans.factory.annotation.Autowired; @@ -11,6 +12,7 @@ import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Set; @Service public class AlumnoService implements IAlumno{ @@ -167,4 +169,7 @@ public class AlumnoService implements IAlumno{ return alumnoRepository.findByCorreo(email); } + public List findBySkillsIn(List skills) { + return alumnoRepository.findBySkillsIn(skills,skills.size()); + } } diff --git a/src/main/java/com/example/proyectofinal/servicios/empresa/OfertaService.java b/src/main/java/com/example/proyectofinal/servicios/empresa/OfertaService.java index 6212f5a..8a299fb 100644 --- a/src/main/java/com/example/proyectofinal/servicios/empresa/OfertaService.java +++ b/src/main/java/com/example/proyectofinal/servicios/empresa/OfertaService.java @@ -1,9 +1,11 @@ package com.example.proyectofinal.servicios.empresa; +import com.example.proyectofinal.models.empresas.Alumno; import com.example.proyectofinal.models.empresas.Oferta; import com.example.proyectofinal.models.empresas.Skill; import com.example.proyectofinal.models.empresas.Sucursal; import com.example.proyectofinal.repositories.empresas.OfertaRepository; +import com.example.proyectofinal.servicios.MailService; import com.example.proyectofinal.servicios.implemetations.empresas.IOferta; import jakarta.persistence.EntityManager; import org.springframework.beans.factory.annotation.Autowired; @@ -18,11 +20,13 @@ import java.util.*; @Service public class OfertaService implements IOferta { - @Autowired - private EntityManager entityManager; @Autowired private OfertaRepository ofertaRepository; + @Autowired + private MailService mailService; + @Autowired + private AlumnoService alumnoService; @Override @@ -205,4 +209,26 @@ public class OfertaService implements IOferta { return listEmpPrime; } + @Override + public void mailOferta(Oferta oferta) { + Set listSkill = oferta.getSkills(); + List skillArray = new ArrayList<>(listSkill); + List alumnos = alumnoService.findBySkillsIn(skillArray); + StringBuilder sbMessage = new StringBuilder(); + for (Alumno alumno : alumnos) { + sbMessage.append("Hola ").append(alumno.getNombre()).append(" ").append(alumno.getApellido()).append(",\n"); + sbMessage.append("Hemos encontrado una oferta que puede interesarte:\n"); + sbMessage.append("Nombre de la oferta: ").append(oferta.getNombre()).append("\n"); + sbMessage.append("Empresa: ").append(oferta.getSucursal().getEmpresa().getNombre()).append("\n"); + System.out.println("TEST " + alumno.getNombre()); + System.out.println("TEST CORREO " + alumno.getCorreo()); + //mailService.sendEmail(alumno.getCorreo(), "Nueva oferta de practicas", sbMessage.toString()); + if(alumno.getCorreo2() != null){ + //mailService.sendEmail(alumno.getCorreo2(), "Nueva oferta de practicas", sbMessage.toString()); + } + + } + } + + } diff --git a/src/main/java/com/example/proyectofinal/servicios/implemetations/empresas/IOferta.java b/src/main/java/com/example/proyectofinal/servicios/implemetations/empresas/IOferta.java index 8971865..407eda6 100644 --- a/src/main/java/com/example/proyectofinal/servicios/implemetations/empresas/IOferta.java +++ b/src/main/java/com/example/proyectofinal/servicios/implemetations/empresas/IOferta.java @@ -28,4 +28,6 @@ public interface IOferta extends IPagination { Page getPage(int pageNum, int size, String sortField, String sortDir, String query, String secondaryOption) throws ParseException; List searchCustom(String querySearchBar, String query, String secondaryOption); + + void mailOferta(Oferta oferta); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 60ac4f6..7c4ef4d 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -16,4 +16,24 @@ server.port=8080 #Dialecto spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect +#EMAIL DATA + +spring.mail.host=135.125.201.208 + +spring.mail.port=587 + +spring.mail.protocol=smtp + +spring.mail.username=empresas@ies.edu.es + +spring.mail.password=1234 + +spring.mail.properties.mail.smtp.auth=true + +spring.mail.properties.mail.smtp.starttls.enable=false + +spring.mail.properties.mail.smtp.starttls.required=false + +spring.mail.properties.mail.smtp.quitwait=false + diff --git a/src/main/resources/templates/admin/alumno/create.html b/src/main/resources/templates/admin/alumno/create.html index d285e20..8ca6b7d 100644 --- a/src/main/resources/templates/admin/alumno/create.html +++ b/src/main/resources/templates/admin/alumno/create.html @@ -74,7 +74,7 @@
- +
diff --git a/src/main/resources/templates/admin/oferta/create.html b/src/main/resources/templates/admin/oferta/create.html index 1211cd1..369c7bf 100644 --- a/src/main/resources/templates/admin/oferta/create.html +++ b/src/main/resources/templates/admin/oferta/create.html @@ -28,7 +28,7 @@ margin-right: 5px; width: 120px; height: 40px; - font-family: Verdana; + font-family: Verdana, Geneva, Tahoma, sans-serif; } form input[type="submit"], form input[type="button"] { width: 100px; diff --git a/src/main/resources/templates/list/ofertas.html b/src/main/resources/templates/list/ofertas.html index 05617bf..0768652 100644 --- a/src/main/resources/templates/list/ofertas.html +++ b/src/main/resources/templates/list/ofertas.html @@ -154,8 +154,6 @@ } function handleDelete(event) { - //console.log("handleDelete function called"); - //console.log("Delete icon clicked"); const row = event.target.closest('tr'); const rowId = row.dataset.id; var modal = document.getElementById("modalDelete"); @@ -177,6 +175,8 @@ success: function(response) { if (response === "La oferta ha sido eliminada") { alert("Oferta borrada con exito"); + modal.style.display = "none"; // Add this line to hide the modal + document.body.style.pointerEvents = 'auto'; window.location.reload(); } else { alert("Error al borrar oferta");