@@ -28,6 +28,7 @@ import com.yacgroup.yacguide.utils.ParserUtils
28
28
import com.yacgroup.yacguide.utils.DateUtils
29
29
import org.json.JSONArray
30
30
import org.json.JSONException
31
+ import org.json.JSONObject
31
32
import java.lang.RuntimeException
32
33
import java.util.*
33
34
import kotlin.jvm.Throws
@@ -137,7 +138,7 @@ class SectorParser(private val _db: DatabaseWrapper) : JSONWebParser() {
137
138
for (i in 0 until jsonComments.length()) {
138
139
val jsonComment = jsonComments.getJSONObject(i)
139
140
val user = jsonComment.getString(" username" )
140
- val date = _parseCommentDate (jsonComment.getString( " datum " ) )
141
+ val date = _getCommentDate (jsonComment)
141
142
val comment = RegionComment (
142
143
id = ParserUtils .jsonField2Int(jsonComment, " komment_ID" ),
143
144
qualityId = ParserUtils .jsonField2Int(jsonComment, " qual" ),
@@ -236,7 +237,7 @@ class SectorParser(private val _db: DatabaseWrapper) : JSONWebParser() {
236
237
val rockId = ParserUtils .jsonField2Int(jsonComment, " gipfelid" )
237
238
val sectorId = ParserUtils .jsonField2Int(jsonComment, " sektorid" )
238
239
val user = jsonComment.getString(" username" )
239
- val date = _parseCommentDate (jsonComment.getString( " datum " ) )
240
+ val date = _getCommentDate (jsonComment)
240
241
when {
241
242
routeId != 0 -> {
242
243
val comment = RouteComment (
@@ -285,8 +286,24 @@ class SectorParser(private val _db: DatabaseWrapper) : JSONWebParser() {
285
286
}
286
287
287
288
/* *
288
- * Parse comment date-times in the format yyyy-MM-dd HH:mm:ss
289
- * and return only the date in the format yyyy-MM-dd.
289
+ * Return the change date from the given JSON object.
290
+ *
291
+ * If the change date is empty, try the creation date.
290
292
*/
291
- private fun _parseCommentDate (date : String ): String = DateUtils .formatDate(date.split(" " )[0 ])
293
+ private fun _getCommentDate (jsonObject : JSONObject ): String {
294
+ val changeDate = jsonObject.getString(" adatum" )
295
+ val creationDate = jsonObject.getString(" datum" )
296
+ val date = if (changeDate.isNotBlank()) {
297
+ changeDate
298
+ } else if (creationDate.isNotBlank()) {
299
+ creationDate
300
+ } else {
301
+ " "
302
+ }
303
+ // The date is expected in the format yyyy-MM-dd HH:mm:ss.
304
+ // Return only the date in the format yyyy-MM-dd.
305
+ date.split(" " ).let {
306
+ return if (it.isNotEmpty()) DateUtils .formatDate(it[0 ]) else " "
307
+ }
308
+ }
292
309
}
0 commit comments