Skip to content

v0.2.51..v0.2.52 changeset GrailResource.java

Garret Voltz edited this page Jan 15, 2020 · 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 7bbab14..2849e23 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,6 +26,7 @@
  */
 package hoot.services.controllers.grail;
 
+import static hoot.services.HootProperties.CHANGESETS_FOLDER;
 import static hoot.services.HootProperties.GRAIL_OVERPASS_LABEL;
 import static hoot.services.HootProperties.GRAIL_OVERPASS_STATS_QUERY;
 import static hoot.services.HootProperties.GRAIL_RAILS_LABEL;
@@ -76,6 +77,7 @@ import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.ResponseBuilder;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.TransformerException;
 
 import org.apache.commons.io.FileUtils;
@@ -97,6 +99,7 @@ import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
 
 import hoot.services.command.Command;
 import hoot.services.command.ExternalCommand;
@@ -202,7 +205,7 @@ public class GrailResource {
 
         String jobId = "grail_" + UUID.randomUUID().toString().replace("-", "");
 
-        File workDir = new File(TEMP_OUTPUT_PATH, jobId);
+        File workDir = new File(CHANGESETS_FOLDER, jobId);
         try {
             FileUtils.forceMkdir(workDir);
         }
@@ -264,24 +267,23 @@ public class GrailResource {
     }
 
     /**
-     * Retrieve statistics on the specified differential
+     * Retrieve statistics on the specified changeset
      *
-     * GET hoot-services/grail/differentialstats/{jobId}
+     * GET hoot-services/grail/changesetstats/{jobId}
      *
      * @param jobDir
-     *      Internally, this is the directory that the files are kept in. We expect that the directory
-     *      has dest.osm & source.osm files to run the diff with.
+     *      Internally, this is the directory that the files are kept in.
      *
      * @param debugLevel
      *      debug level
      *
-     * @return JSON object containing info about the differential such as
+     * @return JSON object containing info about the changeset such as
      *      the count of new nodes, ways, and relations
      */
     @GET
-    @Path("/differentialstats")
+    @Path("/changesetstats")
     @Produces(MediaType.APPLICATION_JSON)
-    public Response differentialStats(@Context HttpServletRequest request,
+    public Response changesetStats(@Context HttpServletRequest request,
             @QueryParam("jobId") String jobDir,
             @QueryParam("includeTags") Boolean includeTags,
             @QueryParam("DEBUG_LEVEL") @DefaultValue("info") String debugLevel) {
@@ -291,7 +293,7 @@ public class GrailResource {
 
         JSONObject jobInfo = new JSONObject();
 
-        String fileDirectory = TEMP_OUTPUT_PATH + "/" + jobDir;
+        String fileDirectory = CHANGESETS_FOLDER + "/" + jobDir;
         File workDir = new File(fileDirectory);
 
         try {
@@ -306,8 +308,8 @@ public class GrailResource {
 
             for(File currentOsc : oscFilesList) {
                 String xmlData = FileUtils.readFileToString(currentOsc, "UTF-8");
-                Document changesetDiffDoc = XmlDocumentBuilder.parse(xmlData);
-                logger.debug("Parsing changeset diff XML: {}", StringUtils.abbreviate(xmlData, 1000));
+                Document changesetDoc = XmlDocumentBuilder.parse(xmlData);
+                logger.debug("Parsing changeset XML: {}", StringUtils.abbreviate(xmlData, 1000));
 
                 for (DbUtils.EntityChangeType entityChangeType : DbUtils.EntityChangeType.values()) {
                     String changeTypeName = entityChangeType.toString().toLowerCase();
@@ -315,7 +317,7 @@ public class GrailResource {
                     for (DbUtils.nwr_enum elementType : DbUtils.nwr_enum.values()) {
                         String elementTypeName = elementType.toString();
 
-                        NodeList elementXmlNodes = XPathAPI.selectNodeList(changesetDiffDoc,
+                        NodeList elementXmlNodes = XPathAPI.selectNodeList(changesetDoc,
                                 "//osmChange/" + changeTypeName +"/" + elementTypeName);
 
                         String key = changeTypeName + "-" + elementTypeName;
@@ -330,30 +332,40 @@ public class GrailResource {
             File tagDiffFile = new File(workDir, "diff.tags.osc");
             jobInfo.put("hasTags", tagDiffFile.exists());
         }
-        catch (TransformerException e) {
-            throw new RuntimeException("Error invoking XPathAPI!", e);
+        catch (IllegalArgumentException e) {
+            throw new WebApplicationException(e, Response.serverError().entity("Changeset file not found.").build());
         }
-        catch (Exception exc) {
-            String msg = "Error during differential stats! jobid: " + jobDir;
-            throw new WebApplicationException(exc, Response.serverError().entity(msg).build());
+        catch (IOException e) {
+            throw new WebApplicationException(e, Response.serverError().entity("Changeset file not found.").build());
+        }
+        catch (ParserConfigurationException e) {
+            throw new WebApplicationException(e, Response.serverError().entity("Changeset file is malformed.").build());
+        }
+        catch (SAXException e) {
+            throw new WebApplicationException(e, Response.serverError().entity("Changeset file is malformed.").build());
         }
+        catch (TransformerException e) {
+            throw new WebApplicationException(e, Response.serverError().entity("Changeset file is malformed.").build());
+        }
+
 
         return Response.ok(jobInfo.toJSONString()).build();
     }
 
     /**
-     * Pushes the specified differential changeset back to the
+     * Pushes the specified changeset back to the
      * reference dataset API
      *
      * Takes in a json object
-     * POST hoot-services/grail/differentialpush
+     * POST hoot-services/grail/changesetpush
      *
      * {
-     *   //the job id. We make the directory for the differential with the name of the jobid
+     *   //the job id. We make the directory for the changeset with the name of the jobid
      *   "folder" : "grail_8e8c4681-ead9-4c27-aa2e-80746d627523",
      *
      *   //boolean if the user wants to apply the tags when pushing to reference API
      *   "APPLY_TAGS" : true
+     *   //this is only applicable if the changeset is the result of "create differential changeset"
      * }
      *
      * @param reqParams
@@ -362,20 +374,20 @@ public class GrailResource {
      * @param debugLevel
      *      debug level
      *
-     * @return Job ID. Can be used to check status of the conflate push
+     * @return Job ID. Can be used to check status of the changeset push
      */
     @POST
-    @Path("/differentialpush")
+    @Path("/changesetpush")
     @Consumes(MediaType.APPLICATION_JSON)
     @Produces(MediaType.APPLICATION_JSON)
-    public Response differentialPush(@Context HttpServletRequest request,
+    public Response changesetPush(@Context HttpServletRequest request,
             GrailParams reqParams,
             @QueryParam("DEBUG_LEVEL") @DefaultValue("info") String debugLevel) {
 
         Users user = Users.fromRequest(request);
         advancedUserCheck(user);
 
-        GrailParams params = new GrailParams();
+        GrailParams params = new GrailParams(reqParams);
         params.setUser(user);
         params.setPushUrl(RAILSPORT_PUSH_URL);
 
@@ -388,31 +400,31 @@ public class GrailResource {
         JSONObject json = new JSONObject();
         json.put("jobid", jobId);
         String jobDir = reqParams.getParentId();
-        File workDir = new File(TEMP_OUTPUT_PATH, jobDir);
+        File workDir = new File(CHANGESETS_FOLDER, jobDir);
 
         if (!workDir.exists()) {
-            logger.error("ApplyDiff: jobDir {} does not exist.", workDir.getAbsolutePath());
+            logger.error("ApplyChangeset: jobDir {} does not exist.", workDir.getAbsolutePath());
             return Response.status(Response.Status.BAD_REQUEST).entity("Job " + jobDir + " does not exist.").build();
         }
 
         try {
             APICapabilities railsPortCapabilities = getCapabilities(RAILSPORT_CAPABILITIES_URL);
-            logger.info("ApplyDiff: railsPortAPI status = " + railsPortCapabilities.getApiStatus());
+            logger.info("ApplyChangeset: railsPortAPI status = " + railsPortCapabilities.getApiStatus());
             if (railsPortCapabilities.getApiStatus() == null || railsPortCapabilities.getApiStatus().equals("offline")) {
-                return Response.status(Response.Status.SERVICE_UNAVAILABLE).entity("The reference OSM API server is offline. Try again later").build();
+                return Response.status(Response.Status.SERVICE_UNAVAILABLE).entity("The reference OSM API server is offline.").build();
             }
 
-            File geomDiffFile = new File(workDir, "diff.osc");
+            File changesetFile = new File(workDir, "diff.osc");
             List<Command> workflow = new LinkedList<>();
 
-            if (geomDiffFile.exists()) {
-                params.setOutput(geomDiffFile.getAbsolutePath());
+            if (changesetFile.exists()) {
+                params.setOutput(changesetFile.getAbsolutePath());
 
                 ExternalCommand applyGeomChange = grailCommandFactory.build(jobId, params, debugLevel, ApplyChangesetCommand.class, this.getClass());
                 workflow.add(applyGeomChange);
             }
             else {
-                String msg = "Error during differential push! Could not find differential file ";
+                String msg = "Error during changeset push! Could not find osc file ";
                 throw new WebApplicationException(new FileNotFoundException(), Response.serverError().entity(msg).build());
             }
 
@@ -426,7 +438,7 @@ public class GrailResource {
                     workflow.add(applyTagChange);
                 }
                 else {
-                    String msg = "Error during differential push! Could not find differential tags file ";
+                    String msg = "Error during changset push! Could not find osc tags file ";
                     throw new WebApplicationException(new FileNotFoundException(), Response.serverError().entity(msg).build());
                 }
             }
@@ -468,7 +480,7 @@ public class GrailResource {
             throw new WebApplicationException(iae, Response.status(Response.Status.BAD_REQUEST).entity(iae.getMessage()).build());
         }
         catch (Exception e) {
-            String msg = "Error during differential push! Params: " + params;
+            String msg = "Error during changeset push! Params: " + params;
             throw new WebApplicationException(e, Response.serverError().entity(msg).build());
         }
 
@@ -516,7 +528,7 @@ public class GrailResource {
 
         List<Command> workflow = new LinkedList<>();
 
-        File workDir = new File(TEMP_OUTPUT_PATH, mainJobId);
+        File workDir = new File(CHANGESETS_FOLDER, mainJobId);
         try {
             FileUtils.forceMkdir(workDir);
         }
@@ -530,8 +542,14 @@ public class GrailResource {
 
         try {
             params.setInput1(HOOTAPI_DB_URL + "/" + input1);
-            params.setInput2(HOOTAPI_DB_URL + "/" + input2);
-            params.setConflationType(DbUtils.getConflationType(Long.parseLong(input2)));
+
+            //If not passed INPUT2 assume an adds only changeset using one input
+            if(params.getInput2() != null) {
+                params.setInput2(HOOTAPI_DB_URL + "/" + input2);
+                params.setConflationType(DbUtils.getConflationType(Long.parseLong(input2)));
+            } else {
+                params.setInput2("\"\"");
+            }
 
             File changeSet = new File(workDir, "diff.osc");
             if (changeSet.exists()) { changeSet.delete(); }
@@ -547,7 +565,6 @@ public class GrailResource {
             jobStatusTags.put("input2", input2);
             jobStatusTags.put("parentId", reqParams.getParentId());
 
-            // Now roll the dice and run everything.....
             jobProcessor.submitAsync(new Job(mainJobId, user.getId(), workflow.toArray(new Command[workflow.size()]), JobType.DERIVE_CHANGESET, jobStatusTags));
         }
         catch (WebApplicationException wae) {
Clone this wiki locally