Skip to content

Commit

Permalink
Merge pull request #1575 from AtlasOfLivingAustralia/feature/#1570
Browse files Browse the repository at this point in the history
#1570 check if shape file is uploading correctly
  • Loading branch information
temi authored Nov 22, 2023
2 parents f168fdb + 8127766 commit 0027ede
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import static org.apache.http.HttpStatus.*
class MetadataService {
static DateFormat ISO8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")

def grailsApplication, webService, cacheService, settingService, modelService
def grailsApplication, cacheService, settingService, modelService
WebService webService

def activitiesModel() {
return cacheService.get('activity-model',{
Expand Down Expand Up @@ -247,14 +248,14 @@ class MetadataService {
def facetConfig = webService.getJson(grailsApplication.config.ecodata.service.url + "/metadata/getGeographicFacetConfig")
facetConfig.grouped.each { k, v ->
v.each { name, fid ->
def objects = webService.getJson(grailsApplication.config.spatial.baseURL + '/ws/objects/'+fid)
def objects = webService.getJson(grailsApplication.config.spatial.baseURL + '/ws/objects/'+fid, null, false, true, true)
results[k] << [(objects[0].fieldname):objects[0]] // Using the fieldname instead of the name for grouped facets is a temp workaround for the GER.
}

}

facetConfig.contextual.each { name, fid ->
def objects = webService.getJson(grailsApplication.config.spatial.baseURL + '/ws/objects/'+fid)
def objects = webService.getJson(grailsApplication.config.spatial.baseURL + '/ws/objects/'+fid, null, false, true, true)
objects.each {
results[name] << [(it.name):it]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import org.opengis.feature.simple.SimpleFeature

class SiteService {

def webService, grailsApplication, commonService, metadataService, userService
def grailsApplication, commonService, metadataService, userService
def documentService
ActivityService activityService
ProjectService projectService
LinkGenerator grailsLinkGenerator
ReportService reportService
ProjectActivityService projectActivityService
SiteService siteService

WebService webService
def list() {
webService.getJson(grailsApplication.config.ecodata.service.url + '/site/').list
}
Expand Down Expand Up @@ -162,7 +162,7 @@ class SiteService {
def userId = userService.getUser().userId
def url = "${grailsApplication.config.spatial.layersUrl}/shape/upload/shp?user_id=${userId}&api_key=${grailsApplication.config.api_key}"

return webService.postMultipart(url, [:], shapefile)
return webService.postMultipart(url, [:], shapefile, 'files', true)
}

/**
Expand All @@ -183,7 +183,7 @@ class SiteService {

def url = "${baseUrl}/${shapeFileId}/${siteId}"

def result = webService.doPost(url, site)
def result = webService.doPost(url, site, true)

String error
if (!result?.resp?.id) {
Expand Down Expand Up @@ -245,7 +245,7 @@ class SiteService {
Geometry geom = placemark.getDefaultGeometry()
def site = [name:name, description: description, user_id:userId, api_key:grailsApplication.config.api_key, wkt:geom.toText()]

def result = webService.doPost(url, site)
def result = webService.doPost(url, site, true)
if (!result.error) {
def id = result.resp.id
if (!result.resp.error) {
Expand Down
30 changes: 23 additions & 7 deletions grails-app/services/au/org/ala/biocollect/merit/WebService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,18 @@ class WebService {
tokenService.getAuthToken(false).toAuthorizationHeader()
}

def getJson(String url, Integer timeout = null, boolean includeApiKey = false, boolean includeUserId = true) {
def getJson(String url, Integer timeout = null, boolean includeApiKey = false, boolean includeUserId = true, boolean useToken = false) {
def conn = null
try {
conn = configureConnection(url, includeUserId, timeout)
if (includeApiKey) {
conn.setRequestProperty("Authorization", grailsApplication.config.getProperty("api_key"))
}

if (useToken) {
conn.setRequestProperty("Authorization", getAuthHeader())
}

conn.setRequestProperty(ACCEPT, MediaType.APPLICATION_JSON_VALUE)
def json = responseText(conn)
def result = JSON.parse(json)
Expand Down Expand Up @@ -281,14 +286,20 @@ class WebService {
}
}

def doPost(String url, Map postBody) {
def doPost(String url, Map postBody, boolean useToken = false) {
def conn = null
def charEncoding = 'utf-8'
try {
conn = new URL(url).openConnection()
conn.setDoOutput(true)
conn.setRequestProperty("Content-Type", "application/json;charset=${charEncoding}")
conn.setRequestProperty("Authorization", grailsApplication.config.getProperty("api_key"))

if (useToken) {
conn.setRequestProperty("Authorization", getAuthHeader())
} else {
conn.setRequestProperty("Authorization", grailsApplication.config.getProperty("api_key"))
}

addHubUrlPath(conn)

def user = getUserService().getUser()
Expand Down Expand Up @@ -434,9 +445,9 @@ class WebService {
* @param file the Multipart file object to forward.
* @return [status:<request status>, content:<The response content from the server, assumed to be JSON>
*/
def postMultipart(url, Map params, MultipartFile file, fileParam = 'files') {
def postMultipart(url, Map params, MultipartFile file, fileParam = 'files', boolean useToken = false) {

postMultipart(url, params, file.inputStream, file.contentType, file.originalFilename, fileParam)
postMultipart(url, params, file.inputStream, file.contentType, file.originalFilename, fileParam, useToken)
}

/**
Expand All @@ -449,7 +460,7 @@ class WebService {
* @param fileParamName the name of the HTTP parameter that will be used for the post.
* @return [status:<request status>, content:<The response content from the server, assumed to be JSON>
*/
def postMultipart(url, Map params, InputStream contentIn, contentType, originalFilename, fileParamName = 'files') {
def postMultipart(url, Map params, InputStream contentIn, contentType, originalFilename, fileParamName = 'files', boolean useToken = false) {

def result = [:]
def user = userService.getUser()
Expand All @@ -466,7 +477,12 @@ class WebService {
}

addHubUrlPath(headers)
headers."Authorization" = grailsApplication.config.getProperty("api_key")
if (useToken) {
headers."Authorization" = getAuthHeader()
} else {
headers."Authorization" = grailsApplication.config.getProperty("api_key")
}

if (user) {
headers[grailsApplication.config.app.http.header.userId] = user.userId
}
Expand Down

0 comments on commit 0027ede

Please sign in to comment.