Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/validateLandsatImage' in…
Browse files Browse the repository at this point in the history
…to feature/pagination
  • Loading branch information
cilasmarques committed Dec 2, 2022
2 parents 21571dc + 2227f2a commit 3b9b096
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.6.1</version>
<configuration>
<configuration>feature/validateLandsatImage
<!-- optional: limit format enforcement to just the files changed by
this feature branch -->
<!-- <ratchetFrom>origin/main</ratchetFrom> -->
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/saps/catalog/core/Catalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import saps.catalog.core.exceptions.UserNotFoundException;
import saps.common.core.model.SapsImage;
import saps.common.core.model.SapsUser;
import saps.common.core.model.SapsLandsatImage;
import saps.common.core.model.enums.ImageTaskState;

public interface Catalog {
Expand Down Expand Up @@ -142,6 +143,8 @@ void addUser(
*/
List<SapsImage> getAllTasks() throws CatalogException;

List<SapsLandsatImage> getLandsatImages(String region, Date date, String landsat) throws CatalogException;

List<SapsImage> getTasksByState(ImageTaskState... tasksStates) throws CatalogException;

SapsImage getTaskById(String taskId) throws CatalogException, TaskNotFoundException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ public class CatalogException extends RuntimeException {
public CatalogException(String msg) {
super(msg);
}

public CatalogException(String msg, Exception e) {
super(msg, e);
}

}
48 changes: 48 additions & 0 deletions src/main/java/saps/catalog/core/jdbc/JDBCCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;
Expand All @@ -18,6 +20,7 @@
import saps.catalog.core.exceptions.UserNotFoundException;
import saps.catalog.core.jdbc.exceptions.JDBCCatalogException;
import saps.common.core.model.SapsImage;
import saps.common.core.model.SapsLandsatImage;
import saps.common.core.model.SapsUser;
import saps.common.core.model.enums.ImageTaskState;
import saps.common.utils.SapsPropertiesUtil;
Expand Down Expand Up @@ -366,6 +369,51 @@ public List<SapsImage> getAllTasks() throws CatalogException {
}
}

@Override
public List<SapsLandsatImage> getLandsatImages(String region, Date date, String landsat) throws CatalogException {

if (region == null || region.isEmpty()) {
LOGGER.error("Invalid region " + region);
throw new IllegalArgumentException("Region is missing");
}

if (date == null) {
LOGGER.error("Invalid date " + date);
throw new NullPointerException("Invalid date (null)");
}

if (landsat == null || landsat.isEmpty()) {
LOGGER.error("Invalid landsat " + landsat);
throw new IllegalArgumentException("Landsat is missing");
}

PreparedStatement statement = null;
Connection connection = null;

try {
connection = getConnection();
statement = connection.prepareStatement(JDBCCatalogConstants.Queries.Select.LANDSAT_IMAGES);
statement.setString(1, "%" + region + "%");
statement.setDate(2, javaDateToSqlDate(date));
statement.setString(3, landsat.toUpperCase());
statement.setQueryTimeout(300);

LOGGER.info(statement.toString());
statement.execute();

ResultSet rs = statement.getResultSet();
List<SapsLandsatImage> result = JDBCCatalogUtil.extractSapsImages(rs);
return result;

} catch (SQLException e) {
throw new CatalogException("Erro while select landsat images", e);
} catch (JDBCCatalogException e) {
throw new CatalogException("Error while getting landsat images", e);
} finally {
close(statement, connection);
}
}

@Override
public SapsUser getUserByEmail(String userEmail) throws CatalogException, UserNotFoundException {

Expand Down
40 changes: 35 additions & 5 deletions src/main/java/saps/catalog/core/jdbc/JDBCCatalogConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public final class TablesName {
public static final String NOTIFY = "NOTIFY";
public static final String DEPLOY_CONFIG = "DEPLOY_CONFIG";
public static final String PROVENANCE_DATA = "PROVENANCE_DATA";
public static final String LANDSAT_IMAGES = "LANDSAT_IMAGES";
}

public final class Tables {
Expand Down Expand Up @@ -57,6 +58,12 @@ public final class Processing {
}
}

public final class LandsatImages {
public static final String DATE = "date_acquired";
public static final String REGION = "product_id";
public static final String DATASET = "spacecraft_id";
}

public final class User {
public static final String EMAIL = "user_email";
public static final String NAME = "user_name";
Expand Down Expand Up @@ -125,11 +132,22 @@ public final class Update {
public final class Select {
public static final String TASKS = "SELECT * FROM " + JDBCCatalogConstants.TablesName.TASKS;

public static final String USER = "SELECT * FROM "
+ JDBCCatalogConstants.TablesName.USERS
+ " WHERE "
+ JDBCCatalogConstants.Tables.User.EMAIL
+ " = ?";
public static final String LANDSAT_IMAGES = "SELECT * FROM "
+ JDBCCatalogConstants.TablesName.LANDSAT_IMAGES
+ " WHERE "
+ JDBCCatalogConstants.Tables.LandsatImages.REGION
+ " LIKE ? AND "
+ JDBCCatalogConstants.Tables.LandsatImages.DATE
+ " = ? AND "
+ JDBCCatalogConstants.Tables.LandsatImages.DATASET
+ " = ? LIMIT 1";

public static final String USER =
"SELECT * FROM "
+ JDBCCatalogConstants.TablesName.USERS
+ " WHERE "
+ JDBCCatalogConstants.Tables.User.EMAIL
+ " = ?";

public static final String TASKS_BY_STATE_ORDER_BY_PRIORITY_ASC = "SELECT * FROM "
+ JDBCCatalogConstants.TablesName.TASKS
Expand Down Expand Up @@ -334,4 +352,16 @@ public final class CreateTable {
+ JDBCCatalogConstants.Tables.ProvenanceData.OUTPUT_KERNEL_VERSION
+ " VARCHAR(100) )";
}


/*down the road it will create a new table, for landsat_images. but that's not necessary now
public static final String LANDSAT_IMAGES =
"CREATE TABLE IF NOT EXISTS "
+ JDBCCatalogConstants.TablesName.LANDSAT_IMAGES
+ "("
+ JDBCCatalogConstants.Tables.LandsatImages.REGION
+ " VARCHAR(40), "
+ JDBCCatalogConstants.Tables.LandsatImages.DATE
*/

}
18 changes: 18 additions & 0 deletions src/main/java/saps/catalog/core/jdbc/JDBCCatalogUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import saps.catalog.core.jdbc.exceptions.JDBCCatalogException;
import saps.common.core.model.SapsImage;
import saps.common.core.model.SapsLandsatImage;
import saps.common.core.model.SapsUser;
import saps.common.core.model.enums.ImageTaskState;

Expand Down Expand Up @@ -63,4 +65,20 @@ public static List<SapsImage> extractSapsTasks(ResultSet rs) throws JDBCCatalogE
}
return imageTasks;
}

public static List<SapsLandsatImage> extractSapsImages(ResultSet rs) throws JDBCCatalogException {
List<SapsLandsatImage> validImages = new ArrayList<>();
while(true) {
try {
if (!rs.next()) break;
validImages.add(
new SapsLandsatImage(
rs.getString(JDBCCatalogConstants.Tables.LandsatImages.REGION),
rs.getDate(JDBCCatalogConstants.Tables.LandsatImages.DATE),
rs.getString(JDBCCatalogConstants.Tables.LandsatImages.DATASET)));
} catch (SQLException e) {
throw new JDBCCatalogException("Error while extract landsat images", e);
}
} return validImages;
}
}
47 changes: 40 additions & 7 deletions src/main/java/saps/catalog/core/retry/CatalogUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import saps.catalog.core.Catalog;
import saps.catalog.core.retry.catalog.AddNewTask;
import saps.catalog.core.retry.catalog.AddNewUser;
import saps.catalog.core.retry.catalog.AddTimestampRetry;
import saps.catalog.core.retry.catalog.CatalogRetry;
import saps.catalog.core.retry.catalog.GetAllTasks;
import saps.catalog.core.retry.catalog.GetLandsatImages;
import saps.catalog.core.retry.catalog.GetProcessedTasks;
import saps.catalog.core.retry.catalog.GetProcessingTasksRetry;
import saps.catalog.core.retry.catalog.GetTaskById;
Expand All @@ -24,6 +27,7 @@
import saps.catalog.core.retry.catalog.GetCountCompletedTasks;

import saps.common.core.model.SapsImage;
import saps.common.core.model.SapsLandsatImage;
import saps.common.core.model.SapsUser;
import saps.common.core.model.enums.ImageTaskState;

Expand Down Expand Up @@ -58,7 +62,7 @@ private static <T> T retry(CatalogRetry<?> function, int sleepInSeconds, String
LOGGER.error("Failed while " + message, e);
}

try {
try {
LOGGER.info("Sleeping for " + sleepInSeconds + " seconds");
Thread.sleep(Long.valueOf(sleepInSeconds) * 1000);
} catch (InterruptedException e) {
Expand Down Expand Up @@ -189,9 +193,19 @@ public static SapsImage addNewTask(
String digestPreprocessing,
String processingPhaseTag,
String digestProcessing,
String message) {
return retry(
new AddNewTask(
String message) {

//how to deal with date value, java date, sql date or string ?
//Collection <SapsLandsatImage> newColl = (validateLandsatImage(imageStore, region, date, dataset, message))
//if newColl.length > 0: then continue, else return (?)

LOGGER.info("===== ENTROU NO ADD NEW TASK");
List<SapsLandsatImage> landsatImagesList = validateLandsatImage(imageStore, region, date, dataset, message);
if (landsatImagesList.size() > 0) {
LOGGER.info("image has been found");

return retry(
new AddNewTask(
imageStore,
taskId,
dataset,
Expand All @@ -205,9 +219,28 @@ public static SapsImage addNewTask(
digestPreprocessing,
processingPhaseTag,
digestProcessing),
CATALOG_DEFAULT_SLEEP_SECONDS,
message);
}
CATALOG_DEFAULT_SLEEP_SECONDS,
message);
}

LOGGER.info("image has NOT been found");
return null;
}

/**
* This function checks if we got a valid image
*
* @param region region specified by the user submission
* @param date date specified by the user submission
* @param landsat landsat who (perhaps) got the image
* @param message
* @return boolean indicating if the image does exist or not
*
*/
private static List<SapsLandsatImage> validateLandsatImage(Catalog imageStore, String region, Date date, String landsat, String message) {
// SQL QUERY, if we got the image return TRUE, else return FALSE
return retry(new GetLandsatImages(imageStore, region, date, landsat), CATALOG_DEFAULT_SLEEP_SECONDS, message);
}

/**
* This function gets a specific task with id.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package saps.catalog.core.retry.catalog;

import java.util.List;
import java.util.Date;
import saps.catalog.core.Catalog;
import saps.common.core.model.SapsLandsatImage;
import org.apache.log4j.Logger;

public class GetLandsatImages implements CatalogRetry<List<SapsLandsatImage>> {

private static final Logger LOGGER = Logger.getLogger(GetLandsatImages.class);

private Catalog imageStore;
private String region;
private Date date;
private String landsat;

public GetLandsatImages(Catalog imageStore, String region, Date date, String landsat) {
this.imageStore = imageStore;
this.region = region;
this.date = date;
this.landsat = landsat;
}

@Override
public List<SapsLandsatImage> run() {
List<SapsLandsatImage> X = imageStore.getLandsatImages(region, date, landsat);
LOGGER.info(X);
return imageStore.getLandsatImages(region, date, landsat);
}
}

0 comments on commit 3b9b096

Please sign in to comment.