diff --git a/src/main/java/com/gymtracker/model/Application.java b/src/main/java/com/gymtracker/model/Application.java index 168771e8e87b5bea583a6ebd55e4371497a9c15b..492b6c70e60ff40d3ae69897352dac317e896295 100644 --- a/src/main/java/com/gymtracker/model/Application.java +++ b/src/main/java/com/gymtracker/model/Application.java @@ -60,6 +60,18 @@ public class Application implements Serializable { return application_description; } + public String toXML() { + return "<application>" + + "<username>" + username + "</username>" + + "<gym_id>" + gym_id + "</gym_id>" + + "<email>" + email + "</email>" + + "<acceptance>" + acceptance + "</acceptance>" + + "<trainer_name>" + trainer_name + "</trainer_name>" + + "<trainer_description>" + trainer_description + "</trainer_description>" + + "<application_description>" + application_description + "</application_description>" + + "</application>"; + } + // Constructors public Application(String username, int gym_id, String email, int acceptance, String trainer_name) { this.username = username; @@ -69,6 +81,11 @@ public class Application implements Serializable { this.trainer_name = trainer_name; } + // Default constructor + public Application() { + } + + //Setters public void setUsername(String username) { this.username = username; diff --git a/src/main/java/com/gymtracker/service/ApplicationService.java b/src/main/java/com/gymtracker/service/ApplicationService.java index 050b527e969f07c7a10076efcd806795af7808d2..10d415e2864b077e5dc9c18dc597bcfc341be166 100644 --- a/src/main/java/com/gymtracker/service/ApplicationService.java +++ b/src/main/java/com/gymtracker/service/ApplicationService.java @@ -1,6 +1,7 @@ package com.gymtracker.service; import com.gymtracker.model.Application; +import com.gymtracker.service.Email; import com.gymtracker.util.HibernateUtil; import org.hibernate.Session; import org.hibernate.SessionFactory; @@ -10,17 +11,49 @@ import javax.jws.HandlerChain; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; -import javax.xml.soap.SOAPMessage; import javax.xml.ws.WebServiceContext; -import javax.xml.ws.handler.MessageContext; -import javax.xml.ws.handler.soap.SOAPMessageContext; -import java.util.Map; +import java.util.List; +// import javax.xml.soap.SOAPMessage; +// import javax.xml.ws.handler.MessageContext; +// import javax.xml.ws.handler.soap.SOAPMessageContext; +// import java.util.Map; @WebService @HandlerChain(file = "log_and_auth.xml") public class ApplicationService { @Resource private WebServiceContext wsContext; + + private static Session getSession() { + SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); + return sessionFactory.getCurrentSession(); + } + + private static void beginTransaction(Session session) { + if (session != null && !session.getTransaction().isActive()) { + session.beginTransaction(); + } + } + + private static void commitTransaction(Session session) { + if (session != null && session.getTransaction().isActive()) { + session.getTransaction().commit(); + } + } + + private static void rollbackTransaction(Session session) { + if (session != null && session.getTransaction().isActive()) { + session.getTransaction().rollback(); + } + } + + private static Application getApplication(Session session, String username, int gym_id) { + return (Application) session.createQuery("FROM Application WHERE username = :username AND gym_id = :gym_id") + .setParameter("username", username) + .setParameter("gym_id", gym_id) + .uniqueResult(); + } + @WebMethod public String apply( @WebParam(name = "username") String username, @@ -46,19 +79,145 @@ public class ApplicationService { application.setApplicationDescription(application_description); } - SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); - Session session = sessionFactory.getCurrentSession(); + Session session = ApplicationService.getSession(); - session.beginTransaction(); + ApplicationService.beginTransaction(session); session.save(application); - session.getTransaction().commit(); + ApplicationService.commitTransaction(session); + + return "Application successful with username: " + username + " and gym_id: " + gym_id; + } catch (Exception e) { + ApplicationService.rollbackTransaction(getSession()); + e.printStackTrace(); + return "Application failed with error: " + e.getMessage(); + } + } - return "Application successful"; + @WebMethod + public String accept( + @WebParam(name = "username") String username, + @WebParam(name = "gym_id") int gym_id + ) { + try { + // set the application acceptance to 1 and make the other applications for the same gym rejected with 2 + Session session = ApplicationService.getSession(); + ApplicationService.beginTransaction(session); + + // Update the acceptance status of the application based on the gym_id (1 for accepted, 2 for rejected) + String query = "UPDATE Application SET acceptance = (CASE WHEN gym_id = :gym_id THEN 1 ELSE 2 END) " + + "WHERE username = :username"; + session.createQuery(query) + .setParameter("gym_id", gym_id) + .setParameter("username", username) + .executeUpdate(); + + // Send email to the trainer + String email = "default1.wbd@gmail.com"; + String trainer_name = ""; + Application application = ApplicationService.getApplication(session, username, gym_id); + if (application != null) { + email = application.getEmail(); + trainer_name = application.getTrainerName(); + } + + Email.sendMail(email, true, "GYM " + gym_id, "Trainer " + trainer_name); + ApplicationService.commitTransaction(session); + return "Application accepted with username: " + username + " and gym_id: " + gym_id; } catch (Exception e) { + ApplicationService.rollbackTransaction(getSession()); e.printStackTrace(); - return "Application failed"; + return "Application failed with error: " + e.getMessage(); } } + @WebMethod + public String reject( + @WebParam(name = "username") String username, + @WebParam(name = "gym_id") int gym_id + ) { + try { + // set the application acceptance to 2 + Session session = ApplicationService.getSession(); + ApplicationService.beginTransaction(session); + + // Update the acceptance status of the application based on the gym_id (1 for accepted, 2 for rejected) + String query = "UPDATE Application SET acceptance = 2 " + + "WHERE username = :username AND gym_id = :gym_id"; + session.createQuery(query) + .setParameter("username", username) + .setParameter("gym_id", gym_id) + .executeUpdate(); + + // Send email to the trainer + String email = ""; + String trainer_name = ""; + Application application = ApplicationService.getApplication(session, username, gym_id); + if (application != null) { + email = application.getEmail(); + trainer_name = application.getTrainerName(); + } + + Email.sendMail(email, false, "GYM " + gym_id, "Trainer " + trainer_name); + ApplicationService.commitTransaction(session); + return "Application rejected with username: " + username + " and gym_id: " + gym_id; + } catch (Exception e) { + ApplicationService.rollbackTransaction(getSession()); + e.printStackTrace(); + return "Application failed with error: " + e.getMessage(); + } + } + + @WebMethod + public String resign( + @WebParam(name = "username") String username + ){ + try { + Session session = ApplicationService.getSession(); + ApplicationService.beginTransaction(session); + + // Delete the application based on the username + String query = "DELETE FROM Application WHERE username = :username"; + session.createQuery(query) + .setParameter("username", username) + .executeUpdate(); + + ApplicationService.commitTransaction(session); + return "Trainer resigned with username: " + username; + } catch (Exception e) { + ApplicationService.rollbackTransaction(getSession()); + e.printStackTrace(); + return "Resign failed with error: " + e.getMessage(); + } + } + + public String getAllStatus( + @WebParam(name = "username") String username + ){ + try { + Session session = ApplicationService.getSession(); + ApplicationService.beginTransaction(session); + + // Get all the gym_id and acceptance status of the aplication + String query = "SELECT g.gym_id, g.acceptance FROM Application g WHERE g.username = :username"; + List<Object[]> resultList = session.createQuery(query, Object[].class) + .setParameter("username", username) + .getResultList(); + + ApplicationService.commitTransaction(session); + + StringBuilder result = new StringBuilder(); + for (Object[] status : resultList) { + int gymId = (int) status[0]; + int acceptance = (int) status[1]; + result.append("(gym_id: ").append(gymId).append(", acceptance: )").append(acceptance).append("\n"); + } + + return result.toString(); + } catch (Exception e) { + ApplicationService.rollbackTransaction(getSession()); + e.printStackTrace(); + return "Failed to retrieve status with error: " + e.getMessage(); + } + } -} +} \ No newline at end of file