com.josue.kingdom.credential.entity.Manager.java Source code

Java tutorial

Introduction

Here is the source code for com.josue.kingdom.credential.entity.Manager.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.josue.kingdom.credential.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.josue.kingdom.rest.Resource;
import com.josue.kingdom.rest.TenantResource;
import com.josue.kingdom.util.validation.Email;
import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;
import org.apache.shiro.authc.AuthenticationToken;

/**
 *
 * @author Josue
 */
@Entity
@Table(name = "manager", uniqueConstraints = { @UniqueConstraint(columnNames = { "email", "application_uuid" }),
        @UniqueConstraint(columnNames = { "username", "application_uuid" }) })
public class Manager extends TenantResource implements AuthenticationToken {

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Email
    @NotNull
    private String email;

    private String username;

    @NotNull
    @JsonIgnore //Allowed only for Deserialization, set method must be annotated with @JsonProperty
    private String password;

    @NotNull
    @Enumerated(EnumType.STRING)
    private AccountStatus status;

    @Override
    public void copyUpdatable(Resource newData) {
        if (newData instanceof Manager) {
            Manager manager = (Manager) newData;
            firstName = manager.firstName == null ? firstName : manager.firstName;
            lastName = manager.lastName == null ? lastName : manager.lastName;
            //TODO how to update 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 String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

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

    public AccountStatus getStatus() {
        return status;
    }

    public void setStatus(AccountStatus status) {
        this.status = status;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 17 * hash + Objects.hashCode(this.firstName);
        hash = 17 * hash + Objects.hashCode(this.lastName);
        hash = 17 * hash + Objects.hashCode(this.email);
        hash = 17 * hash + Objects.hashCode(this.username);
        hash = 17 * hash + Objects.hashCode(this.password);
        hash = 17 * hash + Objects.hashCode(this.status);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Manager other = (Manager) obj;
        if (!Objects.equals(this.firstName, other.firstName)) {
            return false;
        }
        if (!Objects.equals(this.lastName, other.lastName)) {
            return false;
        }
        if (!Objects.equals(this.email, other.email)) {
            return false;
        }
        if (!Objects.equals(this.username, other.username)) {
            return false;
        }
        if (!Objects.equals(this.password, other.password)) {
            return false;
        }
        return this.status == other.status;
    }

    @Override
    public Object getPrincipal() {
        if (email == null) {
            return username;
        }
        return email;
    }

    @Override
    public Object getCredentials() {
        return password;
    }

}