Report.scala 2.51 KB
Newer Older
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
1
2
/*
 * Import Process Delete
3
 * Copyright (C) 2021 Memoriav
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
19
20
package ch.memobase.models

21
22
import org.apache.kafka.clients.consumer.ConsumerRecord

23
24
import java.text.SimpleDateFormat
import java.util.Date
25
26
27
28
29
30
31
32
import scala.util.Try

case class Report(msgKey: String,
                  recordId: String,
                  timestamp: Date,
                  recordSetId: String,
                  institutionId: String,
                  sessionId: String)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
33
34

object Report {
35
  private val dateFormatter = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ss.SSS")
36
  private val shortDateFormatter = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ss")
37
38

  def apply(consumerRecord: ConsumerRecord[String, String]): Try[Report] = Try {
39
    val json = Try(ujson.read(consumerRecord.value())).getOrElse(throw new ParserException("JSON is not valid!"))
40
    val id = Try(json.obj("id").str).getOrElse(throw new ParserException("No `id` field in JSON obj"))
41
    val timestampString = Try(json.obj("timestamp").str).getOrElse(throw new ParserException("No `timestamp` field in JSON obj"))
42
43
44
    val timestamp = Try(dateFormatter.parse(timestampString))
      .orElse(Try(shortDateFormatter.parse(timestampString)))
      .getOrElse(throw new ParserException("No valid timestamp"))
45
    val headers = consumerRecord.headers()
46
47
48
49
50
51
    val recordSet = Try(new String(headers.lastHeader("recordSetId").value()))
      .getOrElse(throw new ParserException("Extraction of `recordSetId` header field failed"))
    val institution = Try(new String(headers.lastHeader("institutionId").value()))
      .getOrElse(throw new ParserException("Extraction of `institutionId` header field failed"))
    val session = Try(new String(headers.lastHeader("sessionId").value()))
      .getOrElse(throw new ParserException("Extraction of `sessionId` header field failed"))
52
    Report(consumerRecord.key, id, timestamp, recordSet, institution, session)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
53
54
55
  }
}