Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1.  LocalDateTime getApplicationStatusesLastChangeDate(int appId, int statusCode, final boolean throwError = true){
  2.         // build url
  3.         String strStatusCode = statusCode as String //"["+statusCodes.join(",")+"]"
  4.         String strUrl = appStatusesChangesUrl.replaceAll('%appId', appId as String).replaceAll('%statusCode', strStatusCode)
  5.         log.debug('Got strUrl: {}', strUrl)
  6.         String jsonStr = new URL(/*cashupCrmUrl*/ strUrl).getText(
  7.             //connectionTimeout: 5000,
  8.             requestProperties: ['Connection': 'close', 'Cookie': cookies, 'Content-Type': 'application/json', 'Accept': '*/*'] // put here auth headers
  9.         )
  10.         log.debug('Got response: {}', jsonStr)
  11.  
  12.         if (!jsonStr || jsonStr == '[]'){
  13.             // empty result
  14.             if (throwError) {
  15.                 throw new InternalServerErrorException("Got empty status change history for app ${appId} by status id ${strStatusCode}. Most likely database data consistency error")
  16.             } else {
  17.                 null
  18.             }
  19.         }
  20.  
  21.         def appsJson = new JsonSlurper().parseText(jsonStr)
  22.         // appsJson is a list (may be empty) of apps entities
  23.         //TODO: analyze business result?
  24.  
  25.         /* wil get result in form of list of dicts:
  26.         [
  27.   {
  28.     "id": 421,
  29.     "created": "2017-02-28T15:20:43.605382Z",
  30.     "old_application_status_id": 17,
  31.     "old_application_status_name": "InGracefulPeriod",
  32.     "new_application_status_id": 15,
  33.     "new_application_status_name": "Closed"
  34.   }
  35. ]
  36.         */
  37.  
  38.  
  39.         def lastUpdatedDate = appsJson.findAll {
  40.             (it.new_application_status_id as String) == strStatusCode
  41.         }.max {
  42.             //DatatypeConverter.parseDateTime(it.created)
  43.             //LocalDateTime.parse(it.created)
  44.             LocalDateTime.ofInstant(DatatypeConverter.parseDateTime(it.created).getTime().toInstant(), ZoneId.systemDefault())
  45.         }.created
  46.  
  47.         if (lastUpdatedDate){
  48.             //lastUpdatedDate = DatatypeConverter.parseDateTime(lastUpdatedDate).getTime()
  49.             lastUpdatedDate = LocalDateTime.ofInstant(DatatypeConverter.parseDateTime(lastUpdatedDate).getTime().toInstant(), ZoneId.systemDefault())  //LocalDateTime.parse(lastUpdatedDate)
  50.         } else {
  51.             throw new IllegalStateException("Could not get lastUpdateDate (got null/empty value)")
  52.         }
  53. //        .collect {
  54. //            DatatypeConverter.parseDateTime(it.change_date)
  55. //        }.first().getTime()
  56.  
  57. //        def minCredStartDate = report.Contracts.Contract.findAll { con ->
  58. //            con.Roles.Role.count { r -> r.ImportCode == '1' } > 0
  59. //        }.min { con ->
  60. //            Date.parse(DTFM, con.CreditStartDate.toString())
  61. //        }.collect { con ->
  62. //            Date.parse(DTFM, con.CreditStartDate.toString())
  63. //        }.first()
  64.  
  65.  
  66.  
  67.         def ret = lastUpdatedDate // appsJson //.max {} //.apps //.collect { it.id }
  68.         log.debug("Will return {}", ret)
  69.  
  70.         ret //[2,3,5,6,7]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement