MsgFilter.scala 2.86 KB
Newer Older
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
1
2
/*
 * Import Process Delete
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
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
21
package ch.memobase

import ch.memobase.models.Report
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
22
import org.apache.logging.log4j.scala.Logging
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
23

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
24
25
26
import java.text.SimpleDateFormat
import java.util.Calendar

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
27
trait MsgFilter {
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
28
  self: Logging =>
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
29
30
31

  private val dateFormatter = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ss.SSS")

32
  type FilterFun = Report => Boolean
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
33

34
35
36
37
38
  def buildFilters(createdAfter: Calendar,
                   createdBefore: Calendar,
                   institutions: Seq[String],
                   recordSets: Seq[String],
                   records: Seq[String],
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
39
40
                   sessions: Seq[String]): Seq[FilterFun] = {
    logger.info("Build filters")
41
    Seq(buildCreatedAfterFilter(standardiseTimestamp(createdAfter))) ++
42
43
44
45
46
      Seq(buildCreatedBeforeFilter(standardiseTimestamp(createdBefore))) ++
      institutions.map(v => buildInstitutionIdFilter(v)) ++
      records.map(v => buildRecordIdFilter(v)) ++
      recordSets.map(v => buildRecordSetIdFilter(v)) ++
      sessions.map(v => buildSessionIdFilter(v))
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
47
  }
48
49
50
51
52
53
54
55
56
57
58
59


  private def standardiseTimestamp(calendar: Calendar): String =
    f"${calendar.get(Calendar.YEAR)}%04d-" +
      f"${calendar.get(Calendar.MONTH)}%02d-" +
      f"${calendar.get(Calendar.DAY_OF_MONTH)}%02dT" +
      f"${calendar.get(Calendar.HOUR_OF_DAY)}%02d:" +
      f"${calendar.get(Calendar.MINUTE)}%02d:" +
      f"${calendar.get(Calendar.SECOND)}%02d." +
      f"${calendar.get(Calendar.MILLISECOND)}%03d"

  private val buildSessionIdFilter: String => FilterFun =
60
    sessionId => report => report.sessionId == sessionId
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
61

62
  private val buildRecordSetIdFilter: String => FilterFun =
63
    recordSetId => report => report.recordSetId == recordSetId
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
64

65
  private val buildInstitutionIdFilter: String => FilterFun =
66
    institutionId => report => report.institutionId == institutionId
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
67

68
  private val buildRecordIdFilter: String => FilterFun =
69
    recordId => report => report.recordId == recordId
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
70

71
  private val buildCreatedAfterFilter: String => FilterFun =
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
72
    timestamp => report => dateFormatter.parse(timestamp).before(report.timestamp)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
73

74
  private val buildCreatedBeforeFilter: String => FilterFun =
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
75
    timestamp => report => dateFormatter.parse(timestamp).after(report.timestamp)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
76
}