no.abmu.user.domain.User.java Source code

Java tutorial

Introduction

Here is the source code for no.abmu.user.domain.User.java

Source

/*$Id: User.java 13026 2009-02-14 01:28:46Z jens $*/
/*
 ****************************************************************************
 *                                                                          *
 *                   (c) Copyright 2005 ABM-utvikling                       *
 *                                                                          *
 * This program is free software; you can redistribute it and/or modify it  *
 * under the terms of the GNU General Public License as published by the    *
 * Free Software Foundation; either version 2 of the License, or (at your   *
 * option) any later version.                                               *
 *                                                                          *
 * This program is distributed in the hope that it will be useful, but      *
 * WITHOUT ANY WARRANTY; without even the implied warranty of               *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
 * Public License for more details. http://www.gnu.org/licenses/gpl.html    *
 *                                                                          *
 ****************************************************************************
 */

package no.abmu.user.domain;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.annotations.Index;
import org.hibernate.classic.Lifecycle;

/**
 * This class represents a user in the system.
 *
 * @author Erik Romson, erik@zenior.no
 * @author $Author: jens $
 * @version $Rev: 13026 $
 * @date $Date: 2009-02-14 02:28:46 +0100 (Sat, 14 Feb 2009) $
 * @copyright ABM-Utvikling
 *
 * @hibernate.joined-subclass
 * table="AbmUser"
 * @hibernate.joined-subclass-key
 *  column="id"
 */
@SuppressWarnings("serial")
@Entity
@Table(name = "abmuser")
@PrimaryKeyJoinColumn(name = "id")
public class User extends Principal implements Lifecycle {

    private static final Log logger = (Log) LogFactory.getLog(User.class);

    private String password;
    private ContactInfo contactInfo;
    private boolean enabled = true;

    /**
     * Indicates if this user is a temporary user.
     */
    private boolean temporaryUser;

    public User() {
        this.temporaryUser = false;
    }

    public User(String name, String password) {
        super(name);
        this.password = password;
        this.temporaryUser = false;
    }

    /**
     * getId, primary key.
     * 
     * @return
     * @ hibernate.id generator-class="increment"
     */
    //    @Column(insertable=false, updatable=false)
    //    public Long getId() {
    //        return super.getId();
    //    }

    /**
     * getPassword.
     * 
     * @return
     * @hibernate.property
     * update="true"
     * insert="true"
     * not-null="true"
     * length="20"
     */
    @Column(length = 20, nullable = false)
    public String getPassword() {
        return password;
    }

    /**
     * getContactInfo.
     * 
     * @return
     * @hibernate.many-to-one
     * cascade="all"
     *
     * @hibernate.column name="contactInfo"
     *      index="user_contactInfo_idx"
     */
    @ManyToOne(cascade = { CascadeType.ALL })
    @JoinColumn(name = "contactInfo")
    public ContactInfo getContactInfo() {
        return contactInfo;
    }

    /**
     * isEnabled.
     * 
     * @return
     *
     * @hibernate.property
     */
    public boolean isEnabled() {
        return enabled;
    }

    /**
     * Flag indicating if the user is temporary. Typical usage is for
     * users that should have access to the system for a short period.
     *
     * @return true if temporary, otherwise false
     *
     * @hibernate.property
     */
    public boolean isTemporaryUser() {
        return temporaryUser;
    }

    /**
     * Need to produce the user table required by acegi,
     * duplicates the data found in user in superclass Principal.
     *
     * @return
     *
     * @hibernate.property
     *
     * @hibernate.column name="username"
     *      index="user_username_idx"
     */
    @Index(name = "user_username_idx")
    public String getUsername() {
        return super.getName();
    }

    /**
     * Sets the name.
     * 
     * @param userName
     */
    public void setUsername(String userName) {
        super.setName(userName);
    }

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

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

    public void setContactInfo(ContactInfo contactInfo) {
        this.contactInfo = contactInfo;
    }

    /**
     * Specified this user as a temporary user.
     *
     * @param temporaryUser
     */
    public void setTemporaryUser(boolean temporaryUser) {
        this.temporaryUser = temporaryUser;
    }

    public boolean equals(Object o) {
        if (!super.equals(o)) {
            return false;
        }
        final User user = (User) o;

        if (getContactInfo() != null ? !getContactInfo().equals(user.getContactInfo())
                : user.getContactInfo() != null) {
            return false;
        }
        if (getPassword() != null ? !getPassword().equals(user.getPassword()) : user.getPassword() != null) {
            return false;
        }

        return true;
    }

    public boolean equalsAttr(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof User)) {
            return false;
        }
        if (!super.equalsAttr(o)) {
            return false;
        }

        final User user = (User) o;

        if (getContactInfo() != null ? !getContactInfo().equals(user.getContactInfo())
                : user.getContactInfo() != null) {
            return false;
        }
        if (!getPassword().equals(user.getPassword())) {
            return false;
        }

        return true;
    }

    public int hashCode() {
        int result = super.hashCode();
        result = 29 * result + (getPassword() != null ? getPassword().hashCode() : 0);
        result = 29 * result + (getContactInfo() != null ? getContactInfo().hashCode() : 0);
        return result;
    }

    public String toString() {
        return super.toString() + " ::: User{" + "password='" + password + "'" + ", contactInfo=" + contactInfo
                + "}";
    }

    public boolean onDelete(Session s) {
        return false;
    }

    public void onLoad(Session s, Serializable id) {
    }

    public boolean onSave(Session s) {
        if (getName() == null) {
            setName(getId().toString());
        }
        return false;
    }

    public boolean onUpdate(Session s) {
        return false;
    }

}