Advertisement
knoker

Untitled

Jan 26th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package pt.eoliveira.appmanager.dao;
  6.  
  7. import java.io.Serializable;
  8. import java.security.NoSuchAlgorithmException;
  9. import java.util.Collection;
  10. import javax.persistence.Basic;
  11. import javax.persistence.CascadeType;
  12. import javax.persistence.Column;
  13. import javax.persistence.Entity;
  14. import javax.persistence.GeneratedValue;
  15. import javax.persistence.GenerationType;
  16. import javax.persistence.Id;
  17. import javax.persistence.JoinColumn;
  18. import javax.persistence.ManyToOne;
  19. import javax.persistence.NamedQueries;
  20. import javax.persistence.NamedQuery;
  21. import javax.persistence.OneToMany;
  22. import javax.persistence.OneToOne;
  23. import javax.persistence.Table;
  24. import javax.xml.bind.annotation.XmlRootElement;
  25. import javax.xml.bind.annotation.XmlTransient;
  26. import org.codehaus.jackson.annotate.JsonIgnore;
  27. import pt.eoliveira.appmanager.utils.MySqlTools;
  28.  
  29. @Entity
  30. @Table(name = "users")
  31. @XmlRootElement
  32. @NamedQueries({
  33. @NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
  34. @NamedQuery(name = "Users.findByUserId", query = "SELECT u FROM Users u WHERE u.userId = :userId"),
  35. @NamedQuery(name = "Users.findByUsername", query = "SELECT u FROM Users u WHERE u.username = :username"),
  36. @NamedQuery(name = "Users.findByPassword", query = "SELECT u FROM Users u WHERE u.password = :password"),
  37. @NamedQuery(name = "Users.findByEmail", query = "SELECT u FROM Users u WHERE u.email = :email"),
  38. @NamedQuery(name = "Users.findByPermissions", query = "SELECT u FROM Users u WHERE u.permissions = :permissions")
  39. })
  40.  
  41. public class Users implements Serializable {
  42.  
  43. private static final long serialVersionUID = 1L;
  44.  
  45. @Id
  46. @GeneratedValue(strategy = GenerationType.IDENTITY)
  47. @Basic(optional = false)
  48. @Column(name = "user_id")
  49. private Integer userId;
  50.  
  51. @Basic(optional = false)
  52. @Column(name = "username")
  53. private String username;
  54.  
  55. @Basic(optional = false)
  56. @Column(name = "password")
  57. @XmlTransient
  58. @JsonIgnore
  59. private String password;
  60.  
  61. @Basic(optional = false)
  62. @Column(name = "email")
  63. private String email;
  64.  
  65. @Basic(optional = false)
  66. @Column(name = "permissions")
  67. private int permissions;
  68.  
  69. @OneToOne(mappedBy = "owner")
  70. private Sessions sessions;
  71.  
  72. @JoinColumn(name = "country", referencedColumnName = "country_id")
  73. @ManyToOne(optional = false)
  74. private Country country;
  75.  
  76. @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
  77. private Collection<Apps> appsCollection;
  78.  
  79. public Users() {
  80. }
  81.  
  82. public Users(String username) {
  83. this.username = username;
  84. }
  85.  
  86. public Users(Integer userId) {
  87. this.userId = userId;
  88. }
  89.  
  90. public Users(Integer userId, String username, String password, String email, int permissions) {
  91. this.userId = userId;
  92. this.username = username;
  93. this.password = password;
  94. this.email = email;
  95. this.permissions = permissions;
  96. }
  97.  
  98. public Integer getUserId() {
  99. return userId;
  100. }
  101.  
  102. public void setUserId(Integer userId) {
  103. this.userId = userId;
  104. }
  105.  
  106. public String getUsername() {
  107. return username;
  108. }
  109.  
  110. public void setUsername(String username) {
  111. this.username = username;
  112. }
  113.  
  114. public String getPassword() {
  115. return password;
  116. }
  117.  
  118. public void setPassword(String password) {
  119. this.password = password;
  120. }
  121.  
  122. public String getEmail() {
  123. return email;
  124. }
  125.  
  126. public void setEmail(String email) {
  127. this.email = email;
  128. }
  129.  
  130. public int getPermissions() {
  131. return permissions;
  132. }
  133.  
  134. public void setPermissions(int permissions) {
  135. this.permissions = permissions;
  136. }
  137.  
  138. public Sessions getSessions() {
  139. return sessions;
  140. }
  141.  
  142. public void setSessions(Sessions sessions) {
  143. this.sessions = sessions;
  144. }
  145.  
  146. public Country getCountry() {
  147. return country;
  148. }
  149.  
  150. public void setCountry(Country country) {
  151. this.country = country;
  152. }
  153.  
  154. @XmlTransient
  155. public Collection<Apps> getAppsCollection() {
  156. return appsCollection;
  157. }
  158.  
  159. public void setAppsCollection(Collection<Apps> appsCollection) {
  160. this.appsCollection = appsCollection;
  161. }
  162.  
  163. public boolean isValid(String password) {
  164. try {
  165. return this.password.equals(MySqlTools.MD5Encode(password));
  166. } catch (NoSuchAlgorithmException ex) {
  167. ex.printStackTrace();
  168. }
  169. return false;
  170. }
  171.  
  172. @Override
  173. public int hashCode() {
  174. int hash = 0;
  175. hash += (userId != null ? userId.hashCode() : 0);
  176. return hash;
  177. }
  178.  
  179. @Override
  180. public boolean equals(Object object) {
  181. // TODO: Warning - this method won't work in the case the id fields are not set
  182. if (!(object instanceof Users)) {
  183. return false;
  184. }
  185. Users other = (Users) object;
  186. if ((this.userId == null && other.userId != null) || (this.userId != null && !this.userId.equals(other.userId))) {
  187. return false;
  188. }
  189. return true;
  190. }
  191.  
  192. @Override
  193. public String toString() {
  194. return "Users{" + "userId=" + userId + ", username=" + username + ", password=" + password + ", email=" + email + ", permissions=" + permissions + ", sessions=" + sessions + ", country=" + country + ", appsCollection=" + appsCollection + '}';
  195. }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement