com.qatickets.domain.UserProfile.java Source code

Java tutorial

Introduction

Here is the source code for com.qatickets.domain.UserProfile.java

Source

/*
The MIT License (MIT)
    
Copyright (c) 2015 QAXML Pty Ltd
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.qatickets.domain;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.SafeHtml;
import org.joda.time.DateTimeZone;

import com.qatickets.common.BCrypt;

@SuppressWarnings("serial")
@Entity
@Table(name = "USER_PROFILE", indexes = { @Index(name = "USER_PROFILE_EMAIL_INDEX", columnList = "EMAIL"),
        @Index(name = "USER_PROFILE_UUID_INDEX", columnList = "UUID") })
public class UserProfile implements Serializable {

    public static final int EMAIL_MAX_LENGTH = 128;
    public static final int NAME_MAX_LENGTH = 128;

    @Id
    @Column(name = "USER_PROFILE_ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @NotNull
    @Length(max = 36)
    @Column(name = "UUID", unique = true)
    private String uuid = UUID.randomUUID().toString();

    @NotNull
    @SafeHtml
    @Length(min = 2, max = NAME_MAX_LENGTH)
    @Column(name = "NAME", unique = true)
    private String name;

    @Length(max = 200)
    @Column(name = "PASSWORD")
    private String password;

    @NotNull
    @Email
    @Length(max = EMAIL_MAX_LENGTH)
    @Column(name = "EMAIL", unique = true)
    private String email;

    @NotNull
    @Column(name = "DATE")
    private Date date;

    @NotNull
    @Column(name = "LAST_DATE")
    private Date lastAccessDate;

    @NotNull
    @Column(name = "TZ")
    private int timezone = 0;

    @Column(name = "PASSWORD_RESET_TOKEN", columnDefinition = "CHAR(36)")
    private String passwordResetToken;

    @NotNull
    @Column(name = "HAS_AVATAR")
    private boolean hasAvatar;

    @NotNull
    @Column(name = "ADMIN")
    private boolean admin;

    @NotNull
    @Column(name = "DEACTIVATED")
    private boolean deactivated = false;

    public boolean isAdmin() {
        return admin;
    }

    public void setAdmin(boolean admin) {
        this.admin = admin;
    }

    public boolean isDeactivated() {
        return deactivated;
    }

    public void setDeactivated(boolean deactivated) {
        this.deactivated = deactivated;
    }

    public boolean isHasAvatar() {
        return hasAvatar;
    }

    public void setHasAvatar(boolean hasAvatar) {
        this.hasAvatar = hasAvatar;
    }

    public String getPasswordResetToken() {
        return passwordResetToken;
    }

    public void setPasswordResetToken(String passwordResetToken) {
        this.passwordResetToken = passwordResetToken;
    }

    public String getUuid() {
        return uuid;
    }

    public int getTimezone() {
        return timezone;
    }

    public int getTimezoneInMs() {
        return -timezone * 60 * 1000;
    }

    public DateTimeZone getDateTimeZone() {
        return DateTimeZone.forOffsetMillis(this.getTimezoneInMs());
    }

    public int getTimezoneGMT() {
        return -timezone / 60;
    }

    public void setTimezone(int timezone) {
        this.timezone = timezone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

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

    public int getId() {
        return id;
    }

    public void setPassword(String password) {
        this.password = BCrypt.hashpw(password, BCrypt.gensalt(8));
    }

    public boolean matchPassword(String raw) {
        return BCrypt.checkpw(raw, password);
    }

    public Date getDate() {
        return date;
    }

    public String getDateAsString() {
        return new SimpleDateFormat("dd MMM yyyy").format(date);
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Date getLastAccessDate() {
        return lastAccessDate;
    }

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

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + id;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        UserProfile other = (UserProfile) obj;
        if (id != other.id)
            return false;
        return true;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    public boolean isNew() {
        return id == 0;
    }

}