covid_10012023_covid-reports/src/Main.java

100 lines
4.3 KiB
Java

import data.dtos.QueryDTO;
import data.enums.CustomPreparedStatementsRead;
import data.repos.QueryResultsRepository;
import services.AppConfigService;
import services.DatabaseService;
import services.ReportGenerationService;
import java.sql.SQLException;
/**
* Entry point of the application
*/
public class Main {
public static void main(String[] args) {
final App app = new App();
app.run();
}
}
/**
* Responsible for the overall flow of the application. It creates and manages instances Services.
*/
class App {
public App() {
databaseService = new DatabaseService();
appConfigService = new AppConfigService();
reportGenerationService = new ReportGenerationService();
}
final private DatabaseService databaseService;
final private AppConfigService appConfigService;
final private ReportGenerationService reportGenerationService;
/**
* Endpoints mentioned under the `Report` section
*/
private static final CustomPreparedStatementsRead[] reportEndpoints = {
CustomPreparedStatementsRead.HighestAndLowest10Vaccination,
CustomPreparedStatementsRead.HighestInfectionsPer100K,
CustomPreparedStatementsRead.CountriesLaggingBehind,
};
/**
* Endpoints mentioned under the `Query` section
*/
private static final CustomPreparedStatementsRead[] queryEndpoints = {
CustomPreparedStatementsRead.DailyInfectionsAndDeathAggregate,
};
/**
* Responsible for the overall flow of the application.
* It retrieves data from the database using the `DatabaseService` class,
* generates reports using the `ReportGenerationService` class, and prompts the user for input using the `AppConfigService` class.
*/
public void run() {
executeNoInteractionEndpointPipeline(reportEndpoints);
executeInteractiveEndpointPipeline(queryEndpoints);
}
/**
* This method is responsible for executing a pipeline of read-only queries, using a provided set of
* {@link CustomPreparedStatementsRead} queryEndpoints, without any user interaction.
* The method will iterate over the provided queryEndpoints, and for each query it will:
* Execute the read query using {@link DatabaseService#getReadReportsEndpoint(QueryDTO)}
* Store the results in a new {@link QueryResultsRepository}
* Generate a report for the stored results using {@link ReportGenerationService#reportBaseResults(QueryResultsRepository)}
* @param queryEndpoints an array of {@link CustomPreparedStatementsRead} containing the queries to be executed.
* @throws RuntimeException when there is a SQLException thrown by {@link DatabaseService#getReadReportsEndpoint(QueryDTO)}
*/
private void executeNoInteractionEndpointPipeline(CustomPreparedStatementsRead[] queryEndpoints) {
QueryResultsRepository resultsRepository;
for (final CustomPreparedStatementsRead query : queryEndpoints) {
try {
resultsRepository = databaseService.getReadReportsEndpoint(new QueryDTO(query));
} catch (SQLException e) {
throw new RuntimeException(e);
}
reportGenerationService.reportBaseResults(resultsRepository);
}
}
/**
* Executes a series of read queries specified in the {@code queryEndpoints} parameter, and prompts the user for input of country name and date.
* The results of the queries are passed to reportGenerationService to be reported.
* @param queryEndpoints the endpoints to execute in the pipeline
* @throws RuntimeException if a SQLException occurs when getting the read reports endpoint
*/
private void executeInteractiveEndpointPipeline(CustomPreparedStatementsRead[] queryEndpoints) {
QueryResultsRepository resultsRepository;
try {
for(final CustomPreparedStatementsRead query: queryEndpoints) {
appConfigService.promptAndSetUserArguments();
resultsRepository = databaseService.getReadReportsEndpoint(new QueryDTO(query, new Object[]{appConfigService.getCountryName(), appConfigService.getDate(),}));
reportGenerationService.reportBaseResults(resultsRepository);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}