ArgParser.scala 2.9 KB
Newer Older
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package ch.memobase

import java.util.Calendar

import ch.memobase.models._
import scopt.{OParser, OParserBuilder}

trait ArgParser {
  self: MsgFilter =>

  private val builder: OParserBuilder[Args] = OParser.builder[Args]

  private val parser: OParser[Unit, Args] = {
    import builder._
    OParser.sequence(
      programName("import-process-delete"),
      head("", "0.1.0"),
      opt[String]('c', "record-set-filter")
        .action((v, c) => c.copy(recordSetFilters = c.recordSetFilters :+ v))
        .valueName("<id>")
        .text("record set id filter")
        .optional(),
      opt[String]('i', "institution-filter")
        .action((v, c) => c.copy(institutionFilters = c.institutionFilters :+ v))
        .valueName("<id>")
        .text("institution id filter")
        .optional(),
      opt[String]('r', "record-filter")
        .action((v, c) => c.copy(recordFilters = c.recordFilters :+ v))
        .valueName("<id>")
        .text("record id filter")
        .optional(),
      opt[String]('s', "session-filter")
        .action((v, c) => c.copy(sessionFilters = c.sessionFilters :+ v))
        .valueName("<id>")
        .text("session id filter")
        .optional(),
      opt[Calendar]('a', "created-after")
        .action((v, c) => c.copy(createdAfterFilter = v))
        .valueName("<datetime>")
        .text("retains only records processed after timestamp")
        .maxOccurs(1)
        .optional(),
      opt[Calendar]('b', "created-before")
        .action((v, c) => c.copy(createdBeforeFilter = v))
        .valueName("<datetime>")
        .text("retains only records processed before timestamp")
        .maxOccurs(1)
        .optional(),
      opt[String]("<session-id>")
        .action((v, c) => c.copy(sessionId = v))
        .text("session id assigned to delete message")
        .required(),
      help("help").text("prints this text")
    )
  }

  def parse(args: Array[String]): Option[(String, Seq[FilterFun])] = {
    OParser.parse(parser, args, Args()) match {
      case Some(config) => Some(
        config.sessionId,
        Seq(createdAfterFilter(standardiseTimestamp(config.createdAfterFilter))) ++
          Seq(createdBeforeFilter(standardiseTimestamp(config.createdBeforeFilter))) ++
          config.institutionFilters.map(v => institutionIdFilter(v)) ++
          config.recordFilters.map(v => createRecordIdFilter(v)) ++
          config.recordSetFilters.map(v => createRecordIdFilter(v)) ++
          config.sessionFilters.map(v => sessionIdFilter(v)))
      case None => None
    }
  }

  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"


}