All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.iintelligas.persist.entity.UserEntity Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
package com.github.iintelligas.persist.entity;

import com.github.iintelligas.persist.dto.User;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;


@Entity
@Table(name = "user")
public class UserEntity implements UserDetails {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long id;

    @Column(unique = true)
    private String username;

    private String password;

    private String firstName;

    private String lastName;

    private String email;


    @Temporal(TemporalType.TIMESTAMP)
    private Date lastAccessDate;

    @Temporal(TemporalType.TIMESTAMP)
    private Date createdOn;

    private String createdBy;

    private Date updatedOn;

    private String updatedBy;

    private boolean enabled;

    private boolean tokenExpired;

    @ManyToMany
    @LazyCollection(LazyCollectionOption.FALSE)
    @JoinTable(name="user_role",
            joinColumns = {
                    @JoinColumn(name = "user_id", referencedColumnName = "id")},
            inverseJoinColumns = {
                    @JoinColumn(name="role_id", referencedColumnName = "id")
            }
    )
    private Set roles;

    public UserEntity() {

    }

    public UserEntity(String username, String password, String firstName, String lastName, String email) {
        this.username = username;
        this.password = password;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.tokenExpired = false;
    }

    public UserEntity(User user) {
        this.id = user.getId();
        this.username = user.getUsername();
        this.password = user.getPassword();
        this.firstName = user.getFirstName();
        this.lastName = user.getLastName();
        this.email = user.getEmail();
        this.lastAccessDate = user.getLastAccessDate();
        this.createdOn = user.getCreatedOn();
        this.createdBy = user.getCreatedBy();
        this.updatedOn = user.getUpdatedOn();
        this.updatedBy = user.getUpdatedBy();
        this.enabled = user.getEnabled();
        this.tokenExpired = user.isTokenExpired();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getLastAccessDate() {
        return lastAccessDate;
    }

    public void setLastAccessDate(Date lastAccessDate) {
        this.lastAccessDate = lastAccessDate;
    }

    public Date getCreatedOn() {
        return createdOn;
    }

    private void setCreatedOn(Date createdOn) {
        this.createdOn = createdOn;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public Date getUpdatedOn() {
        return updatedOn;
    }

    private void setUpdatedOn(Date updatedOn) {
        this.updatedOn = updatedOn;
    }

    public String getUpdatedBy() {
        return updatedBy;
    }

    public void setUpdatedBy(String updatedBy) {
        this.updatedBy = updatedBy;
    }

    public boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public boolean isTokenExpired() {
        return tokenExpired;
    }

    public void setTokenExpired(boolean tokenExpired) {
        this.tokenExpired = tokenExpired;
    }

    public Set getRoles() {
        return roles;
    }

    public void addRole( RoleEntity role) {
        if(roles == null) {
            roles = new HashSet<>();
        }
        roles.add(role);
    }
    public void setRoles(Set roles) {
        this.roles = roles;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public Collection getAuthorities() {
        Set authorities = new HashSet<>();
        authorities.addAll(getRoles());
        authorities.addAll(getPermissions());
        return authorities;
    }

	@Transient
	private Set getPermissions() {
		Set permissionEntities = new HashSet<>();
		for(RoleEntity role : getRoles()){
			permissionEntities.addAll(role.getPermissions());
		}
		return permissionEntities;
	}


	@PrePersist
    protected void onCreate() {
        setCreatedOn(new Date());
    }

    @PreUpdate
    protected void onUpdate() {
        setUpdatedOn(new Date());
    }
    @Override
    public String toString() {
        return "UserEntity{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", email='" + email + '\'' +
                ", lastAccessDate=" + lastAccessDate +
                ", createdOn=" + createdOn +
                ", createdBy='" + createdBy + '\'' +
                ", updatedOn=" + updatedOn +
                ", updatedBy='" + updatedBy + '\'' +
                ", enabled='" + enabled + '\'' +
                ", tokenExpired=" + tokenExpired +
                ", roles=" + roles +
                '}';
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy