MsgFilter.scala 4.18 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

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
34
35
  def buildFilters(createdAfter: Option[Calendar],
                   createdBefore: Option[Calendar],
36
37
38
                   institutions: Seq[String],
                   recordSets: Seq[String],
                   records: Seq[String],
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
39
40
41
42
43
44
45
                   sessions: Seq[String]): Set[FilterFun] = {
    Set(buildCreatedAfterFilter(createdAfter)) ++
      Set(buildCreatedBeforeFilter(createdBefore)) ++
      Set(buildSessionIdFilters(sessions)) ++
      Set(buildRecordIdFilters(records)) ++
      Set(buildInstitutionIdFilters(institutions)) ++
      Set(buildRecordSetIdFilters(recordSets))
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
46
  }
47
48
49

  private def standardiseTimestamp(calendar: Calendar): String =
    f"${calendar.get(Calendar.YEAR)}%04d-" +
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
50
      f"${calendar.get(Calendar.MONTH) + 1}%02d-" +
51
52
53
54
55
56
      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"

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  private def buildSessionIdFilters(sessionIds: Seq[String]): FilterFun = {
    logger.info(if (sessionIds.isEmpty) {
      "no sessionIdFilters built"
    } else {
      s"sessionIdFilters built for ${sessionIds.mkString(", ")}"
    })
    report => sessionIds.isEmpty || sessionIds.contains(report.sessionId)
  }

  private def buildRecordSetIdFilters(recordSetIds: Seq[String]): FilterFun = {
    logger.info(if (recordSetIds.isEmpty) {
      "no recordSetIdFilters built"
    } else {
      s"recordSetIdFilters built for ${recordSetIds.mkString(", ")}"
    })
    report => recordSetIds.isEmpty || recordSetIds.contains(report.recordSetId)
  }
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
74

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
75
76
77
78
79
80
81
82
  private def buildInstitutionIdFilters(institutionIds: Seq[String]): FilterFun = {
    logger.info(if (institutionIds.isEmpty) {
      "no institutionIdFilters built"
    } else {
      s"institutionIdFilters built for ${institutionIds.mkString(", ")}"
    })
    report => institutionIds.isEmpty || institutionIds.contains(report.institutionId)
  }
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
83

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
84
85
86
87
88
89
90
91
  private def buildRecordIdFilters(recordIds: Seq[String]): FilterFun = {
    logger.info(if (recordIds.isEmpty) {
      "no recordIdFilters built"
    } else {
      s"recordIdFilters built for ${recordIds.mkString(", ")}"
    })
    report => recordIds.isEmpty || recordIds.contains(report.recordId)
  }
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
92

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
93
94
95
96
97
98
99
100
101
102
103
  private def buildCreatedAfterFilter(timestamp: Option[Calendar]): FilterFun = {
    val standardisedTimestamp = timestamp.collect {
      case t => standardiseTimestamp(t)
    }
    logger.info(if (timestamp.isEmpty) {
      "no createdAfterFilter built"
    } else {
      s"createdAfterFilter built for $timestamp"
    })
    report => standardisedTimestamp.isEmpty || dateFormatter.parse(standardisedTimestamp.get).before(report.timestamp)
  }
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
104

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
105
106
107
108
109
110
111
112
113
114
115
  private def buildCreatedBeforeFilter(timestamp: Option[Calendar]): FilterFun = {
    val standardisedTimestamp = timestamp.collect {
      case t => standardiseTimestamp(t)
    }
    logger.info(if (timestamp.isEmpty) {
      "no createdBeforeFilter built"
    } else {
      s"createdBeforeFilter built for $timestamp"
    })
    report => standardisedTimestamp.isEmpty || dateFormatter.parse(standardisedTimestamp.get).after(report.timestamp)
  }
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
116
117

}