Advertisement
Guest User

Untitled

a guest
Jun 20th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.36 KB | None | 0 0
  1. package pl.com.sokaris.bok.domain.model.user;
  2.  
  3. import static javax.persistence.GenerationType.IDENTITY;
  4.  
  5. import java.io.Serializable;
  6. import java.util.Collection;
  7. import java.util.HashSet;
  8.  
  9. import javax.persistence.CascadeType;
  10. import javax.persistence.Column;
  11. import javax.persistence.Entity;
  12. import javax.persistence.FetchType;
  13. import javax.persistence.GeneratedValue;
  14. import javax.persistence.Id;
  15. import javax.persistence.OneToMany;
  16. import javax.persistence.Table;
  17.  
  18. import org.springframework.security.core.GrantedAuthority;
  19. import org.springframework.security.core.userdetails.UserDetails;
  20.  
  21. import pl.com.sokaris.bok.domain.DomainEntity;
  22.  
  23. @Entity
  24. @Table(name = "user")
  25. public class User implements DomainEntity<User>, UserDetails, Serializable {
  26.  
  27.     private static final long serialVersionUID = 8489541351526006318L;
  28.  
  29.     @Id
  30.     @GeneratedValue(strategy = IDENTITY)
  31.     @Column(name = "user_id", unique = true, nullable = false)
  32.     private Long id;
  33.  
  34.     @Column(name = "username", length = 30)
  35.     private String username;
  36.  
  37.     @Column(name = "password", length = 30)
  38.     private String password;
  39.  
  40.     @Column(name = "account_not_expired", nullable = false)
  41.     private boolean accountNotExpired;
  42.  
  43.     @Column(name = "account_not_locked", nullable = false)
  44.     private boolean accountNotLocked;
  45.  
  46.     @Column(name = "credentials_not_expiered", nullable = false)
  47.     private boolean credentialsNotExpiered;
  48.  
  49.     @Column(name = "account_is_enabled", nullable = false)
  50.     private boolean accountIsEnabled;
  51.  
  52.     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "user_role")
  53.     private Collection<GrantedAuthority> userRoles;
  54.  
  55.     public User(Long id, String username, String password, boolean accountNotExpired, boolean accountNotLocked, boolean credentialsNotExpiered,
  56.         boolean accountIsEnabled, Collection<UserRole> userRoles) {
  57.     super();
  58.     this.id = id;
  59.     this.username = username;
  60.     this.password = password;
  61.     this.accountNotExpired = accountNotExpired;
  62.     this.accountNotLocked = accountNotLocked;
  63.     this.credentialsNotExpiered = credentialsNotExpiered;
  64.     this.accountIsEnabled = accountIsEnabled;
  65.     this.userRoles = new HashSet<GrantedAuthority>();
  66.  
  67.     for (UserRole userRole : userRoles) {
  68.         this.userRoles.add(userRole);
  69.     }
  70.     }
  71.  
  72.     public Long getId() {
  73.     return id;
  74.     }
  75.  
  76.     public void setId(Long id) {
  77.     this.id = id;
  78.     }
  79.  
  80.     @Override
  81.     public Collection<GrantedAuthority> getAuthorities() {
  82.     return userRoles;
  83.     }
  84.  
  85.     @Override
  86.     public String getPassword() {
  87.     return password;
  88.     }
  89.  
  90.     @Override
  91.     public String getUsername() {
  92.     return username;
  93.     }
  94.  
  95.     @Override
  96.     public boolean isAccountNonExpired() {
  97.     return accountNotExpired;
  98.     }
  99.  
  100.     @Override
  101.     public boolean isAccountNonLocked() {
  102.     return accountNotLocked;
  103.     }
  104.  
  105.     @Override
  106.     public boolean isCredentialsNonExpired() {
  107.     return credentialsNotExpiered;
  108.     }
  109.  
  110.     @Override
  111.     public boolean isEnabled() {
  112.     return accountIsEnabled;
  113.     }
  114.  
  115.     @Override
  116.     public boolean sameAs(User otherEntity) {
  117.     return (otherEntity.getUsername().compareTo(getUsername()) == 0) ? true : false;
  118.     }
  119.  
  120.     @Override
  121.     public boolean equals(Object obj) {
  122.     if (obj == null || !(obj instanceof User)) {
  123.         return false;
  124.     }
  125.  
  126.     return ((User) obj).getUsername().equals(this.getUsername());
  127.     }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement