|
| 1 | +package org.baeldung.persistence.model; |
| 2 | + |
| 3 | +import java.sql.Timestamp; |
| 4 | +import java.util.Calendar; |
| 5 | +import java.util.Date; |
| 6 | + |
| 7 | +import javax.persistence.Entity; |
| 8 | +import javax.persistence.FetchType; |
| 9 | +import javax.persistence.GeneratedValue; |
| 10 | +import javax.persistence.GenerationType; |
| 11 | +import javax.persistence.Id; |
| 12 | +import javax.persistence.JoinColumn; |
| 13 | +import javax.persistence.OneToOne; |
| 14 | + |
| 15 | +@Entity |
| 16 | +public class PasswordResetToken { |
| 17 | + |
| 18 | + private static final int EXPIRATION = 60 * 24; |
| 19 | + |
| 20 | + @Id |
| 21 | + @GeneratedValue(strategy = GenerationType.AUTO) |
| 22 | + private Long id; |
| 23 | + |
| 24 | + private String token; |
| 25 | + |
| 26 | + @OneToOne(targetEntity = User.class, fetch = FetchType.EAGER) |
| 27 | + @JoinColumn(nullable = false, name = "user_id") |
| 28 | + private User user; |
| 29 | + |
| 30 | + private Date expiryDate; |
| 31 | + |
| 32 | + public PasswordResetToken() { |
| 33 | + super(); |
| 34 | + } |
| 35 | + |
| 36 | + public PasswordResetToken(String token) { |
| 37 | + super(); |
| 38 | + |
| 39 | + this.token = token; |
| 40 | + this.expiryDate = calculateExpiryDate(EXPIRATION); |
| 41 | + } |
| 42 | + |
| 43 | + public PasswordResetToken(String token, User user) { |
| 44 | + super(); |
| 45 | + |
| 46 | + this.token = token; |
| 47 | + this.user = user; |
| 48 | + this.expiryDate = calculateExpiryDate(EXPIRATION); |
| 49 | + } |
| 50 | + |
| 51 | + public String getToken() { |
| 52 | + return token; |
| 53 | + } |
| 54 | + |
| 55 | + public void setToken(String token) { |
| 56 | + this.token = token; |
| 57 | + } |
| 58 | + |
| 59 | + public User getUser() { |
| 60 | + return user; |
| 61 | + } |
| 62 | + |
| 63 | + public void setUser(User user) { |
| 64 | + this.user = user; |
| 65 | + } |
| 66 | + |
| 67 | + public Date getExpiryDate() { |
| 68 | + return expiryDate; |
| 69 | + } |
| 70 | + |
| 71 | + public void setExpiryDate(Date expiryDate) { |
| 72 | + this.expiryDate = expiryDate; |
| 73 | + } |
| 74 | + |
| 75 | + private Date calculateExpiryDate(int expiryTimeInMinutes) { |
| 76 | + Calendar cal = Calendar.getInstance(); |
| 77 | + cal.setTime(new Timestamp(cal.getTime().getTime())); |
| 78 | + cal.add(Calendar.MINUTE, expiryTimeInMinutes); |
| 79 | + return new Date(cal.getTime().getTime()); |
| 80 | + } |
| 81 | + |
| 82 | + public void updateToken(String token) { |
| 83 | + this.token = token; |
| 84 | + this.expiryDate = calculateExpiryDate(EXPIRATION); |
| 85 | + } |
| 86 | + |
| 87 | + // |
| 88 | + |
| 89 | + @Override |
| 90 | + public int hashCode() { |
| 91 | + final int prime = 31; |
| 92 | + int result = 1; |
| 93 | + result = prime * result + ((expiryDate == null) ? 0 : expiryDate.hashCode()); |
| 94 | + result = prime * result + ((token == null) ? 0 : token.hashCode()); |
| 95 | + result = prime * result + ((user == null) ? 0 : user.hashCode()); |
| 96 | + return result; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public boolean equals(Object obj) { |
| 101 | + if (this == obj) |
| 102 | + return true; |
| 103 | + if (obj == null) |
| 104 | + return false; |
| 105 | + if (getClass() != obj.getClass()) |
| 106 | + return false; |
| 107 | + PasswordResetToken other = (PasswordResetToken) obj; |
| 108 | + if (expiryDate == null) { |
| 109 | + if (other.expiryDate != null) |
| 110 | + return false; |
| 111 | + } else if (!expiryDate.equals(other.expiryDate)) |
| 112 | + return false; |
| 113 | + if (token == null) { |
| 114 | + if (other.token != null) |
| 115 | + return false; |
| 116 | + } else if (!token.equals(other.token)) |
| 117 | + return false; |
| 118 | + if (user == null) { |
| 119 | + if (other.user != null) |
| 120 | + return false; |
| 121 | + } else if (!user.equals(other.user)) |
| 122 | + return false; |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public String toString() { |
| 128 | + final StringBuilder builder = new StringBuilder(); |
| 129 | + builder.append("Token [String=").append(token).append("]").append("[Expires").append(expiryDate).append("]"); |
| 130 | + return builder.toString(); |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments