Ticket.java

/*
 * Copyright © 2023-2025 The CTAN Team and individual authors
 *
 * This file is distributed under the 3-clause BSD license.
 * See file LICENSE for details.
 */
package org.ctan.site.domain.account;

import java.time.LocalDateTime;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Builder.Default;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * The class <code>Ticket</code> represents a request to perform some operation
 * which requires an acknowledgment from the user. For instance the change of
 * the password.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
@Entity
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@Builder
@SuppressFBWarnings(value = "EI_EXPOSE_REP")
public class Ticket {

    /**
     * The field <code>REGISTER_PREFIX</code> contains the prefix indicating a
     * registration request.
     */
    public static final String REGISTER_PREFIX = "R-";

    /**
     * The field <code>PASSWORD_PREFIX</code> contains the prefix indicating a
     * password request.
     */
    public static final String PASSWORD_PREFIX = "P-";

    /**
     * The field <code>DELETE_PREFIX</code> contains the prefix indicating a
     * delete request.
     */
    public static final String DELETE_PREFIX = "D-";

    /**
     * The field <code>key</code> contains the unique key of the ticket.
     */
    @Id
    @Column(nullable = false, unique = true, length = 128)
    private String key;

    /**
     * The field <code>account</code> contains the login name of the user.
     */
    @Column(nullable = false, length = 64)
    private String account;

    /**
     * The field <code>dateCreated</code> contains the creation date.
     */
    @Column(name = "date_created")
    @Default
    private LocalDateTime dateCreated = LocalDateTime.now();
    /*
     * static constraints = { key blank:false, unique:true, maxSize: 128,
     * index:'ticket_key_idx' username blank:false, maxSize: 64 dateCreated() }
     */

    /**
     * Check whether the ticket has expired its lifetime. For this purpose the
     * lifetime is passed in and the creation date is compared to the current
     * date wrt. the lifetime.
     *
     * @param lifetime the lifetime in minutes
     *
     * @return {@code true} iff the ticket is not too old
     */
    public boolean isExpired(long lifetime) {

        // return dateCreated.getTime() + lifetime * 60000L < new
        // Date().getTime();
        return dateCreated.plusMinutes(lifetime).isBefore(LocalDateTime.now());
    }

    /**
     * Check whether this Ticket deals with a password request.
     *
     * @return {@code true} iff this Ticket deals with a password request
     */
    public boolean isPasswordRequest() {

        return key.startsWith(PASSWORD_PREFIX);
    }

    /**
     * Check whether this Ticket deals with a registration.
     *
     * @return {@code true} iff this Ticket deals with a registration
     */
    public boolean isRegistration() {

        return key.startsWith(REGISTER_PREFIX);
    }

    /**
     * Check whether this Ticket deals with a withdrawal.
     *
     * @return {@code true} iff this Ticket deals with an withdrawal
     */
    public boolean isWithdraw() {

        return key.startsWith(DELETE_PREFIX);
    }

    /**
     * {@inheritDoc}
     *
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {

        return key + " " + account;
    }
}