Skip to content

v0.2.48..v0.2.49 changeset GrailResource.java

Garret Voltz edited this page Oct 2, 2019 · 1 revision
diff --git a/hoot-services/src/main/java/hoot/services/controllers/grail/GrailResource.java b/hoot-services/src/main/java/hoot/services/controllers/grail/GrailResource.java
index 8e0b256..478753c 100644
--- a/hoot-services/src/main/java/hoot/services/controllers/grail/GrailResource.java
+++ b/hoot-services/src/main/java/hoot/services/controllers/grail/GrailResource.java
@@ -26,11 +26,13 @@
  */
 package hoot.services.controllers.grail;
 
-import static hoot.services.HootProperties.MAX_OVERPASS_FEATURE_COUNT;
 import static hoot.services.HootProperties.GRAIL_OVERPASS_QUERY;
 import static hoot.services.HootProperties.GRAIL_OVERPASS_STATS_QUERY;
+import static hoot.services.HootProperties.GRAIL_OVERPASS_CODENAME;
+import static hoot.services.HootProperties.GRAIL_RAILS_CODENAME;
 import static hoot.services.HootProperties.HOME_FOLDER;
 import static hoot.services.HootProperties.HOOTAPI_DB_URL;
+import static hoot.services.HootProperties.MAX_OVERPASS_FEATURE_COUNT;
 import static hoot.services.HootProperties.PUBLIC_OVERPASS_URL;
 import static hoot.services.HootProperties.RAILSPORT_CAPABILITIES_URL;
 import static hoot.services.HootProperties.RAILSPORT_PULL_URL;
@@ -52,6 +54,7 @@ import java.util.UUID;
 
 import javax.servlet.UnavailableException;
 import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.BadRequestException;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DefaultValue;
 import javax.ws.rs.ForbiddenException;
@@ -563,23 +566,25 @@ public class GrailResource {
     @Path("/pulloverpasstodb")
     @Produces(MediaType.APPLICATION_JSON)
     public Response pullOverpassToDb(@Context HttpServletRequest request,
-            @QueryParam("bbox") String bbox) {
+            @QueryParam("bbox") String bbox,
+            @QueryParam("name") String layerName) {
 
         Users user = Users.fromRequest(request);
         advancedUserCheck(user);
 
         String jobId = UUID.randomUUID().toString().replace("-", "");
-        String mapSuffix = jobId.substring(0, 7);
-        String mapName = SECONDARY + "_" + mapSuffix;
         String folderName = "grail_" + bbox.replace(",", "_");
 
+        if (DbUtils.mapExists(layerName)) {
+            throw new BadRequestException("Record with name : " + layerName + " already exists.  Please try a different name.");
+        }
+
         Response response;
         JSONObject json = new JSONObject();
         json.put("jobid", jobId);
 
         List<Command> workflow = new LinkedList<>();
 
-
         // Create the folder if it doesn't exist
         Long folderId = DbUtils.createFolder(folderName, 0L, user.getId(), false);
 
@@ -587,30 +592,20 @@ public class GrailResource {
         GrailParams params = new GrailParams();
         params.setUser(user);
 
-        // Get grail overpass query from the file and store it in a string
-        String overpassQuery;
-        File overpassQueryFile = new File(HOME_FOLDER, GRAIL_OVERPASS_QUERY);
+        String url;
         try {
-            overpassQuery = FileUtils.readFileToString(overpassQueryFile, "UTF-8");
-        } catch(Exception exc) {
-            logger.error("Grail pull overpass error. Couldn't read overpass query file: " + overpassQueryFile.getAbsolutePath());
-            return Response.status(Response.Status.BAD_REQUEST).entity("Could not find grail overpass query file").build();
+            url = "'" + PullOverpassCommand.getOverpassUrl(bbox) + "'";
+        } catch(IllegalArgumentException exc) {
+            return Response.status(Response.Status.BAD_REQUEST).entity(exc.getMessage()).build();
         }
 
-        //replace the {{bbox}} from the overpass query with the actual coordinates and encode the query
-        overpassQuery = overpassQuery.replace("{{bbox}}", new BoundingBox(bbox).toOverpassString());
-        try {
-            overpassQuery = URLEncoder.encode(overpassQuery, "UTF-8").replace("+", "%20");
-        } catch (UnsupportedEncodingException ignored) {} // Can be safely ignored because UTF-8 is always supported
-
-        String url = "'" + PUBLIC_OVERPASS_URL + "/api/interpreter?data=" + overpassQuery + "'";
         params.setInput1(url);
-        params.setOutput(mapName);
+        params.setOutput(layerName);
         ExternalCommand importOverpass = grailCommandFactory.build(jobId, params, "info", PushToDbCommand.class, this.getClass());
         workflow.add(importOverpass);
 
         // Move the data to the folder
-        InternalCommand setFolder = updateParentCommandFactory.build(jobId, folderId, mapName, user, this.getClass());
+        InternalCommand setFolder = updateParentCommandFactory.build(jobId, folderId, layerName, user, this.getClass());
         workflow.add(setFolder);
 
         jobProcessor.submitAsync(new Job(jobId, user.getId(), workflow.toArray(new Command[workflow.size()]), JobType.IMPORT));
@@ -622,10 +617,11 @@ public class GrailResource {
     }
 
     @GET
-    @Path("/overpassStatsQuery")
+    @Path("/grailMetadataQuery")
     @Produces(MediaType.APPLICATION_JSON)
-    public Response overpassStats(@Context HttpServletRequest request,
+    public Response grailMetadata(@Context HttpServletRequest request,
             @QueryParam("bbox") String bbox) {
+
         Users user = Users.fromRequest(request);
         advancedUserCheck(user);
 
@@ -643,9 +639,16 @@ public class GrailResource {
         overpassQuery = overpassQuery.replace("{{bbox}}", new BoundingBox(bbox).toOverpassString());
         String url = replaceSensitiveData(PUBLIC_OVERPASS_URL) + "/api/interpreter?data=" + overpassQuery;
 
+        // append first 7 digits of a uuid to the rails and overpass codenames
+        String maxSuffix = UUID.randomUUID().toString().replace("-", "").substring(0, 7);
+        String railsCodename = GRAIL_RAILS_CODENAME + "_" + maxSuffix;
+        String overpassCodename = GRAIL_OVERPASS_CODENAME + "_" + maxSuffix;
+
         JSONObject jobInfo = new JSONObject();
         jobInfo.put("overpassQuery", url);
         jobInfo.put("maxFeatureCount", MAX_OVERPASS_FEATURE_COUNT);
+        jobInfo.put("railsCodename", railsCodename);
+        jobInfo.put("overpassCodename", overpassCodename);
 
         return Response.ok(jobInfo.toJSONString()).build();
     }
@@ -672,17 +675,20 @@ public class GrailResource {
     @Path("/pullrailsporttodb")
     @Produces(MediaType.APPLICATION_JSON)
     public Response pullRailsPortToDb(@Context HttpServletRequest request,
-            @QueryParam("bbox") String bbox) {
+            @QueryParam("bbox") String bbox,
+            @QueryParam("name") String layerName) {
 
         Users user = Users.fromRequest(request);
         advancedUserCheck(user);
 
         String jobId = UUID.randomUUID().toString().replace("-", "");
         File workDir = new File(TEMP_OUTPUT_PATH, "grail_" + jobId);
-        String mapSuffix = jobId.substring(0, 7);
-        String mapName = REFERENCE + "_" + mapSuffix;
         String folderName = "grail_" + bbox.replace(",", "_");
 
+        if (DbUtils.mapExists(layerName)) {
+            throw new BadRequestException("Record with name : " + layerName + " already exists.  Please try a different name.");
+        }
+
         Response response;
         JSONObject json = new JSONObject();
         json.put("jobid", jobId);
@@ -690,7 +696,7 @@ public class GrailResource {
         GrailParams params = new GrailParams();
         params.setUser(user);
         params.setWorkDir(workDir);
-        params.setOutput(mapName);
+        params.setOutput(layerName);
         params.setBounds(bbox);
         params.setParentId(folderName);
 
Clone this wiki locally