/fap/app/models/Agente.java

https://github.com/FAP-Team/Fap-Module · Java · 178 lines · 114 code · 35 blank · 29 comment · 17 complexity · 392cf020bff1ba757e54937bb53f67e5 MD5 · raw file

  1. package models;
  2. import java.util.*;
  3. import javax.persistence.*;
  4. import play.Logger;
  5. import play.db.jpa.JPA;
  6. import play.db.jpa.Model;
  7. import play.data.validation.*;
  8. import org.joda.time.DateTime;
  9. import models.*;
  10. import messages.Messages;
  11. import validation.*;
  12. import audit.Auditable;
  13. import java.text.ParseException;
  14. import java.text.SimpleDateFormat;
  15. import validation.PasswordCheck;
  16. import validation.PasswordCheck;
  17. // === IMPORT REGION START ===
  18. import play.mvc.Scope.Session;
  19. import enumerado.fap.gen.AccesoAgenteEnum;
  20. // === IMPORT REGION END ===
  21. @Auditable
  22. @Entity
  23. @Inheritance(strategy = InheritanceType.JOINED)
  24. public class Agente extends FapModel {
  25. // Código de los atributos
  26. public String username;
  27. public String passwordAntiguo;
  28. @CheckWith(validation.PasswordCheck.class)
  29. public String password;
  30. @CheckWith(validation.PasswordCheck.class)
  31. @Transient
  32. public String newpassword;
  33. @Email
  34. public String email;
  35. public String name;
  36. @ElementCollection
  37. @ValueFromTable("roles")
  38. @FapEnum("enumerado.fap.gen.RolesEnum")
  39. public Set<String> roles;
  40. @ValueFromTable("roles")
  41. @FapEnum("enumerado.fap.gen.RolesEnum")
  42. public String rolActivo;
  43. @ValueFromTable("accesoAgente")
  44. @FapEnum("enumerado.fap.gen.AccesoAgenteEnum")
  45. public String acceso;
  46. public Boolean funcionario;
  47. @Transient
  48. public String verificacion;
  49. @Column(length = 500)
  50. public String cargo;
  51. public String usuarioldap;
  52. public String sessionHash;
  53. public Agente() {
  54. init();
  55. }
  56. public void init() {
  57. if (roles == null)
  58. roles = new HashSet<String>();
  59. postInit();
  60. }
  61. // === MANUAL REGION START ===
  62. public String getRolActivo() {
  63. String rol = null;
  64. // Issue #98
  65. // Mantiene el rol activo en sesión
  66. if (Session.current().contains("role")) {
  67. rol = Session.current().get("role");
  68. } else {
  69. //El rol activo no está en sesión, usa el de db
  70. cambiarRolActivo(this.rolActivo);
  71. rol = this.rolActivo;
  72. }
  73. return rol;
  74. }
  75. public void cambiarRolActivo(String rolActivo) {
  76. if (rolActivo == null) {
  77. //El usuario no tiene ningún rolActivo guardado en db
  78. rolActivo = getFirstRole();
  79. } else if (!roles.contains(rolActivo)) {
  80. //El usuario está intentando cambiar a un rol que no tiene
  81. //Se le asigna el primer de la lista.
  82. //Se debería lanzar un error?
  83. rolActivo = getFirstRole();
  84. }
  85. //Guarda el último rolActivo en base de datos
  86. this.rolActivo = rolActivo;
  87. this.save();
  88. //Mantiene el rolActivo en sesión
  89. Session.current().put("role", rolActivo);
  90. }
  91. //Devuelve el primer rol de la lista
  92. private String getFirstRole() {
  93. String rol;
  94. if (roles.size() > 0) {
  95. rol = roles.iterator().next();
  96. } else {
  97. //TODO mejorar esta excepción
  98. throw new RuntimeException("Intentando establecer un rolActivo que no tiene el usuario");
  99. }
  100. return rol;
  101. }
  102. @Override
  103. public String toString() {
  104. return "Agente [username=" + username + ", email=" + email + "]";
  105. }
  106. public boolean getFuncionario() {
  107. if (funcionario != null)
  108. return funcionario;
  109. return false;
  110. }
  111. /**
  112. * Devuelve la lista de roles ordenados alfabéticamente
  113. * @return
  114. */
  115. public List<String> getSortRoles() {
  116. List<String> list = new ArrayList<String>(this.roles);
  117. Collections.sort(list);
  118. return list;
  119. }
  120. /**
  121. * Devuelve true si el agente accedió por certificado
  122. * @return
  123. */
  124. public Boolean accedidoPorCertificado() {
  125. return this.acceso != null && this.acceso.equals(AccesoAgenteEnum.certificado.name());
  126. }
  127. /**
  128. * Devuelve un agente buscando por su nombre (el primero que encuentra)
  129. * @param usernameSearch
  130. * @return
  131. */
  132. public static Agente getAgenteByUsername(String usernameSearch) {
  133. List<Agente> listaAgentes = Agente.find("select agente from Agente agente where agente.username = ?", usernameSearch).fetch();
  134. if (listaAgentes.isEmpty()) {
  135. play.Logger.warn("No se han encontrado agentes con username: " + usernameSearch);
  136. return null;
  137. } else if (listaAgentes.size() > 1) {
  138. play.Logger.warn("Existen " + listaAgentes.size() + " agentes con username: " + usernameSearch);
  139. }
  140. return listaAgentes.get(0);
  141. }
  142. // === MANUAL REGION END ===
  143. }