Due to a scheduled upgrade to version 14.10, GitLab will be unavailabe on Monday 30.05., from 19:00 until 20:00.

MsgFilter.scala 4.44 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
  private def buildSessionIdFilters(sessionIds: Seq[String]): FilterFun = {
    logger.info(if (sessionIds.isEmpty) {
      "no sessionIdFilters built"
    } else {
      s"sessionIdFilters built for ${sessionIds.mkString(", ")}"
    })
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
63
64
    val trimmedSessionIds = sessionIds.map(_.trim)
    report => sessionIds.isEmpty || trimmedSessionIds.contains(report.sessionId.trim)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
65
66
67
68
69
70
71
72
  }

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

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
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(", ")}"
    })
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
83
84
    val trimmedInstitutionIds = institutionIds.map(_.trim)
    report => institutionIds.isEmpty || trimmedInstitutionIds.contains(report.institutionId.trim)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
85
  }
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
86

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

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
97
98
99
100
101
102
103
104
105
106
107
  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
108

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
109
110
111
112
113
114
115
116
117
118
119
  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
120
121

}