Message.java

/*
 * Copyright (C) 2012-2025 Gerd Neugebauer
 *
 * This file is distributed under the 3-clause BSD license.
 * See file LICENSE for details.
 */
package org.ctan.site.domain.site;

import java.time.LocalDateTime;

import org.ctan.site.domain.AbstractEntity;
import org.ctan.site.services.DateUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.common.collect.ImmutableMap;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

/**
 * The class <code>Message</code> represents a message.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
@Entity
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@SuppressFBWarnings(value = "EI_EXPOSE_REP")
public class Message extends AbstractEntity {

    /**
     * This enumeration lists all message types and helps mapping them to the
     * database as number.
     */
    public enum MessageType {
        /**
         * The field <code>MOTD</code> contains the indicates the message of the
         * day.
         */
        MOTD,
        /**
         * The field <code>POPUP</code> contains the indicator for the pop-up
         * message to be shown once on each page.
         */
        POPUP,
        /**
         * The field <code>RIBBON</code> contains the indicator for the ribbon
         * message to be shown once on each page.
         */
        RIBBON,
        /**
         * The field <code>UPLOAD_ERROR</code> contains the indicator for the
         * upload error message.
         */
        UPLOAD_ERROR,
        /**
         * The field <code>UPLOAD_DOWN</code> contains the message if the upload
         * is not processed.
         */
        UPLOAD_DOWN;
    }

    @Enumerated(EnumType.STRING)
    private MessageType type;

    @Column(name = "title_en", length = 255, nullable = false)
    private String titleEn;

    @Column(name = "text_en", length = 1024)
    private String textEn;

    @Column(name = "title_de", length = 255)
    private String titleDe;

    @Column(name = "text_de", length = 1024)
    private String textDe;

    @Column(name = "valid_from")
    @JsonFormat(pattern = "dd-MM-yyyy hh:mm:ss")
    private LocalDateTime validFrom;

    @Column(name = "valid_to")
    @JsonFormat(pattern = "dd-MM-yyyy hh:mm:ss")
    private LocalDateTime validTo;

    /**
     * The method <code>toMap</code> provides means to get the instance as an
     * immutable Map.
     *
     * @return the Map
     */
    public ImmutableMap<String, Object> toMap() {

        return ImmutableMap.of("id", (Object) getId(),
            "type", type.toString(),
            "titleEn", titleEn,
            "textEn", textEn,
            "titleDe", titleDe,
            "textDe", textDe,
            "validFrom", DateUtils.formatDateTime(validFrom),
            "validTo", DateUtils.formatDateTime(validTo));
    }
}