Skip to content

Instantly share code, notes, and snippets.

@jittagornp
Last active June 23, 2021 10:00
Show Gist options
  • Save jittagornp/d6f901ba9439356e0db3 to your computer and use it in GitHub Desktop.
Save jittagornp/d6f901ba9439356e0db3 to your computer and use it in GitHub Desktop.
User.java
package com.pamarin.persistence.entity;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.pamarin.persistence.converter.JsonDateTimeSerializer;
import com.pamarin.persistence.converter.JsonEnumSerializer;
import com.pamarin.persistence.generator.PrimaryKeyGenerator;
import com.pamarin.persistence.support.Link;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.Transient;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.jsondoc.core.annotation.ApiObject;
import org.jsondoc.core.annotation.ApiObjectField;
/**
* @author jittagornp
*/
@ApiObject(
name = "user",
group = "user",
description = "for keep user information"
)
@Entity
@Table(name = "pr_user", indexes = {
@Index(
name = "user_username_idx",
unique = true,
columnList = "username"
),
@Index(
name = "user_social_idx",
unique = true,
columnList = "social_id, social"
)
})
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
public class User implements Serializable {
public static final String USERNAME_REGEX = "[A-Za-z0-9\\.\\-\\_]+";
public static enum Gender {
UNKNOWN,
MALE,
FEMALE;
private String desc;
public String getDescription() {
return desc;
}
public void setDescription(String description) {
this.desc = description;
}
}
public static enum Social {
FACEBOOK,
GOOGLE_PLUS,
TWITTER
}
@Id
@Column(
name = "user_id",
length = 32,
nullable = false
)
@JsonSerialize
@JsonInclude(Include.NON_NULL)
@JsonProperty("user_id")
@NotNull(message = "{javax.validation.constraints.NotNull.id.message}")
@Length(
min = 32,
max = 32,
message = "{org.hibernate.validator.constraints.Length.id.message}"
)
@ApiObjectField(
name = "user_id",
description = "(unique) *** fixed length 32 character",
required = true,
order = 10
)
private String id;
//--------------------------------------------------------------------------
@Column(
name = "username",
unique = true,
nullable = false,
length = 255
)
@JsonSerialize
@JsonInclude(Include.NON_NULL)
@NotEmpty(message = "{javax.validation.constraints.NotEmpty.username.message}")
@Pattern(
regexp = USERNAME_REGEX,
message = "{javax.validation.constraints.Pattern.username.message}"
)
@Length(
max = 255,
message = "{org.hibernate.validator.constraints.Length.username.message}"
)
@ApiObjectField(
name = "username",
description = "(unique) should show on url (/+username), max length 255 character",
format = "must contains A-Z a-z 0-9 . - _ only",
required = true,
order = 20
)
private String username;
//--------------------------------------------------------------------------
@Column(
name = "first_name",
nullable = false,
columnDefinition = "VARCHAR(255) CHARACTER SET utf8mb4"
)
@JsonSerialize
@JsonInclude(Include.NON_NULL)
@JsonProperty("first_name")
@NotEmpty(message = "{javax.validation.constraints.NotEmpty.firstName.message}")
@Length(
max = 255,
message = "{org.hibernate.validator.constraints.Length.firstName.message}"
)
@ApiObjectField(
name = "first_name",
description = "max length 255 character",
required = true,
order = 30
)
private String firstName;
//--------------------------------------------------------------------------
@Column(
name = "middle_name",
columnDefinition = "VARCHAR(255) CHARACTER SET utf8mb4"
)
@JsonSerialize
@JsonInclude(Include.NON_NULL)
@JsonProperty("middle_name")
@Length(
max = 255,
message = "{org.hibernate.validator.constraints.Length.middleName.message}"
)
@ApiObjectField(
name = "middle_name",
description = "max length 255 character",
required = false,
order = 35
)
private String middleName;
//--------------------------------------------------------------------------
@Column(
name = "last_name",
nullable = false,
columnDefinition = "VARCHAR(255) CHARACTER SET utf8mb4"
)
@JsonSerialize
@JsonInclude(Include.NON_NULL)
@JsonProperty("last_name")
@NotEmpty(message = "{javax.validation.constraints.NotEmpty.lastName.message}")
@Length(
max = 255,
message = "{org.hibernate.validator.constraints.Length.lastName.message}"
)
@ApiObjectField(
name = "last_name",
description = "max length 255 character",
required = true,
order = 40
)
private String lastName;
//--------------------------------------------------------------------------
@Transient
@JsonSerialize
@JsonInclude(Include.NON_NULL)
@ApiObjectField(
name = "name",
description = "first_name + last_name",
required = false,
order = 50
)
private String name;
//--------------------------------------------------------------------------
@Column(
name = "email",
nullable = false,
length = 255
)
@JsonSerialize
@JsonInclude(Include.NON_NULL)
@NotEmpty(message = "{javax.validation.constraints.NotEmpty.email.message}")
@Length(
max = 255,
message = "{org.hibernate.validator.constraints.Length.email.message}"
)
@ApiObjectField(
name = "email",
description = "max length 255 character",
required = true,
order = 60
)
private String email;
//--------------------------------------------------------------------------
@Enumerated(EnumType.STRING)
@Column(
name = "gender",
nullable = false,
length = 255
)
@JsonSerialize(using = JsonEnumSerializer.class)
@JsonInclude(Include.NON_NULL)
@ApiObjectField(
name = "gender",
description = "gender or sex",
required = false,
allowedvalues = {
"UNKNOWN",
"MALE",
"FEMALE"
},
order = 70
)
private Gender gender;
//--------------------------------------------------------------------------
@Column(
name = "locale",
length = 255,
nullable = false
)
@JsonSerialize
@JsonInclude(Include.NON_NULL)
@Length(
max = 255,
message = "{org.hibernate.validator.constraints.Length.locale.message}"
)
@ApiObjectField(
name = "locale",
description = "locale for change language (such as th_TH), max length 255 character",
required = false,
order = 80
)
private String locale;
//--------------------------------------------------------------------------
@Column(
name = "timezone_id",
length = 255
)
@JsonProperty("timezone_id")
@JsonSerialize
@JsonInclude(Include.NON_NULL)
@Length(
max = 255,
message = "{org.hibernate.validator.constraints.Length.timezoneId.message}"
)
@ApiObjectField(
name = "timezone_id",
description = "such as asia/bangkok for get timezone, max length 255 character",
required = false,
order = 90
)
private String timezoneId;
//--------------------------------------------------------------------------
@Column(name = "timezone")
@JsonSerialize
@JsonInclude(Include.NON_NULL)
@ApiObjectField(
name = "timezone",
description = "timezone number, such as +7",
required = false,
order = 100
)
private Integer timezone;
//--------------------------------------------------------------------------
@Transient
@JsonProperty("picture")
@JsonInclude(JsonInclude.Include.NON_NULL)
@ApiObjectField(
name = "picture",
description = "picture profile of user",
required = false,
order = 110
)
private String picture;
//--------------------------------------------------------------------------
@Valid
@Transient
@JsonProperty("photo")
@JsonInclude(JsonInclude.Include.NON_NULL)
@NotNull(message = "{javax.validation.constraints.NotNull.photo.message}")
private Photo photo;
//--------------------------------------------------------------------------
@Valid
@JsonSerialize
@JsonInclude(Include.NON_EMPTY)
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "pr_user_authority",
joinColumns = {
@JoinColumn(
name = "user_id",
referencedColumnName = "user_id"
)
},
inverseJoinColumns = {
@JoinColumn(
name = "auth_id",
referencedColumnName = "auth_id"
)
}
)
@ApiObjectField(
name = "authorities",
description = "role for access resources",
required = false,
order = 120
)
private Set<Authority> authorities;
//--------------------------------------------------------------------------
@Enumerated(EnumType.STRING)
@Column(
name = "social",
nullable = false,
length = 255
)
@JsonSerialize(using = JsonEnumSerializer.class)
@JsonInclude(Include.NON_NULL)
@NotNull(message = "{javax.validation.constraints.NotNull.social.message}")
@ApiObjectField(
name = "social",
description = "social network type authen",
required = true,
allowedvalues = {
"FACEBOOK",
"GOOGLE_PLUS",
"TWITTER"
},
order = 130
)
private Social social;
//--------------------------------------------------------------------------
@Column(
name = "social_id",
nullable = false,
length = 255
)
@JsonSerialize
@JsonInclude(Include.NON_NULL)
@JsonProperty("social_id")
@NotNull(message = "{javax.validation.constraints.NotNull.socialId.message}")
@Length(
max = 255,
message = "{org.hibernate.validator.constraints.Length.socialId.message}"
)
@ApiObjectField(
name = "social_id",
description = "get from social network authen, max length 255 character",
required = true,
order = 140
)
private String socialId;
//--------------------------------------------------------------------------
@Column(
name = "create_date",
nullable = false
)
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
@JsonProperty("create_date")
@JsonSerialize(using = JsonDateTimeSerializer.class)
@JsonInclude(Include.NON_NULL)
@NotNull(message = "{javax.validation.constraints.NotNull.createDate.message}")
@ApiObjectField(
name = "create_date",
description = "register date time",
format = "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
required = true,
order = 150
)
private Date createDate;
//--------------------------------------------------------------------------
@Column(
name = "update_date",
nullable = false
)
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
@JsonProperty("update_date")
@JsonSerialize(using = JsonDateTimeSerializer.class)
@JsonInclude(Include.NON_NULL)
@NotNull(message = "{javax.validation.constraints.NotNull.updateDate.message}")
@ApiObjectField(
name = "update_date",
description = "update user information date time",
format = "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
required = true,
order = 160
)
private Date updateDate;
//--------------------------------------------------------------------------
@Lob
@Column(
name = "about_me",
columnDefinition = "VARCHAR(500) CHARACTER SET utf8mb4"
)
@JsonSerialize
@JsonInclude(Include.NON_NULL)
@JsonProperty("about_me")
@Length(
max = 500,
message = "{org.hibernate.validator.constraints.Length.aboutMe.message}"
)
@ApiObjectField(
name = "about_me",
description = "max length 500 character",
required = false,
order = 170
)
private String aboutMe;
//--------------------------------------------------------------------------
@Transient
@ApiObjectField(
name = "link",
description = "access link of user",
required = false,
order = 180
)
private Link link;
//--------------------------------------------------------------------------
@Transient
@JsonInclude(Include.NON_NULL)
@JsonProperty("is_owner")
@ApiObjectField(
name = "is_owner",
description = "user login is own object",
required = false,
order = 190
)
private Boolean isOwner;
//--------------------------------------------------------------------------
public User() {
}
public User(String id) {
this.id = id;
this.username = id;
}
public User randomKey() {
this.id = PrimaryKeyGenerator.generate();
return this;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getName() {
if (firstName == null && middleName == null && lastName == null) {
return null;
}
String f = firstName == null ? "" : firstName;
String m = middleName == null ? " " : (" " + middleName + " ");
String l = lastName == null ? "" : lastName;
return (f + m + l).replaceAll("\\s+", " ").trim();
}
public void setName(String name) {
//fake for json setter
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Set<Authority> getAuthorities() {
if (authorities == null) {
authorities = new HashSet<>();
}
return authorities;
}
public void setAuthorities(Set<Authority> authorities) {
this.authorities = authorities;
}
public Gender getGender() {
if (gender == null) {
gender = Gender.UNKNOWN;
}
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public String getLocale() {
return locale;
}
public void setLocale(String locale) {
this.locale = locale;
}
public String getTimezoneId() {
return timezoneId;
}
public void setTimezoneId(String timezoneId) {
this.timezoneId = timezoneId;
}
public Integer getTimezone() {
return timezone;
}
public void setTimezone(Integer timezone) {
this.timezone = timezone;
}
public String getPicture() {
return picture;
}
public Photo getPhoto() {
return photo;
}
public void setPhoto(Photo photo) {
this.photo = photo;
}
public void setPicture(String picture) {
this.picture = picture;
}
public Social getSocial() {
return social;
}
public void setSocial(Social social) {
this.social = social;
}
public String getSocialId() {
return socialId;
}
public void setSocialId(String socialId) {
this.socialId = socialId;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public Date getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
public String getAboutMe() {
return aboutMe;
}
public void setAboutMe(String aboutMe) {
this.aboutMe = aboutMe;
}
public Link getLink() {
return link;
}
public void setLink(Link link) {
this.link = link;
}
public Boolean getIsOwner() {
return isOwner;
}
public void setIsOwner(Boolean isOwner) {
this.isOwner = isOwner;
}
@Override
public int hashCode() {
int hash = 7;
hash = 29 * hash + Objects.hashCode(this.id);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final User other = (User) obj;
if (!Objects.equals(this.id, other.id)) {
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment