1
1
package au.org.ala.volunteer
2
2
3
3
import com.google.common.base.Stopwatch
4
+ import com.google.common.base.Strings
4
5
import grails.converters.JSON
5
6
import grails.gorm.transactions.Transactional
6
7
import org.elasticsearch.action.search.SearchResponse
7
8
import org.elasticsearch.action.search.SearchType
8
9
import org.springframework.dao.DataIntegrityViolationException
9
10
11
+ import java.util.regex.Pattern
12
+
10
13
import static org.springframework.http.HttpStatus.NO_CONTENT
11
14
12
15
class UserController {
@@ -387,7 +390,8 @@ class UserController {
387
390
if (! user) {
388
391
flash. message = message(code : ' default.not.found.message' ,
389
392
args : [message(code : ' user.label' , default : ' User' ), params. id]) as String
390
- redirect(action : " list" )
393
+ redirect(action : " adminList" )
394
+ return
391
395
}
392
396
393
397
def roles = UserRole . findAllByUser(user)
@@ -470,7 +474,7 @@ class UserController {
470
474
else {
471
475
flash. message = message(code : ' default.not.found.message' ,
472
476
args : [message(code : ' user.label' , default : ' User' ), params. id]) as String
473
- redirect(action : " list " )
477
+ redirect(action : " adminList " )
474
478
}
475
479
}
476
480
@@ -488,7 +492,7 @@ class UserController {
488
492
user. delete(flush : true )
489
493
flash. message = message(code : ' default.deleted.message' ,
490
494
args : [message(code : ' user.label' , default : ' User' ), params. id]) as String
491
- redirect(action : " list " )
495
+ redirect(action : " adminList " )
492
496
} catch (DataIntegrityViolationException e) {
493
497
String message = message(code : ' default.not.deleted.message' ,
494
498
args : [message(code : ' user.label' , default : ' User' ), params. id]) as String
@@ -499,7 +503,7 @@ class UserController {
499
503
} else {
500
504
flash. message = message(code : ' default.not.found.message' ,
501
505
args : [message(code : ' user.label' , default : ' User' ), params. id]) as String
502
- redirect(action : " list " )
506
+ redirect(action : " adminList " )
503
507
}
504
508
}
505
509
@@ -618,40 +622,74 @@ class UserController {
618
622
log. debug(" ajaxGetPoints| Transcription.countByFullyTranscribedBy(): ${ sw.toString()} " )
619
623
sw. reset(). start()
620
624
621
- final query = """ {
622
- "constant_score": {
623
- "filter": {
624
- "and": [
625
- { "term": { "transcriptions.fullyTranscribedBy": "${ userInstance.userId} " } },
626
- { "nested" :
627
- {
628
- "path" : "fields",
629
- "filter" : { "term" : { "name": "decimalLongitude"}}
630
- }
631
- },
632
- { "nested" :
633
- {
634
- "path" : "fields",
635
- "filter" : { "term" : { "name": "decimalLongitude"}}
636
- }
625
+ final query = """
626
+ {
627
+ "query": {
628
+ "bool": {
629
+ "must": [
630
+ {
631
+ "term": {
632
+ "transcriptions.fullyTranscribedBy": "${ userInstance.userId} "
633
+ }
634
+ },
635
+ {
636
+ "term": {
637
+ "fields.name": "decimalLongitude"
638
+ }
639
+ },
640
+ {
641
+ "term": {
642
+ "fields.name": "decimalLatitude"
643
+ }
644
+ }
645
+ ]
637
646
}
638
- ]
639
647
}
640
- }
641
648
}"""
642
-
643
- def searchResponse = fullTextIndexService. rawSearch(query, SearchType . QUERY_THEN_FETCH , taskCount. intValue(), fullTextIndexService. rawResponse)
649
+ // Elastic Search max value.
650
+ final int MAX_SEARCH = 10000
651
+ def searchResponse = fullTextIndexService. rawSearch(query, SearchType . QUERY_THEN_FETCH , MAX_SEARCH , fullTextIndexService. rawResponse)
644
652
sw. stop()
645
653
log. debug(" ajaxGetPoints| fullTextIndexService.rawSearch(): ${ sw.toString()} " )
646
654
sw. reset(). start()
647
655
656
+ // Regex for detecting traditional latitude/longitude. We will convert to decimal for Google Maps.
657
+ def regex = Pattern . compile(/ (((\d +)°)?)(((\d +)')?)(((\d +)")?)([NnSsEeWw])/ )
658
+
648
659
def data = searchResponse. hits. hits. collect { hit ->
649
660
def field = hit. source[' fields' ]
650
661
651
662
def pt = field. findAll { value ->
652
663
value[' name' ] == ' decimalLongitude' || value[' name' ] == ' decimalLatitude'
653
664
}. collectEntries { value ->
654
- def dVal = value[' value' ]
665
+ def dVal = value[' value' ] as String
666
+ log. debug(" ajaxGetPoints| dVal: ${ dVal} " )
667
+
668
+ def matcher = regex. matcher(dVal)
669
+ if (matcher. find()) {
670
+ log. debug(" ajaxGetPoints| Group count: ${ matcher.groupCount()} " )
671
+ for (int i = 0 ; i <= matcher. groupCount(); i++ ) {
672
+ log. debug(" match[${ i} ]: ${ matcher.group(i)} " )
673
+ }
674
+ try {
675
+ BigDecimal minutes = (getBigDecimalFromString(matcher. group(6 ). toString()) / new BigDecimal (60 ))
676
+ BigDecimal seconds = (getBigDecimalFromString(matcher. group(9 ). toString()) / new BigDecimal (3600 ))
677
+ BigDecimal degrees = getBigDecimalFromString(matcher. group(3 ). toString())
678
+ log. debug(" Conversion: degrees: [${ degrees} ], minutes: [${ minutes} ], seconds: [${ seconds} ]" )
679
+ degrees + = (minutes + seconds)
680
+
681
+ // Check direction and assign negative if necessary
682
+ if (matcher. group(10 ). equalsIgnoreCase(" S" ) ||
683
+ matcher. group(10 ). equalsIgnoreCase(" W" )) {
684
+ degrees = - degrees
685
+ }
686
+
687
+ dVal = degrees. toString()
688
+ log. debug(" dVal: ${ dVal} " )
689
+ } catch (Exception e) {
690
+ log. error(" Error attempting to convert degrees, minutes and seconds to a decimal value ${ dVal} , skipping." , e)
691
+ }
692
+ }
655
693
656
694
if (value[' name' ] == ' decimalLongitude' ) {
657
695
[lng : dVal]
@@ -669,6 +707,19 @@ class UserController {
669
707
render(data as JSON )
670
708
}
671
709
710
+ /**
711
+ * Converts a string value to a BigDecimal, returning 0 if the value isn't parseable.
712
+ * @param input the value to convert
713
+ * @return a BigDecimal or 0 if null/empty.
714
+ */
715
+ def getBigDecimalFromString (String input ) {
716
+ if (! Strings . isNullOrEmpty(input) && ! input. equalsIgnoreCase(" null" )) {
717
+ return new BigDecimal (input. toInteger(). intValue())
718
+ } else {
719
+ return new BigDecimal (0 )
720
+ }
721
+ }
722
+
672
723
def notebookMainFragment () {
673
724
def user = User . get(params. int (" id" ))
674
725
// def simpleTemplateEngine = new SimpleTemplateEngine()
0 commit comments