ca.ualberta.physics.cssdp.domain.auth.Session.java Source code

Java tutorial

Introduction

Here is the source code for ca.ualberta.physics.cssdp.domain.auth.Session.java

Source

/* ============================================================
 * Session.java
 * ============================================================
 * Copyright 2013 University of Alberta
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============================================================ 
 */
package ca.ualberta.physics.cssdp.domain.auth;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.hibernate.annotations.Type;
import org.joda.time.Hours;
import org.joda.time.LocalDateTime;

import ca.ualberta.physics.cssdp.dao.Persistent;

@Entity
@Table(name = "auth_session")
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Session extends Persistent {

    private static final long serialVersionUID = 1L;

    @ManyToOne
    @JoinColumn(name = "user_id", nullable = false)
    private User user;

    @Column(name = "token", length = 22, nullable = false)
    private String token;

    @Column(name = "token_date", nullable = false)
    @Type(type = "ca.ualberta.physics.cssdp.dao.type.PersistentLocalDateTime")
    private LocalDateTime tokenDate = new LocalDateTime();

    public Session() {

    }

    public Session(User user, String token) {
        this.user = user;
        this.token = token;
    }

    @Override
    public String _pk() {
        return token;
    }

    @XmlElement
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @XmlElement
    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public boolean hasExpired() {
        return Hours.hoursBetween(tokenDate, new LocalDateTime()).getHours() > 48;
    }

}