/* * Import Process Delete * Copyright (C) 2020 Memoriav * * 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 . */ package ch.memobase import java.text.SimpleDateFormat import java.util.Calendar import ch.memobase.models.Report import org.apache.logging.log4j.scala.Logging trait MsgFilter { self: Logging => private val dateFormatter = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ss.SSS") type FilterFun = Report => Boolean def buildFilters(createdAfter: Calendar, createdBefore: Calendar, institutions: Seq[String], recordSets: Seq[String], records: Seq[String], sessions: Seq[String]): Seq[FilterFun] = Seq(buildCreatedAfterFilter(standardiseTimestamp(createdAfter))) ++ Seq(buildCreatedBeforeFilter(standardiseTimestamp(createdBefore))) ++ institutions.map(v => buildInstitutionIdFilter(v)) ++ records.map(v => buildRecordIdFilter(v)) ++ recordSets.map(v => buildRecordSetIdFilter(v)) ++ sessions.map(v => buildSessionIdFilter(v)) 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 = sessionId => report => report.sessionId == sessionId private val buildRecordSetIdFilter: String => FilterFun = recordSetId => report => report.recordSetId == recordSetId private val buildInstitutionIdFilter: String => FilterFun = institutionId => report => report.institutionId == institutionId private val buildRecordIdFilter: String => FilterFun = recordId => report => report.recordId == recordId private val buildCreatedAfterFilter: String => FilterFun = timestamp => report => dateFormatter.parse(timestamp).after(report.timestamp) private val buildCreatedBeforeFilter: String => FilterFun = timestamp => report => dateFormatter.parse(timestamp).before(report.timestamp) }