/*
 * 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.analytics.common;

import java.util.Date;
import org.hisp.dhis.program.AnalyticsType;
import org.hisp.dhis.program.ProgramIndicator;
import org.hisp.dhis.relationship.RelationshipType;

/**
 * Component responsible for generating a complete sub-query which fetches the aggregated values of
 * the specified Program Indicator. This component is designed to be invoked when listing events or
 * enrollments and the list requires an additional value derived from the Program Indicator
 * expression/filter.
 *
 * <p>For instance:
 *
 * <pre>
 * SELECT
 *    pi,
 *    tei,
 *    ou, ...
 *    (select avg( PROGRAM INDICATOR SUBQUERY )) // generated by this component
 * FROM
 *    analytics_enrollment_uy2gu8kt1jf as ax // this is the "outer" sql entity
 * WHERE
 *    ...
 * </pre>
 *
 * An example of a generated sub-query is:
 *
 * <pre>
 * avg((date_part('year', age(cast(executiondate as date), cast("iESIqZ0R0R0" as date)))))
 * FROM analytics_event_uy2gu8kt1jf as subax"
 * </pre>
 *
 * Note that this component does not add the {@code SELECT } keyword to the generated query.
 *
 * @author Luciano Fiandesio
 */
public interface ProgramIndicatorSubqueryBuilder {
  /**
   * Generates the program indicator sub-query to be used as aggregation column within an
   * enrollment/event list query.
   *
   * @param programIndicator a {@link ProgramIndicator} object
   * @param outerSqlEntity a {@link AnalyticsType} object, representing the outer SQL context.
   * @param earliestStartDate the earliest reporting start date.
   * @param latestDate the latest reporting end date.
   * @return a string containing a program indicator sub-query.
   */
  String getAggregateClauseForProgramIndicator(
      ProgramIndicator programIndicator,
      AnalyticsType outerSqlEntity,
      Date earliestStartDate,
      Date latestDate);

  /**
   * Generates the program indicator sub-query to be used as aggregation column within an
   * enrollment/event list query. This method accepts a {@link RelationshipType} object, that is
   * used to filter the program indicator values by the type of relationship specified. For
   * instance, given a "Mother > Child" relationship type, this method will generated a SQL query
   * that will fetch only the "Child" side of the relationship when aggregating the value for the
   * program indicator.
   *
   * @param programIndicator the {@link ProgramIndicator}.
   * @param relationshipType the {@link RelationshipType}.
   * @param outerSqlEntity the {@link AnalyticsType} object, representing the outer SQL context.
   * @param earliestStartDate the earliest reporting start date.
   * @param latestDate the latest reporting end date.
   * @return a String containing a Program Indicator sub-query
   */
  String getAggregateClauseForProgramIndicator(
      ProgramIndicator programIndicator,
      RelationshipType relationshipType,
      AnalyticsType outerSqlEntity,
      Date earliestStartDate,
      Date latestDate);
}
