Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- removed spatial portal from shape file upload
  • Loading branch information
temi committed Oct 24, 2024
1 parent dfca356 commit 39d567a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ class SiteController {
try {
while (!progress.cancelling && progress.uploaded < progress.total) {
Map site = siteData.sites[progress.uploaded]
Map result = siteService.createSiteFromUploadedShapefile(siteData.shapeFileId, site.id, asString(site.externalId), asString(site.name), asString(site.description, 'No description supplied'), siteData.projectId, false)
Map result = siteService.createSiteFromUploadedShapefile(siteData.shapeFileId, site.id, asString(site.externalId), asString(site.name), asString(site.description, 'No description supplied'), siteData.projectId)
if (!result.success) {
progress.errors << [error: result.error, detail: result.detail]
}
Expand Down
2 changes: 1 addition & 1 deletion grails-app/services/au/org/ala/merit/ImportService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ class ImportService {
sites << name
}
else {
def resp = siteService.createSiteFromUploadedShapefile(shapeFileId, shape.id, siteExternalId, name, description, project.projectId, false)
def resp = siteService.createSiteFromUploadedShapefile(shapeFileId, shape.id, siteExternalId, name, description, project.projectId)
if (resp?.siteId) {
projectDetails.sites << [siteId:resp.siteId, name:name, description:description]
sites << name
Expand Down
26 changes: 6 additions & 20 deletions grails-app/services/au/org/ala/merit/SiteService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -231,27 +231,16 @@ class SiteService {
* @param description the description for the site
* @param projectId the project the site should be associated with.
*/
Map createSiteFromUploadedShapefile(String shapeFileId, String featureId, String externalId, String name, String description, String projectId, boolean persistInSpatialPortal = true) {
Map createSiteFromUploadedShapefile(String shapeFileId, String featureId, String externalId, String name, String description, String projectId) {

Map returnValue
Map result = spatialService.createObjectFromShapefileFeature(shapeFileId, featureId, name, description)
Map result = spatialService.createObjectFromShapefileFeature(shapeFileId, featureId)

if (!result.error && !result.resp.error) {
String pid = result.resp.id
String source = SITE_SOURCE_SPATIAL_PORTAL

Map geomResult = spatialService.objectGeometry(pid)
if (geomResult.statusCode == HttpStatus.SC_OK) {
// We use the spatial portal for it's shapefile handling and re-projection to WGS84 but don't want the
// site to remain.
if (!persistInSpatialPortal && pid) {
Integer resp = spatialService.deleteFromSpatialPortal(pid)
if (resp == HttpStatus.SC_OK) {
pid = null
source = SITE_SOURCE_DRAWN
}
}
result = createSite(projectId, name, description, externalId, source, geomResult.resp, pid)
String source = SITE_SOURCE_DRAWN
Map geoJson = result.resp.geoJson
if (geoJson) {
result = createSite(projectId, name, description, externalId, source, geoJson)
if (result.statusCode == HttpStatus.SC_OK && result.resp?.siteId) {
returnValue = [success:true, siteId:result.resp.siteId]
}
Expand All @@ -262,9 +251,6 @@ class SiteService {
else {
returnValue = [success:false, error:"Failed to retrieve geometry for feature $featureId of shapefile $shapeFileId"]
}
if (returnValue.success == false && pid) {
spatialService.deleteFromSpatialPortal(pid)
}
}
else {
String detailedError = result.error ?: result.resp.error
Expand Down
17 changes: 6 additions & 11 deletions grails-app/services/au/org/ala/merit/SpatialService.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package au.org.ala.merit

import static java.net.URLEncoder.encode
import grails.core.GrailsApplication
import org.springframework.web.multipart.MultipartFile

Expand Down Expand Up @@ -41,8 +42,7 @@ class SpatialService {
* attributes of that feature. eg. [shp_id: <shapefileId>, "0":[attribute1:<value>, attribute2:<value>, etc], "1":[attribute1:<value>, attribute2:<value>, etc]]]
*/
Map uploadShapefile(MultipartFile shapefile) {
String url = "${grailsApplication.config.getProperty('spatial.layersUrl')}${UPLOAD_SHAPE_PATH}"

String url = "${grailsApplication.config.getProperty('ecodata.baseUrl')}/shapefile"
webService.postMultipart(url, [:], shapefile, 'files', true)
}

Expand All @@ -57,15 +57,10 @@ class SpatialService {
* @return [statusCode:<HTTP status code returned from the call>, resp:[id:<id of new object in spatial portal>], error:<if there was an error creating the object>]
* e.g [statusCode:200, resp:[id:12345]] or [statusCode:500, error:"Failed to create object"]
*/
Map createObjectFromShapefileFeature(String shapeFileId, String featureId, String objectName, String objectDescription) {
String baseUrl = "${grailsApplication.config.getProperty('spatial.layersUrl')}${UPLOAD_SHAPE_PATH}"
String userId = userService.getCurrentUserId()

Map site = [name:objectName, description: objectDescription, user_id:userId]

String url = "${baseUrl}/${shapeFileId}/${featureId}"

webService.doPost(url, site, true)
Map createObjectFromShapefileFeature(String shapeFileId, String featureId) {
String baseUrl = "${grailsApplication.config.getProperty('ecodata.baseUrl')}/shapefile/geojson"
String url = "${baseUrl}/${encode(shapeFileId, "UTF-8")}/${encode(featureId, "UTF-8")}"
webService.getJson2(url)
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/test/groovy/au/org/ala/merit/ImportServiceSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class ImportServiceSpec extends Specification implements ServiceUnitTest<ImportS
1 * siteService.uploadShapefile(shapefile) >> [statusCode: HttpStatus.SC_OK, resp:[shp_id:shapefileId, "0":["GRANT_ID":"g1", "EXTERNAL_I":"e1"]]]
1 * projectService.search([grantId:"g1", externalId:"e1"]) >> [resp:[projects:[[projectId:"p1", grantId: "g1"]]]]
1 * projectService.get('p1', _) >> [name:'p1 name', description:'p1 description']
1 * siteService.createSiteFromUploadedShapefile(shapefileId, "0", shapefileId+'-0', "g1 - Site 1", _, "p1", false) >> [success:true, siteId:'s1']
1 * siteService.createSiteFromUploadedShapefile(shapefileId, "0", shapefileId+'-0', "g1 - Site 1", _, "p1") >> [success:true, siteId:'s1']
result.success == true
result.message.sites.size() == 1
}
Expand All @@ -77,7 +77,7 @@ class ImportServiceSpec extends Specification implements ServiceUnitTest<ImportS
1 * projectService.search([grantId:"g1", externalId:"e1"]) >> [resp:[projects:[]]]
1 * projectService.search([grantId:"g1"]) >> [resp:[projects:[[projectId:"p1", grantId: "g1"]]]]
1 * projectService.get('p1', _) >> [name:'p1 name', description:'p1 description']
1 * siteService.createSiteFromUploadedShapefile(shapefileId, "0", shapefileId+'-0', "g1 - Site 1", _, "p1", false) >> [success:true, siteId:'s1']
1 * siteService.createSiteFromUploadedShapefile(shapefileId, "0", shapefileId+'-0', "g1 - Site 1", _, "p1") >> [success:true, siteId:'s1']
result.success == true
result.message.sites.size() == 1

Expand All @@ -89,7 +89,7 @@ class ImportServiceSpec extends Specification implements ServiceUnitTest<ImportS
1 * projectService.search([grantId:"g1", externalId:"e1"]) >> [resp:[projects:[]]]
1 * projectService.search([grantId:"g1"]) >> [resp:[projects:[[projectId:"p1", grantId: "g1"], [projectId:'p2', grantId:'g1']]]]
0 * projectService.get('p1', _) >> [name:'p1 name', description:'p1 description']
0 * siteService.createSiteFromUploadedShapefile(shapefileId, "0", shapefileId+'-0', "g1 - Site 1", _, "p1", false) >> [success:true, siteId:'s1']
0 * siteService.createSiteFromUploadedShapefile(shapefileId, "0", shapefileId+'-0', "g1 - Site 1", _, "p1") >> [success:true, siteId:'s1']
result.success == true
result.message.sites.size() == 0
result.message.errors.size() == 1
Expand All @@ -107,7 +107,7 @@ class ImportServiceSpec extends Specification implements ServiceUnitTest<ImportS
1 * siteService.uploadShapefile(shapefile) >> [statusCode: HttpStatus.SC_OK, resp:[shp_id:shapefileId, "0":["GRANT_ID":"g1", "EXTERNAL_I":"e1"]]]
1 * projectService.search([grantId:"g1", externalId:"e1"]) >> [resp:[projects:[[projectId:"p1", grantId: "g1"]]]]
1 * projectService.get('p1', _) >> [name:'p1 name', description:'p1 description']
0 * siteService.createSiteFromUploadedShapefile(shapefileId, "0", shapefileId+'-0', "g1 - Site 1", _, "p1", false) >> [success:true, siteId:'s1']
0 * siteService.createSiteFromUploadedShapefile(shapefileId, "0", shapefileId+'-0', "g1 - Site 1", _, "p1") >> [success:true, siteId:'s1']
result.success == true
result.message.sites.size() == 1
}
Expand Down
36 changes: 9 additions & 27 deletions src/test/groovy/au/org/ala/merit/SiteServiceSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package au.org.ala.merit

import grails.testing.spring.AutowiredTest
import org.apache.http.HttpStatus
import grails.core.GrailsApplication
import spock.lang.Specification

class SiteServiceSpec extends Specification implements AutowiredTest{
Expand Down Expand Up @@ -33,17 +32,14 @@ class SiteServiceSpec extends Specification implements AutowiredTest{
String description = "description"
String projectId = "projectId"
Map geojson = [type:'Point', coordinates:[-35, 100]]
int id = 1234

when:
Map result = service.createSiteFromUploadedShapefile(shapefileId, featureId, externalId, name, description, projectId)

then: "the object representing the site is created in the spatial portal"
1 * spatialService.createObjectFromShapefileFeature(shapefileId, featureId, name, description) >> [statusCode: HttpStatus.SC_OK, resp:[id:id]]
and: "the geometry is retrieved from the spatial portal for storage with the site"
1 * spatialService.objectGeometry(Integer.toString(id)) >> [statusCode: HttpStatus.SC_OK, resp:geojson]
1 * spatialService.createObjectFromShapefileFeature(shapefileId, featureId) >> [statusCode: HttpStatus.SC_OK, resp:[geoJson: geojson]]
and: "the site is created in ecodata"
1 * webService.doPost("site/", [extent:[source:'pid', pid:Integer.toString(id), geometry:[type:'Point', coordinates:[-35, 100], pid:Integer.toString(id)]], projects:[projectId], name:name, description:description, type:'worksArea', externalId:externalId, visibility:'private']) >> [statusCode: HttpStatus.SC_OK, resp:[siteId:'site12']]
1 * webService.doPost("site/", [extent:[source:'drawn', geometry:[type:'Point', coordinates:[-35, 100]]], projects:[projectId], name:name, description:description, type:'worksArea', externalId:externalId, visibility:'private']) >> [statusCode: HttpStatus.SC_OK, resp:[siteId:'site12']]
and: "the return value contains the siteId of the new site"
result.success == true
result.siteId == 'site12'
Expand All @@ -58,17 +54,12 @@ class SiteServiceSpec extends Specification implements AutowiredTest{
String description = "description"
String projectId = "projectId"
Map geojson = [type:'Point', coordinates:[-35, 100]]
int id = 1234

when:
Map result = service.createSiteFromUploadedShapefile(shapefileId, featureId, externalId, name, description, projectId, false)
Map result = service.createSiteFromUploadedShapefile(shapefileId, featureId, externalId, name, description, projectId)

then: "the object representing the site is created in the spatial portal"
1 * spatialService.createObjectFromShapefileFeature(shapefileId, featureId, name, description) >> [statusCode: HttpStatus.SC_OK, resp:[id:id]]
and: "the geometry is retrieved from the spatial portal for storage with the site"
1 * spatialService.objectGeometry(Integer.toString(id)) >> [statusCode: HttpStatus.SC_OK, resp:geojson]
and: "the feature is removed from the spatial portal after the geometry is retrieved"
1 * spatialService.deleteFromSpatialPortal(Integer.toString(id)) >> HttpStatus.SC_OK
1 * spatialService.createObjectFromShapefileFeature(shapefileId, featureId) >> [statusCode: HttpStatus.SC_OK, resp:[geoJson:geojson]]
and: "the site is created in ecodata as if it was a drawn shape"
1 * webService.doPost("site/", [extent:[source:'drawn', geometry:[type:'Point', coordinates:[-35, 100]]], projects:[projectId], name:name, description:description, type:'worksArea', externalId:externalId, visibility:'private']) >> [statusCode: HttpStatus.SC_OK, resp:[siteId:'site12']]
and: "the return value contains the siteId of the new site"
Expand All @@ -84,17 +75,13 @@ class SiteServiceSpec extends Specification implements AutowiredTest{
String name = "name"
String description = "description"
String projectId = "projectId"
int id = 1234
Map geojson = [type:'Point', coordinates:[-35, 100]]

when:
Map result = service.createSiteFromUploadedShapefile(shapefileId, featureId, externalId, name, description, projectId, false)
Map result = service.createSiteFromUploadedShapefile(shapefileId, featureId, externalId, name, description, projectId)

then: "the object representing the site is created in the spatial portal"
1 * spatialService.createObjectFromShapefileFeature(shapefileId, featureId, name, description) >> [statusCode: HttpStatus.SC_OK, resp:[id:id]]
and: "an attempt is made to retrieve the site geometry, which fails in this case"
1 * spatialService.objectGeometry(Integer.toString(id)) >> [statusCode: HttpStatus.SC_INTERNAL_SERVER_ERROR, error:"Failed to retrieve geojson"]
and: "the feature is removed from the spatial portal as it doesn't have valid geometry"
1 * spatialService.deleteFromSpatialPortal(Integer.toString(id)) >> HttpStatus.SC_OK
then: "the object representing the site is invalid"
1 * spatialService.createObjectFromShapefileFeature(shapefileId, featureId) >> [statusCode: HttpStatus.SC_BAD_REQUEST, resp:[error: "Invalid geometry"]]
and: "no site is created in ecodata "
0 * webService.doPost("site/", _)
and: "the return value indicates failure"
Expand All @@ -112,19 +99,14 @@ class SiteServiceSpec extends Specification implements AutowiredTest{
String description = "description"
String projectId = "projectId"
Map geojson = [type:'Point', coordinates:[-35, 100]]
int id = 1234

when:
Map result = service.createSiteFromUploadedShapefile(shapefileId, featureId, externalId, name, description, projectId)

then: "the object representing the site is created in the spatial portal"
1 * spatialService.createObjectFromShapefileFeature(shapefileId, featureId, name, description) >> [statusCode: HttpStatus.SC_OK, resp:[id:id]]
and: "the geometry is retrieved from the spatial portal for storage with the site"
1 * spatialService.objectGeometry(Integer.toString(id)) >> [statusCode: HttpStatus.SC_OK, resp:geojson]
1 * spatialService.createObjectFromShapefileFeature(shapefileId, featureId) >> [statusCode: HttpStatus.SC_OK, resp:[geoJson: geojson]]
and: "the site is attempted to be created in ecodata, which fails for this test case"
1 * webService.doPost("site/", _) >> [statusCode: HttpStatus.SC_INTERNAL_SERVER_ERROR, error:"Failed to create site"]
and: "the feature is removed from the spatial portal to keep consistency with ecodata"
1 * spatialService.deleteFromSpatialPortal(Integer.toString(id)) >> HttpStatus.SC_OK

and: "the return value indicates failure"
result.success == false
Expand Down

0 comments on commit 39d567a

Please sign in to comment.