diff --git a/src/main/java/com/gymtracker/service/ApplicationService.java b/src/main/java/com/gymtracker/service/ApplicationService.java index 6a5224f5ce7c43a350ba60a28ae645979151d2a3..81d120142cff7695727b72492e0900711f595e55 100644 --- a/src/main/java/com/gymtracker/service/ApplicationService.java +++ b/src/main/java/com/gymtracker/service/ApplicationService.java @@ -12,12 +12,17 @@ import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import javax.xml.ws.WebServiceContext; + import java.util.List; // import javax.mail.MessageContext; +// import javax.xml.soap.SOAPException; +// import javax.xml.soap.SOAPFactory; +// import javax.xml.soap.SOAPFault; +// import javax.xml.ws.soap.SOAPFaultException; +// import javax.mail.MessageContext; // import javax.xml.bind.JAXB; // import javax.xml.ws.handler.soap.SOAPMessageContext; // import java.io.StringWriter; - // import javax.xml.soap.SOAPMessage; // import javax.xml.ws.handler.MessageContext; // import javax.xml.ws.handler.soap.SOAPMessageContext; @@ -59,15 +64,26 @@ public class ApplicationService { .uniqueResult(); } + // private SOAPFaultException createSoapFaultException(String errorMessage) { + // try { + // SOAPFactory soapFactory = SOAPFactory.newInstance(); + // SOAPFault soapFault = soapFactory.createFault(); + // soapFault.setFaultString(errorMessage); + // return new SOAPFaultException(soapFault); + // } catch (SOAPException e) { + // throw new RuntimeException("Error creating SOAPFaultException", e); + // } + // } + @WebMethod - public String apply( + public String apply ( @WebParam(name = "username") String username, @WebParam(name = "gym_id") int gym_id, @WebParam(name = "email") String email, @WebParam(name = "trainer_name") String trainer_name, @WebParam(name = "trainer_description") String trainer_description, @WebParam(name = "application_description") String application_description - ) { + ) throws Exception { try { Application application = new Application(username, gym_id, email, 0, trainer_name); @@ -88,7 +104,9 @@ public class ApplicationService { } catch (Exception e) { ApplicationService.rollbackTransaction(getSession()); e.printStackTrace(); - return "Application failed with error: " + e.getMessage(); + // throw createSoapFaultException("Failed: Application failed with error: " + e.getMessage()); + // return "Failed: Application failed with error: " + e.getMessage(); + throw new Exception("Failed: Application failed with error: " + e.getMessage()); } } @@ -96,7 +114,7 @@ public class ApplicationService { public String accept( @WebParam(name = "username") String username, @WebParam(name = "gym_id") int gym_id - ) { + ) throws Exception { try { // set the application acceptance to 1 and make the other applications for the same gym rejected with 2 Session session = ApplicationService.getSession(); @@ -121,11 +139,12 @@ public class ApplicationService { Email.sendMail(email, true, "GYM " + gym_id, "Trainer " + trainer_name); ApplicationService.commitTransaction(session); - return "Application accepted with username: " + username + " and gym_id: " + gym_id; + return "Success: Application accepted with username: " + username + " and gym_id: " + gym_id; } catch (Exception e) { ApplicationService.rollbackTransaction(getSession()); e.printStackTrace(); - return "Application failed with error: " + e.getMessage(); + // return "Failed: Application failed with error: " + e.getMessage(); + throw new Exception("Failed: Application failed with error: " + e.getMessage()); } } @@ -133,7 +152,7 @@ public class ApplicationService { public String reject( @WebParam(name = "username") String username, @WebParam(name = "gym_id") int gym_id - ) { + ) throws Exception { try { // set the application acceptance to 2 Session session = ApplicationService.getSession(); @@ -158,11 +177,12 @@ public class ApplicationService { Email.sendMail(email, false, "GYM " + gym_id, "Trainer " + trainer_name); ApplicationService.commitTransaction(session); - return "Application rejected with username: " + username + " and gym_id: " + gym_id; + return "Success: 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(); + // return "Failed: Application failed with error: " + e.getMessage(); + throw new Exception("Failed: Application failed with error: " + e.getMessage()); } } @@ -181,18 +201,19 @@ public class ApplicationService { .executeUpdate(); ApplicationService.commitTransaction(session); - return "Trainer resigned with username: " + username; + return "Success: Trainer resigned with username: " + username; } catch (Exception e) { ApplicationService.rollbackTransaction(getSession()); e.printStackTrace(); - return "Resign failed with error: " + e.getMessage(); + // return "Failed: Resign failed with error: " + e.getMessage(); + return "Failed: Resign failed with error: " + e.getMessage(); } } @WebMethod public String getAllStatus( @WebParam(name = "username") String username - ){ + ) throws Exception { try { Session session = ApplicationService.getSession(); ApplicationService.beginTransaction(session); @@ -216,14 +237,15 @@ public class ApplicationService { } catch (Exception e) { ApplicationService.rollbackTransaction(getSession()); e.printStackTrace(); - return "Failed to retrieve status with error: " + e.getMessage(); + // return "Failed: Failed to retrieve status with error: " + e.getMessage(); + throw new Exception("Failed: Failed to retrieve status with error: " + e.getMessage()); } } @WebMethod public String getAllAplication( @WebParam(name = "gym_id") int gym_id - ){ + ) throws Exception { try { Session session = ApplicationService.getSession(); ApplicationService.beginTransaction(session); @@ -246,7 +268,8 @@ public class ApplicationService { } catch (Exception e) { ApplicationService.rollbackTransaction(getSession()); e.printStackTrace(); - return "Failed to retrieve all application with error: " + e.getMessage(); + // return "Failed: Failed to retrieve all application with error: " + e.getMessage(); + throw new Exception("Failed: Failed to retrieve all application with error: " + e.getMessage()); } } @@ -254,7 +277,7 @@ public class ApplicationService { public String getAplication( @WebParam(name = "username") String username, @WebParam(name = "gym_id") int gym_id - ){ + ) throws Exception { try { Session session = ApplicationService.getSession(); ApplicationService.beginTransaction(session); @@ -276,7 +299,8 @@ public class ApplicationService { } catch (Exception e) { ApplicationService.rollbackTransaction(getSession()); e.printStackTrace(); - return "Failed to retrieve application with error: " + e.getMessage(); + // return "Failed: Failed to retrieve application with error: " + e.getMessage(); + throw new Exception("Failed: Failed to retrieve application with error: " + e.getMessage()); } }