Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct coordinate parsing logic for lat & long for several locales #254

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package au.org.ala.collectory
import au.org.ala.collectory.resources.PP
import grails.converters.JSON
import org.springframework.dao.DataIntegrityViolationException
import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.multipart.MultipartFile
import java.text.NumberFormat
import java.text.ParseException
import org.springframework.web.context.request.ServletRequestAttributes

/**
* This is a base class for all provider group entities types.
*
Expand Down Expand Up @@ -303,19 +300,9 @@ abstract class ProviderGroupController {
if (pg) {
if (checkLocking(pg,'/shared/location')) { return }

Locale userLocale = (RequestContextHolder.currentRequestAttributes() as ServletRequestAttributes).request.locale
NumberFormat numberFormat = NumberFormat.getNumberInstance(userLocale)

double latitude
double longitude

try {
latitude = params.latitude ? numberFormat.parse(params.latitude).doubleValue() : -1
longitude = params.longitude ? numberFormat.parse(params.longitude).doubleValue() : -1
} catch (ParseException e) {
latitude = -1
longitude = -1
}
// special handling for lat & long
def latitude = parseCoordinate(params.latitude)
def longitude = parseCoordinate(params.longitude)

// special handling for embedded address - need to create address obj if none exists and we have data
if (!pg.address && [params.address?.street, params.address?.postBox, params.address?.city,
Expand All @@ -324,7 +311,7 @@ abstract class ProviderGroupController {
}

pg.properties = params
pg.latitude = latitude
pg.latitude = latitude
pg.longitude = longitude
pg.userLastModified = collectoryAuthService?.username()

Expand Down Expand Up @@ -927,4 +914,14 @@ abstract class ProviderGroupController {
}
return false
}

private Double parseCoordinate(Object value) {
if (value == null || value.toString().trim().isEmpty()) return -1
try {
return Double.parseDouble(value.toString().replace(",", "."))
} catch (NumberFormatException e) {
return -1
}
}
}