dtu.ds.warnme.model.impl.UserEntity.java Source code

Java tutorial

Introduction

Here is the source code for dtu.ds.warnme.model.impl.UserEntity.java

Source

/**
 * Project:   warnme-server
 * File:      UserEntity.java
 * License: 
 *            This file is licensed under GNU General Public License version 3
 *            http://www.gnu.org/licenses/gpl-3.0.txt
 *
 * Copyright: Bartosz Cichecki [ cichecki.bartosz@gmail.com ]
 * Date:      19-03-2014
 */

package dtu.ds.warnme.model.impl;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;

import dtu.ds.warnme.model.AbstractEntity;
import dtu.ds.warnme.model.Mergeable;

/**
 * @author Bartosz Cichecki
 */
@Entity
@Table(name = "USERS")
public class UserEntity extends AbstractEntity implements Mergeable<UserEntity> {

    @Transient
    private static final long serialVersionUID = -3895708148603521817L;

    @Column(name = "USERNAME", nullable = false, unique = true, length = 32)
    protected String username;

    @Column(name = "PASSWORD", nullable = false, unique = false)
    protected String password;

    @Column(name = "EMAIL", nullable = false, unique = true)
    protected String email;

    @Column(name = "LOCKED", nullable = false, unique = false)
    protected boolean locked;

    @Column(name = "COMMENT", nullable = true, unique = false, length = 1000)
    protected String comment;

    @Enumerated(EnumType.STRING)
    @Column(name = "ROLE", nullable = false, unique = false)
    protected Role role;

    @Column(name = "DELETED", nullable = false, unique = false)
    protected boolean deleted;

    public UserEntity() {
        super();
    }

    public UserEntity(String username, String password, String email, boolean locked, String comment,
            boolean deleted) {
        super();
        this.username = username;
        this.password = password;
        this.email = email;
        this.locked = locked;
        this.comment = comment;
        this.deleted = deleted;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!super.equals(obj)) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        UserEntity other = (UserEntity) obj;
        if (comment == null) {
            if (other.comment != null) {
                return false;
            }
        } else if (!comment.equals(other.comment)) {
            return false;
        }
        if (deleted != other.deleted) {
            return false;
        }
        if (email == null) {
            if (other.email != null) {
                return false;
            }
        } else if (!email.equals(other.email)) {
            return false;
        }
        if (locked != other.locked) {
            return false;
        }
        if (password == null) {
            if (other.password != null) {
                return false;
            }
        } else if (!password.equals(other.password)) {
            return false;
        }
        if (role != other.role) {
            return false;
        }
        if (username == null) {
            if (other.username != null) {
                return false;
            }
        } else if (!username.equals(other.username)) {
            return false;
        }
        return true;
    }

    public String getComment() {
        return comment;
    }

    public String getEmail() {
        return email;
    }

    public String getPassword() {
        return password;
    }

    public Role getRole() {
        return role;
    }

    public String getUsername() {
        return username;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = super.hashCode();
        result = prime * result + (comment == null ? 0 : comment.hashCode());
        result = prime * result + (deleted ? 1231 : 1237);
        result = prime * result + (email == null ? 0 : email.hashCode());
        result = prime * result + (locked ? 1231 : 1237);
        result = prime * result + (password == null ? 0 : password.hashCode());
        result = prime * result + (role == null ? 0 : role.hashCode());
        result = prime * result + (username == null ? 0 : username.hashCode());
        return result;
    }

    public boolean isDeleted() {
        return deleted;
    }

    public boolean isLocked() {
        return locked;
    }

    @Override
    public void merge(UserEntity user) {
        setUsername(StringUtils.defaultString(user.getUsername()));
        setPassword(StringUtils.defaultString(user.getPassword()));
        setEmail(StringUtils.defaultString(user.getEmail()));
        setComment(StringUtils.defaultIfBlank(user.getComment(), null));
        setLocked(user.isLocked());
        setDeleted(user.isDeleted());
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

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

    public void setLocked(boolean locked) {
        this.locked = locked;
    }

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

    public void setRole(Role role) {
        this.role = role;
    }

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

    @Override
    public String toString() {
        return "UserEntity [username=" + username + ", password=" + password + ", email=" + email + ", locked="
                + locked + ", comment=" + comment + ", role=" + role + ", deleted=" + deleted + ", id=" + id
                + ", creationUserId=" + creationUserId + ", modificationUserId=" + modificationUserId
                + ", creationDate=" + creationDate + ", modificationDate=" + modificationDate + ", version="
                + version + "]";
    }

}