License.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.catalogue;

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.Builder.Default;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

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

    /**
     * The field <code>key</code> contains the reference key for the license. It
     * is used in the catalogue and in the associated URL.
     *
     * <p>
     * By convention the key is made up of a lower-case letter followed by lower
     * case letters, digits and the minus sign.
     */
    @Column(length = 64, unique = true, nullable = false)
    private String key;

    /**
     * The field <code>name</code> contains the print name of the license in
     * English.
     */
    @Column(length = 128)
    private String name;

    /**
     * The field <code>url</code> contains an external URL for more infos on the
     * license.
     */
    @Column(length = 128)
    private String url;

    /**
     * The field <code>free</code> contains the classification of the license as
     * free. An unknown value is represented as {@code null}.
     */
    @Column
    @Default
    private Boolean free = true;

    /**
     * The field <code>selectable</code> contains the indicator for a license to
     * be selectable during the upload of a package.
     */
    @Column
    @Default
    private Boolean selectable = true;

    /**
     * The method <code>indexPath</code> provides means to retrieve the
     * directory containing the index files.
     *
     * @return the index directory
     * @see org.ctan.site.services.search.base.Searchable#indexPath()
     */
    @Override
    public String indexPath() {

        return "/license/" + key;
    }

    /**
     * The method <code>protect</code> provides means to use the argument or the
     * empty string for <code>null</code>.
     *
     * @param s the argument string
     * @return the protected string
     */
    private String protect(String s) {

        return s == null ? "" : s;
    }

    /**
     * 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) this.getId(),
            "key", protect(key),
            "name", protect(name),
            "free", free,
            "selectable", selectable,
            "url", protect(url));
    }

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

        IndexArgs args = IndexArgs.builder()
            .type(IndexType.LICENSES)
            .title(name)
            .display(name)
            .content(new String[]{
                name,
                key})
            .build();
        for (var locale : CtanConfig.LOCALES) {
            args.setLocale(locale);
            session.updateIndex(indexPath(), args);
        }
    }

}