Skip to content

Commit

Permalink
Adding search and refactoring some methods
Browse files Browse the repository at this point in the history
  • Loading branch information
cilasmarques committed Dec 2, 2022
1 parent 7082566 commit 21571dc
Show file tree
Hide file tree
Showing 10 changed files with 476 additions and 395 deletions.
118 changes: 73 additions & 45 deletions src/main/java/saps/catalog/core/Catalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

public interface Catalog {

// FIXME: maybe, refactor related information into a separated data structured, e.g.:
// FIXME: maybe, refactor related information into a separated data structured,
// e.g.:
// satellite data: dataset, region and date.
// - SAPS schema: taskID, priority, userEmail.
// - versions of the processing step algorithms: inputdownloadingPhaseTag, preprocessingPhaseTag
// - versions of the processing step algorithms: inputdownloadingPhaseTag,
// preprocessingPhaseTag
// and processingPhaseTag.<br>
// - Docker: digestInputdownloading, digestPreprocessing and digestProcessing.
// FIXME: by manel. I think we can use an UUID instead of the taskId string
Expand All @@ -29,36 +31,56 @@ public interface Catalog {
/**
* It adds a new Task into this {@code Catalog}.<br>
*
* @param taskId an unique identifier for the SAPS task.
* @param dataset it is the type of data set associated with the new task to be created. Their
* values ​​can be:<br>
* -- landsat_5: indicates that the task is related to the LANDSAT 5 satellite
* (https://www.usgs.gov/land-resources/nli/landsat/landsat-5)<br>
* -- landsat_7: indicates that the task is related to the LANDSAT 7 satellite
* (https://www.usgs.gov/land-resources/nli/landsat/landsat-7)<br>
* -- landsat_8: indicates that the task is related with the LANDSAT 8 satellite data set
* (https://www.usgs.gov/land-resources/nli/landsat/landsat-8)<br>
* @param region is the location of the satellite data following the global notation system for
* Landsat data (WRS: https://landsat.gsfc.nasa.gov/the-worldwide-reference-system), following
* the PPPRRR form, where PPP is a length-3 to the path number and RRR is a length-3 to the
* row number.
* @param date is the date on which the satellite data was collected following the YYYY/MM/DD
* format.
* @param priority is an integer in the [0, 31] range that indicates the priority of task
* processing.
* @param user it is the email of the user that has submitted the task
* @param inputdownloadingPhaseTag is the version of the algorithm that will be used in the task's
* inputdownloading step
* @param digestInputdownloading is the version of the algorithm that will be used in the task's
* preprocessing step
* @param preprocessingPhaseTag is the version of the algorithm that will be used in the task's
* processing step
* @param digestPreprocessing is the immutable identifier (digest) of the Docker image of the
* version defined in the inputdownloading step (inputdownloadingPhaseTag)
* @param processingPhaseTag is the immutable identifier (digest) of the Docker image of the
* version defined in the preprocessing step (preprocessingPhaseTag)
* @param digestProcessing is the immutable identifier (digest) of the Docker image of the version
* defined in the processing step (processingPhaseTag)
* @param taskId an unique identifier for the SAPS task.
* @param dataset it is the type of data set associated with
* the new task to be created. Their
* values ​​can be:<br>
* -- landsat_5: indicates that the task is
* related to the LANDSAT 5 satellite
* (https://www.usgs.gov/land-resources/nli/landsat/landsat-5)<br>
* -- landsat_7: indicates that the task is
* related to the LANDSAT 7 satellite
* (https://www.usgs.gov/land-resources/nli/landsat/landsat-7)<br>
* -- landsat_8: indicates that the task is
* related with the LANDSAT 8 satellite data set
* (https://www.usgs.gov/land-resources/nli/landsat/landsat-8)<br>
* @param region is the location of the satellite data
* following the global notation system for
* Landsat data (WRS:
* https://landsat.gsfc.nasa.gov/the-worldwide-reference-system),
* following
* the PPPRRR form, where PPP is a length-3 to
* the path number and RRR is a length-3 to the
* row number.
* @param date is the date on which the satellite data was
* collected following the YYYY/MM/DD
* format.
* @param priority is an integer in the [0, 31] range that
* indicates the priority of task
* processing.
* @param user it is the email of the user that has
* submitted the task
* @param inputdownloadingPhaseTag is the version of the algorithm that will be
* used in the task's
* inputdownloading step
* @param digestInputdownloading is the version of the algorithm that will be
* used in the task's
* preprocessing step
* @param preprocessingPhaseTag is the version of the algorithm that will be
* used in the task's
* processing step
* @param digestPreprocessing is the immutable identifier (digest) of the
* Docker image of the
* version defined in the inputdownloading step
* (inputdownloadingPhaseTag)
* @param processingPhaseTag is the immutable identifier (digest) of the
* Docker image of the
* version defined in the preprocessing step
* (preprocessingPhaseTag)
* @param digestProcessing is the immutable identifier (digest) of the
* Docker image of the version
* defined in the processing step
* (processingPhaseTag)
* @return the new {@code SapsImage} created and added to this {@code Catalog}.
* @throws CatalogException if any unexpected behavior occurs.
*/
Expand All @@ -80,7 +102,8 @@ SapsImage addTask(
void addStateChangeTime(String taskId, ImageTaskState state, Timestamp timestamp)
throws CatalogException;

// FIXME: by manel. we want to know the possible values of the parameters. which ones are
// FIXME: by manel. we want to know the possible values of the parameters. which
// ones are
// mandatory? can we use
// null or an empty strings?

Expand All @@ -89,12 +112,14 @@ void addStateChangeTime(String taskId, ImageTaskState state, Timestamp timestamp
/**
* It adds a new user to this {@code Catalog}.
*
* @param userEmail user email used for authentication on the SAPS platform
* @param userName user name on the SAPS platform
* @param userPass user password used for authentication on the SAPS platform
* @param isEnable informs if the user is able to authenticate on the SAPS platform
* @param userEmail user email used for authentication on the SAPS platform
* @param userName user name on the SAPS platform
* @param userPass user password used for authentication on the SAPS platform
* @param isEnable informs if the user is able to authenticate on the SAPS
* platform
* @param userNotify informs the user about their tasks by email
* @param adminRole informs if the user is an administrator of the SAPS platform
* @param adminRole informs if the user is an administrator of the SAPS
* platform
* @throws CatalogException if any unexpected behavior occurs.
*/
void addUser(
Expand All @@ -106,7 +131,8 @@ void addUser(
boolean adminRole)
throws CatalogException;

// FIXME: i think we should throw an exception when there is no imageTask in the Catalog
// FIXME: i think we should throw an exception when there is no imageTask in the
// Catalog
void updateImageTask(SapsImage imageTask) throws CatalogException;

// FIXME: we should explain we return an empty list in some cases
Expand Down Expand Up @@ -138,13 +164,15 @@ List<SapsImage> filterTasks(
String processingTag)
throws CatalogException;

List<SapsImage> getTasksOnGoingWithPagination(Integer page, Integer size, String sortField, String sortOrder) throws CatalogException;

List<SapsImage> getTasksCompletedWithPagination(Integer page, Integer size, String sortField, String sortOrder) throws CatalogException;

Integer getTasksCountOnGoing() throws CatalogException;
List<SapsImage> getTasksOngoingWithPagination(String search, Integer page, Integer size,
String sortField, String sortOrder)
throws CatalogException;

Integer getTasksCountCompleted() throws CatalogException;
List<SapsImage> getTasksCompletedWithPagination(String search, Integer page, Integer size,
String sortField, String sortOrder)
throws CatalogException;

Integer getCountOngoingTasks(String search) throws CatalogException;

Integer getCountCompletedTasks(String search) throws CatalogException;
}
117 changes: 81 additions & 36 deletions src/main/java/saps/catalog/core/jdbc/JDBCCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -560,27 +560,38 @@ public List<SapsImage> filterTasks(
}
}

private String buildOngoingWithPaginationQuery(String search, Integer page, Integer size, String sortField,
String sortOrder) {
StringBuilder query = new StringBuilder("SELECT * FROM " + JDBCCatalogConstants.TablesName.TASKS);
query.append(" WHERE " + JDBCCatalogConstants.Tables.Task.STATE + " <> 'archived' ");
query.append(" AND " + JDBCCatalogConstants.Tables.Task.STATE + " <> 'failed' ");

if (search != null && !search.trim().isEmpty())
query.append(" AND to_char(image_date , 'YYYY-MM-DD') LIKE '" + search + "%' ");
if (sortField != null && sortOrder != null && !sortField.trim().isEmpty() && !sortOrder.trim().isEmpty())
query.append(" ORDER BY " + sortField + " " + sortOrder.toUpperCase());
if (page > 0 && size > 0)
query.append(" OFFSET " + (page - 1) * size + " ROWS FETCH NEXT " + size + " ROWS ONLY");

return query.toString();
}

@Override
public List<SapsImage> getTasksOnGoingWithPagination(Integer page, Integer size, String sortField, String sortOrder)
throws CatalogException {
public List<SapsImage> getTasksOngoingWithPagination(String search, Integer page, Integer size, String sortField,
String sortOrder) throws CatalogException {

Statement statement = null;
Connection connection = null;

try {
String query = buildOngoingWithPaginationQuery(search, page, size, sortField, sortOrder);

connection = getConnection();
statement = connection.createStatement();
statement.setQueryTimeout(300);

// TODO refact this
String orderByQuery = sortField.isEmpty() ? "" : " ORDER BY " + sortField + " " + sortOrder.toUpperCase();
ResultSet rs = statement.executeQuery(
"SELECT * FROM " + JDBCCatalogConstants.TablesName.TASKS
+ " WHERE " + JDBCCatalogConstants.Tables.Task.STATE + " <> 'archived' "
+ " AND " + JDBCCatalogConstants.Tables.Task.STATE + " <> 'failed' "
+ orderByQuery
+ " OFFSET " + (page - 1) * size
+ " ROWS FETCH NEXT " + size + " ROWS ONLY");

statement.execute(query);
ResultSet rs = statement.getResultSet();
return JDBCCatalogUtil.extractSapsTasks(rs);
} catch (SQLException e) {
throw new CatalogException("Error while select all tasks");
Expand All @@ -591,27 +602,38 @@ public List<SapsImage> getTasksOnGoingWithPagination(Integer page, Integer size,
}
}

private String buildCompletedWithPaginationQuery(String search, Integer page, Integer size, String sortField,
String sortOrder) {
StringBuilder query = new StringBuilder("SELECT * FROM " + JDBCCatalogConstants.TablesName.TASKS);
query.append(" WHERE (" + JDBCCatalogConstants.Tables.Task.STATE + " = 'archived' ");
query.append(" OR " + JDBCCatalogConstants.Tables.Task.STATE + " = 'failed') ");

if (search != null && !search.trim().isEmpty())
query.append(" AND to_char(image_date , 'YYYY-MM-DD') LIKE '" + search + "%' ");
if (sortField != null && sortOrder != null && !sortField.trim().isEmpty() && !sortOrder.trim().isEmpty())
query.append(" ORDER BY " + sortField + " " + sortOrder.toUpperCase());
if (page > 0 && size > 0)
query.append(" OFFSET " + (page - 1) * size + " ROWS FETCH NEXT " + size + " ROWS ONLY");

return query.toString();
}

@Override
public List<SapsImage> getTasksCompletedWithPagination(Integer page, Integer size, String sortField, String sortOrder)
public List<SapsImage> getTasksCompletedWithPagination(String search, Integer page, Integer size, String sortField,
String sortOrder)
throws CatalogException {
Statement statement = null;
Connection connection = null;

try {
String query = buildCompletedWithPaginationQuery(search, page, size, sortField, sortOrder);

connection = getConnection();
statement = connection.createStatement();
statement.setQueryTimeout(300);

// TODO refact this
String orderByQuery = sortField.isEmpty() ? "" : " ORDER BY " + sortField + " " + sortOrder.toUpperCase();
String PSQLQUERY = "SELECT * FROM " + JDBCCatalogConstants.TablesName.TASKS
+ " WHERE " + JDBCCatalogConstants.Tables.Task.STATE + " = 'archived' "
+ " OR " + JDBCCatalogConstants.Tables.Task.STATE + " = 'failed' "
+ orderByQuery
+ " OFFSET " + (page - 1) * size
+ " ROWS FETCH NEXT " + size + " ROWS ONLY";

ResultSet rs = statement.executeQuery(PSQLQUERY);
statement.execute(query);
ResultSet rs = statement.getResultSet();
return JDBCCatalogUtil.extractSapsTasks(rs);
} catch (SQLException e) {
throw new CatalogException("Error while select all tasks");
Expand All @@ -622,42 +644,65 @@ public List<SapsImage> getTasksCompletedWithPagination(Integer page, Integer siz
}
}

private String buildCountOngoingQuery(String search) {
StringBuilder query = new StringBuilder(JDBCCatalogConstants.Queries.Select.TASKS_ONGOING_COUNT);

if (search != null && !search.trim().isEmpty())
query.append(" AND to_char(image_date , 'YYYY-MM-DD') LIKE '" + search + "%' ");

return query.toString();
}

@Override
public Integer getTasksCountOnGoing() throws CatalogException {
public Integer getCountOngoingTasks(String search) throws CatalogException {
Statement statement = null;
Connection conn = null;
Connection connection = null;
try {
conn = getConnection();
statement = conn.createStatement();
String query = buildCountOngoingQuery(search);

connection = getConnection();
statement = connection.createStatement();
statement.setQueryTimeout(300);
ResultSet rs = statement.executeQuery(JDBCCatalogConstants.Queries.Select.TASKS_ONGOING_COUNT);
statement.execute(query);

ResultSet rs = statement.getResultSet();
rs.next();
return rs.getInt("count");
} catch (SQLException e) {
throw new CatalogException("Error while select all tasks");
} finally {
close(statement, conn);
close(statement, connection);
}
}

private String buildCountCompletedQuery(String search) {
StringBuilder query = new StringBuilder(JDBCCatalogConstants.Queries.Select.TASKS_COMPLETED_COUNT);

if (search != null && !search.trim().isEmpty())
query.append(" AND to_char(image_date , 'YYYY-MM-DD') LIKE '" + search + "%' ");

return query.toString();
}

@Override
public Integer getTasksCountCompleted() throws CatalogException {
public Integer getCountCompletedTasks(String search) throws CatalogException {
Statement statement = null;
Connection conn = null;
Connection connection = null;
try {
conn = getConnection();
statement = conn.createStatement();
String query = buildCountCompletedQuery(search);

connection = getConnection();
statement = connection.createStatement();
statement.setQueryTimeout(300);
ResultSet rs = statement.executeQuery(JDBCCatalogConstants.Queries.Select.TASKS_COMPLETED_COUNT);
statement.execute(query);

ResultSet rs = statement.getResultSet();
rs.next();
return rs.getInt("count");
} catch (SQLException e) {
throw new CatalogException("Error while select all tasks");
} finally {
close(statement, conn);
close(statement, connection);
}
}

}
Loading

0 comments on commit 21571dc

Please sign in to comment.