NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ejb;

import dtos.CandidaturaDTO;
import dtos.DocumentDTO;
import dtos.PropostaDTO;
import dtos.StudentDTO;
import entities.Candidatura;
import entities.Document;
import entities.Proposta;
import entities.utilizadores.Estudante;
import entities.utilizadores.Instituicao;
import entities.utilizadores.Proponente;
import enums.Estado;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.mail.MessagingException;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.TransactionRequiredException;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
*
* @author Alexandre F.
*/
@Stateless
@Path("/candidaturas")
public class CandidaturaBean extends Bean<Candidatura> {
// Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Business Method")

@PersistenceContext
private EntityManager em;
@EJB
private EmailBean email;

@Override
public List<Candidatura> getAll() {
try {
return em.createNamedQuery("getAllCandidaturas", Candidatura.class).getResultList();
} catch (Exception ex) {
//thorw
}

return null;
}

public List<Candidatura> getAllMyCandidaturas(String username) {

try {
return em.createNamedQuery("getAllMyCandidaturas", Candidatura.class).setParameter("username", username).getResultList();
} catch (Exception ex) {
//thorw
}

return null;
}

public void criarCandidatura(List<Document> ficheiros, Proposta proposta, String username, Estado estadoCandidatura)
throws Exception {
Estudante est = em.find(Estudante.class, username);
if (est == null) {
throw new EntityNotFoundException("Estudante nao encontrado");
}
try {
Candidatura candidatura = new Candidatura(ficheiros, proposta, est, estadoCandidatura);
em.persist(candidatura);
} catch (EntityExistsException | TransactionRequiredException ex) {
// Ocurreu um erro ao gravar
throw new Exception("Erro ao gravar candidatura, tente de novo");
}
}

public void removerCandidatura(int id) {
Candidatura candidaturaAntiga = em.find(Candidatura.class, id);
if (candidaturaAntiga == null) {
throw new EntityNotFoundException("A candidatura a remover ja nao existe");
}
try {
em.remove(candidaturaAntiga);
} catch (TransactionRequiredException e) {
throw new EJBException("Erro ao eleminar candidatura");
}
}

@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("all")
public List<CandidaturaDTO> getAllCandidaturas() {
return toDTOs(getAll(), CandidaturaDTO.class);
}

@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("getAllEstadoPendente")
public List<CandidaturaDTO> getAllCandidaturasEstado() {
return toDTOs(getAllEstadoPendente(), CandidaturaDTO.class);
}

public List<Candidatura> getAllEstadoPendente() {
try {
return em.createNamedQuery("getAllCandidaturasEstado", Candidatura.class).setParameter("estado", Estado.PENDENTE).getResultList();
} catch (Exception ex) {
//thorw
}

return null;
}

@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("getAllEstadoAceite")
public List<CandidaturaDTO> getAllCandidaturasEstadoAceite() {
return toDTOs(getAllEstadoAceite(), CandidaturaDTO.class);
}

public List<Candidatura> getAllEstadoAceite() {
try {
List<Candidatura> lstCnew = new LinkedList<Candidatura>();
List<Candidatura> lstC = em.createNamedQuery("getAllCandidaturasEstado", Candidatura.class).setParameter("estado", Estado.ACEITE).getResultList();
for (Candidatura c : lstC) {
List<Proponente> lstP = c.getProposta().getProponentes();
for (Proponente p : lstP) {
if (p instanceof Instituicao) {
lstCnew.add(c);
break;
}
}
}
return lstCnew;
} catch (Exception ex) {
throw new EJBException(ex.getMessage());
}
}

@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("allMyCandidaturas/{username}")
public List<CandidaturaDTO> getAllMyCandidaturasREST(@PathParam("username") String username) {
return toDTOs(getAllMyCandidaturas(username), CandidaturaDTO.class);
}

@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("/{username}")
public CandidaturaDTO getCandidatura(@PathParam("username") int id) {
try {
Candidatura prof = em.find(Candidatura.class, id);
return candidaturaToDTO(prof);
} catch (Exception e) {
throw new EJBException(e.getMessage());
}
}

@PUT
@Path("/update")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public void updateREST(CandidaturaDTO candidatura) {
updateCandidatura(candidatura);
}

public void updateCandidatura(CandidaturaDTO dto) {
Candidatura candidatura = em.find(Candidatura.class, dto.getId());
List<Document> documents = new LinkedList<>();
for (DocumentDTO doc : dto.getFicheiros()) {
documents.add(new Document(doc.getFilepath(), doc.getDesiredName(), doc.getMimeType()));
}
candidatura.setFicheiros(documents);
}

@DELETE
@Path("/{id}")
public void removeStudent(@PathParam("id") String id) {
Candidatura cand = em.find(Candidatura.class, Integer.valueOf(id));
em.remove(cand);
}

@POST
@Path("/create")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public void createCandidaturaREST(CandidaturaDTO candidaturaDTO) {
try {
if (em.find(Proposta.class, candidaturaDTO.getProposta().getId()) == null) {
// throw exception
}
if (em.find(Estudante.class, candidaturaDTO.getEstudante().getUsername()) == null) {
//throw exception
}
Estudante std = em.find(Estudante.class, candidaturaDTO.getEstudante().getUsername());
Proposta prop = em.find(Proposta.class, candidaturaDTO.getProposta().getId());

List<Document> docs = new LinkedList<>();
for (DocumentDTO doc : candidaturaDTO.getFicheiros()) {
docs.add(new Document(doc.getFilepath(), doc.getDesiredName(), doc.getMimeType()));
}
Candidatura cand = new Candidatura(docs, prop, std, candidaturaDTO.getEstadoCandidatura());
std.addCandidatura(cand);
em.persist(cand);

} catch (Exception ex) {
throw new EJBException(ex.getMessage());
}
}

public CandidaturaDTO candidaturaToDTO(Candidatura candidatura) {
List<DocumentDTO> docs = new LinkedList<DocumentDTO>();
for (Document doc : candidatura.getFicheiros()) {
docs.add(new DocumentDTO(doc.getId(), doc.getFilepath(), doc.getDesiredName(), doc.getMimeType()));
}
return new CandidaturaDTO(
docs,
new PropostaDTO(candidatura.getProposta().getId()),
new StudentDTO(candidatura.getEstudante().getUsername()), Estado.ACEITE
);
}

@PUT
@Path("/validar")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public void AceitarCandidatraREST(CandidaturaDTO candidatura) throws Exception {
aceitarCandidatura(candidatura);
}

public void aceitarCandidatura(CandidaturaDTO candidatura) {
Candidatura cand = em.find(Candidatura.class, candidatura.getId());
cand.setEstadoCandidatura(Estado.ACEITE);
Proposta pro = em.find(Proposta.class, cand.getProposta().getId());
pro.setEstudante(cand.getEstudante());
try {
email.send(cand.getEstudante().getEmail(), "Estado de candidatura", cand.getEstadoCandidatura().toString());
} catch (MessagingException ex) {
Logger.getLogger(CandidaturaBean.class.getName()).log(Level.SEVERE, null, ex);
}
for (Proponente p : pro.getProponentes()) {
try {
email.send(p.getEmail(), "Estado de candidatura", cand.getEstadoCandidatura().toString());
} catch (MessagingException ex) {
Logger.getLogger(PropostaBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
try {
em.merge(cand);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Erro a fazer update");
}
List<Candidatura> cd = em.createNamedQuery("getAllCandidaturasPorProposta", Candidatura.class).setParameter("propostaId", cand.getProposta().getId()).getResultList();
for (Candidatura c : cd) {
c.setEstadoCandidatura(Estado.REJEITADA);
em.merge(c);
}
List<Candidatura> candEstud = em.createNamedQuery("getAllCandidaturasByEstudante", Candidatura.class).setParameter("username", cand.getEstudante().getUsername()).getResultList();
for (Candidatura c : candEstud) {
c.setEstadoCandidatura(Estado.REJEITADA);
em.merge(c);
}
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.