GenericEventChild.java 2.39 KB
package fi.insomnia.bortal.model;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import javax.persistence.Version;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fi.insomnia.bortal.utilities.PasswordFunctions;

@MappedSuperclass
public abstract class GenericEventChild implements EventChildInterface {

    private static final Logger logger = LoggerFactory.getLogger(GenericEventChild.class);
    /**
     * 
     */
    private static final long serialVersionUID = -9041737052951021560L;
    @EmbeddedId
    private EventPk id;
    @Version
    @Column(nullable = false)
    private int jpaVersionField = 0;

    public GenericEventChild(LanEvent event) {
        id = new EventPk(event);
    }

    public GenericEventChild() {
        super();
    }

    public GenericEventChild(EventPk eventPk) {
        id = new EventPk(eventPk.getEventId());
    }

    @Override
    public boolean equals(Object object) {

        if (!this.getClass().equals(object.getClass())) {
            logger.debug("Classes dont match {} {}", getClass(), object.getClass());
            return false;
        }
        GenericEventChild other = (GenericEventChild) object;
        if (tempId != null && tempId.equals(other.tempId)) {
            return true;
        }

        if (this.id == null) {
            if (other.id != null) {
                return false;
            }
            logger.debug("Ids are null. Resorting to randomidcheck!");
            return getRandomId().equals(other.tempId);
        }
        return this.id.equals(other.id);
    }

    @Transient
    private String tempId;

    private String getRandomId() {
        if (tempId == null) {
            tempId = PasswordFunctions.generateRandomString(10);
        }
        return tempId;
    }

    @Override
    public final EventPk getId() {
        return id;
    }

    @Override
    public final void setId(EventPk id) {
        this.id = id;
    }

    @Override
    public final int getJpaVersionField() {
        return jpaVersionField;
    }

    @Override
    public final void setJpaVersionField(int jpaVersionField) {
        this.jpaVersionField = jpaVersionField;
    }

    @Override
    public final int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : getRandomId().hashCode());
        return hash;
    }

}