TexArchiveNotes.java

/*
 * Copyright © 2024-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.archive;

import java.io.IOException;

import org.apache.lucene.index.CorruptIndexException;
import org.ctan.site.CtanConfiguration.CtanConfig;
import org.ctan.site.domain.AbstractEntity;
import org.ctan.site.services.search.base.IndexType;
import org.ctan.site.services.search.base.IndexingSession;
import org.ctan.site.services.search.base.IndexingSession.IndexArgs;
import org.ctan.site.services.search.base.Searchable;

import com.google.common.collect.ImmutableMap;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

/**
 * The class <code>TexArchiveNotes</code> contains additional infos for files or
 * directories.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
@Entity(name = "tex_archive_notes")
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public class TexArchiveNotes extends AbstractEntity implements Searchable {

    /**
     * The field <code>path</code> contains the CTAN path.
     */
    @Column(length = 1024, nullable = false, name = "ctan_path")
    private String path;

    /**
     * The field <code>noteEn</code> contains the note of the directory in
     * English.
     */
    @Column(name = "note_en", length = 1024, nullable = false)
    private String noteEn;

    /**
     * The field <code>noteDe</code> contains the note of the directory in
     * German.
     */
    @Column(name = "note_de", length = 1024, nullable = false)
    private String noteDe;

    /**
     * The method <code>getNote</code> provides means to retrieve the language
     * specific note.
     *
     * @param locale the locale
     * @return the note for the locale or the one for English as fallback
     */
    public String getNote(String locale) {

        return "de".equals(locale) ? noteDe : noteEn;
    }

    /**
     * {@inheritDoc}
     *
     * @see org.ctan.site.services.search.base.Searchable#indexPath()
     */
    @Override
    public String indexPath() {

        return path;
    }

    /**
     * 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(),
            "path", path,
            "noteEn", noteEn,
            "noteDe", noteDe);
    }

    /**
     * {@inheritDoc}
     *
     * @see org.ctan.site.services.search.base.Searchable#updateIndex(IndexingSession)
     */
    @Override
    public void updateIndex(IndexingSession session)
        throws CorruptIndexException,
            IOException {

        for (var locale : CtanConfig.LOCALES) {
            session.updateIndex(indexPath(),
                IndexArgs.builder()
                    .type(IndexType.SITE)
                    .locale(locale)
                    .title(path)
                    .display(path)
                    .content(new String[]{
                        path,
                        getNote(locale)})
                    .build());
        }
    }
}