ua.its.slot7.caccounting.model.userartoken.UserARToken.java Source code

Java tutorial

Introduction

Here is the source code for ua.its.slot7.caccounting.model.userartoken.UserARToken.java

Source

package ua.its.slot7.caccounting.model.userartoken;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Index;
import ua.its.slot7.caccounting.utils.Constants;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

/**
 * CAccounting
 * 26.08.13 : 0:00
 * Alex Velichko
 * alex.itstudio@gmail.com
 * <p/>
 * <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">
 * <img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-sa/3.0/88x31.png" />
 * </a><br />
 * This work is licensed under a
 * <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-ShareAlike 3.0 Unported License</a>.
 */
@Entity
public class UserARToken implements Serializable {

    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
    public long getId() {
        return id;
    }

    @Column(nullable = false)
    @Index(name = "email")
    public String getEmail() {
        return email;
    }

    @Column(nullable = false)
    @Index(name = "tokenCode")
    public String getTokenCode() {
        return tokenCode;
    }

    @Column(nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    public Date getPeriodBegin() {
        return periodBegin;
    }

    @Column(nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    public Date getPeriodEnd() {
        return periodEnd;
    }

    public UserARToken() {
    }

    public UserARToken(final String email) {
        if (StringUtils.isBlank(email)) {
            throw new IllegalArgumentException("Arguments must be not null or empty");
        }
        this.setEmail(email);

        Date d = new Date();
        this.setPeriodBegin(d);
        long pe = this.getPeriodBegin().getTime() + Constants.USERARTOKEN_DURATION;
        this.setPeriodEnd(new Date(pe));

        UUID uuid = UUID.randomUUID();
        this.setTokenCode(uuid.toString());
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("UserARToken : {");
        sb.append("id=").append(id);
        sb.append(", email='").append(email).append('\'');
        sb.append(", tokenCode='").append(tokenCode).append('\'');
        sb.append(", periodBegin=").append(periodBegin);
        sb.append(", periodEnd=").append(periodEnd);
        sb.append('}');
        return sb.toString();
    }

    public void setId(long id) {
        this.id = id;
    }

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

    public void setTokenCode(String tokenCode) {
        this.tokenCode = tokenCode;
    }

    public void setPeriodBegin(Date periodBegin) {
        this.periodBegin = periodBegin;
    }

    public void setPeriodEnd(Date periodEnd) {
        this.periodEnd = periodEnd;
    }

    private long id;
    private String email;
    private String tokenCode;
    private Date periodBegin;
    private Date periodEnd;
}