/WineSOA-war/src/java/adminBeans/UserManagement.java

https://bitbucket.org/MrJames/wineenterprise · Java · 139 lines · 102 code · 25 blank · 12 comment · 21 complexity · 6b6bd62852897906ade86f484e376b8d MD5 · raw file

  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package adminBeans;
  6. import com.google.gson.Gson;
  7. import commonBeans.UserSessionBean;
  8. import helper.UserState;
  9. import java.util.ResourceBundle;
  10. import javax.annotation.PostConstruct;
  11. import javax.faces.application.FacesMessage;
  12. import javax.faces.bean.ManagedBean;
  13. import javax.faces.bean.ViewScoped;
  14. import javax.faces.context.FacesContext;
  15. import javax.inject.Inject;
  16. import javax.xml.ws.WebServiceRef;
  17. import utilities.ServiceConstants;
  18. import webServices.AccountServices.AccountServices;
  19. import webServices.AccountServices.AccountServices_Service;
  20. /**
  21. *
  22. * @author JamesTran
  23. */
  24. @ManagedBean(name = "userManager")
  25. @ViewScoped
  26. public class UserManagement {
  27. @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/ec2-46-137-247-126.ap-southeast-1.compute.amazonaws.com/AccountServices/AccountServices.wsdl")
  28. private AccountServices_Service service;
  29. private AccountServices port;
  30. private ResourceBundle rb;
  31. @Inject
  32. private UserSessionBean userSession;
  33. private String adminPassword;
  34. private String userEmail;
  35. private UserState user;
  36. /**
  37. * Creates a new instance of UserManagement
  38. */
  39. public UserManagement() {}
  40. @PostConstruct
  41. public void prepareService() {
  42. this.rb = ResourceBundle.getBundle("utilities.constants");
  43. this.port = service.getAccountServicesPort();
  44. }
  45. public void getUserInfo() {
  46. this.user = null;
  47. String jsonizedObject = port.getUserInfo(userEmail);
  48. if (jsonizedObject == null) FacesContext.getCurrentInstance().addMessage(null , new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed", "Oops! No user has registered with this email."));
  49. else this.user = new Gson().fromJson(jsonizedObject, UserState.class);
  50. }
  51. public void toggleUserBlockStatus() {
  52. int result = port.toggleUserBlockStatus(userSession.getUs().getEmail(), adminPassword, user.getEmail());
  53. if (result == ServiceConstants.STATUS_WRONG_PASSWORD) FacesContext.getCurrentInstance().addMessage(null , new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed", "Oops! You have entered wrong password."));
  54. else if (result == ServiceConstants.STATUS_NOT_FOUND) FacesContext.getCurrentInstance().addMessage(null , new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed", "Oops! No user has registered with this email."));
  55. else if (result == ServiceConstants.STATUS_SUCCESSFUL) {
  56. Boolean newBlockStatus = !user.isBlocked();
  57. user.setBlocked(newBlockStatus);
  58. if (newBlockStatus) FacesContext.getCurrentInstance().addMessage(null , new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "You have successfully blocked this account."));
  59. else FacesContext.getCurrentInstance().addMessage(null , new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "You have successfully unblocked this account."));
  60. }
  61. }
  62. public void deleteUser() {
  63. int result = port.deleteUser(userSession.getUs().getEmail(), adminPassword, user.getEmail());
  64. if (result == ServiceConstants.STATUS_WRONG_PASSWORD) FacesContext.getCurrentInstance().addMessage(null , new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed", "Oops! You have entered wrong password."));
  65. else if (result == ServiceConstants.STATUS_NOT_FOUND) FacesContext.getCurrentInstance().addMessage(null , new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed", "Oops! No user has registered with this email."));
  66. else if (result == ServiceConstants.STATUS_SUCCESSFUL) {
  67. FacesContext.getCurrentInstance().addMessage(null , new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "You have successfully deleted this account."));
  68. this.user = null;
  69. }
  70. }
  71. // Getters and Setters
  72. public AccountServices_Service getService() {
  73. return service;
  74. }
  75. public void setService(AccountServices_Service service) {
  76. this.service = service;
  77. }
  78. public AccountServices getPort() {
  79. return port;
  80. }
  81. public void setPort(AccountServices port) {
  82. this.port = port;
  83. }
  84. public UserSessionBean getUserSession() {
  85. return userSession;
  86. }
  87. public void setUserSession(UserSessionBean userSession) {
  88. this.userSession = userSession;
  89. }
  90. public String getAdminPassword() {
  91. return adminPassword;
  92. }
  93. public void setAdminPassword(String adminPassword) {
  94. this.adminPassword = adminPassword;
  95. }
  96. public String getUserEmail() {
  97. return userEmail;
  98. }
  99. public void setUserEmail(String userEmail) {
  100. this.userEmail = userEmail;
  101. }
  102. public UserState getUser() {
  103. return user;
  104. }
  105. public void setUser(UserState user) {
  106. this.user = user;
  107. }
  108. public ResourceBundle getRb() {
  109. return rb;
  110. }
  111. public void setRb(ResourceBundle rb) {
  112. this.rb = rb;
  113. }
  114. }