/*
 * Copyright (c) 2004-2022, University of Oslo
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the HISP project nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.hisp.dhis.resourcetable;

import static java.time.LocalDate.now;

import java.util.List;
import java.util.Optional;

import org.hisp.dhis.common.CodeGenerator;

/**
 * @author Lars Helge Overland
 */
public abstract class ResourceTable<T>
{
    public static final int FIRST_YEAR_SUPPORTED = 1975;

    public static final int LATEST_YEAR_SUPPORTED = now().plusYears( 2 ).getYear();

    protected static final String TEMP_TABLE_SUFFIX = "_temp";

    protected List<T> objects;

    // -------------------------------------------------------------------------
    // Constructors
    // -------------------------------------------------------------------------

    protected ResourceTable()
    {
    }

    protected ResourceTable( List<T> objects )
    {
        this.objects = objects;
    }

    // -------------------------------------------------------------------------
    // Public methods
    // -------------------------------------------------------------------------

    /**
     * Provides the name of the resource database table.
     *
     * @return the name of the resource database table.
     */
    public String getTableName()
    {
        return getTableType().getTableName();
    }

    /**
     * Provides the temporary name of the resource database table.
     *
     * @return the temporary name of the resource database table.
     */
    public final String getTempTableName()
    {
        return getTableName() + TEMP_TABLE_SUFFIX;
    }

    public final String getDropTableStatement()
    {
        return "drop table " + getTableName() + ";";
    }

    public final String getDropTempTableStatement()
    {
        return "drop table " + getTempTableName() + ";";
    }

    public final String getRenameTempTableStatement()
    {
        return "alter table " + getTempTableName() + " rename to " + getTableName() + ";";
    }

    // -------------------------------------------------------------------------
    // Protected methods
    // -------------------------------------------------------------------------

    protected String getRandomSuffix()
    {
        return CodeGenerator.generateCode( 5 );
    }

    // -------------------------------------------------------------------------
    // Abstract methods
    // -------------------------------------------------------------------------

    /**
     * Returns the {@link ResourceTableType} of this resource table.
     *
     * @return the {@link ResourceTableType}.
     */
    public abstract ResourceTableType getTableType();

    /**
     * Provides a create table SQL statement for the temporary resource table.
     *
     * @return a create table statement.
     */
    public abstract String getCreateTempTableStatement();

    /**
     * Provides an insert into select from SQL statement for populating the
     * temporary resource table.
     *
     * @return an insert into select from SQL statement.
     */
    public abstract Optional<String> getPopulateTempTableStatement();

    /**
     * Provides content for the temporary resource table as a list of object
     * arrays.
     *
     * @return content for the temporary resource table.
     */
    public abstract Optional<List<Object[]>> getPopulateTempTableContent();

    /**
     * Returns SQL create index statements for the temporary table. Note that
     * the indexes name must have a random component to avoid uniqueness
     * conflicts.
     *
     * @return a list of SQL create index statements.
     */
    public abstract List<String> getCreateIndexStatements();
}

@@ -1931,16 +1931,17 @@
 Years( 2
+5
  ).getYe

Increase number of years supported by analytics exp [DHIS2-13558] (#11406)