@@ -11,6 +11,7 @@ package uk.gov.nationalarchives.csv.validator.schema
1111import org .joda .time .DateTime
1212import uk .gov .nationalarchives .csv .validator .metadata .Row
1313
14+ import scala .collection .mutable .MutableList
1415import scala .util .Try
1516import scala .util .parsing .input .Positional
1617import scalaz ._
@@ -22,10 +23,37 @@ abstract class Rule(name: String, val argProviders: ArgProvider*) extends Positi
2223
2324 var explicitColumn : Option [ColumnReference ] = None
2425
26+ def findColumnReference (): Option [ColumnReference ] = {
27+ if (explicitColumns.nonEmpty){
28+ val index = explicitColumnIndex
29+ val result = explicitColumns.get(index)
30+ if (index + 1 == explicitColumns.length)
31+ explicitColumnIndex = 0
32+ else
33+ explicitColumnIndex = index + 1
34+ result
35+ }
36+ else {
37+ explicitColumn = None
38+ None
39+ }
40+ }
41+
42+
43+ def findColumnRefence (rule : Rule ): Option [ColumnReference ] =
44+ rule.findColumnReference()
45+
46+ var explicitColumnIndex = 0
47+
48+ val explicitColumns : MutableList [ColumnReference ] = MutableList ()
49+
2550 def evaluate (columnIndex : Int , row : Row , schema : Schema , mayBeLast : Option [Boolean ] = None ): RuleValidation [Any ] = {
26- if (valid(cellValue(columnIndex, row, schema), schema.columnDefinitions(columnIndex), columnIndex, row, schema, mayBeLast)) true .successNel[String ] else fail(columnIndex, row, schema)
51+ if (valid(cellValue(columnIndex, row, schema), schema.columnDefinitions(columnIndex), columnIndex, row, schema, mayBeLast))
52+ true .successNel[String ]
53+ else fail(columnIndex, row, schema)
2754 }
2855
56+
2957 def valid (cellValue : String , columnDefinition : ColumnDefinition , columnIndex : Int ,
3058 row : Row , schema : Schema , mayBeLast : Option [Boolean ] = None ): Boolean =
3159 evaluate(columnIndex, row, schema).isSuccess
@@ -36,15 +64,18 @@ abstract class Rule(name: String, val argProviders: ArgProvider*) extends Positi
3664 s " $toError fails for line: ${row.lineNumber}, column: ${columnDefinition.id}, ${toValueError(row,columnIndex)}" .failureNel[Any ]
3765 }
3866
39- def cellValue (columnIndex : Int , row : Row , schema : Schema ): String = explicitColumn match {
40- case Some (columnRef) =>
41- columnRef.referenceValueEx(columnIndex, row, schema)
42- case None =>
43- row.cells(columnIndex).value
67+ def cellValue (columnIndex : Int , row : Row , schema : Schema ): String = {
68+ explicitColumn match {
69+ case Some (columnRef) =>
70+ columnRef.referenceValueEx(columnIndex, row, schema)
71+ case None =>
72+ row.cells(columnIndex).value
73+ }
4474 }
4575
4676 def explicitName : Option [String ] = explicitColumn.map(" $" + _.ref + " /" )
4777
78+
4879 def ruleName : String = explicitName.getOrElse(" " ) + name
4980
5081 def columnIdentifierToIndex (schema : Schema , id : ColumnIdentifier ): Int = {
@@ -57,9 +88,15 @@ abstract class Rule(name: String, val argProviders: ArgProvider*) extends Positi
5788 }
5889 }
5990
60- def toValueError (row : Row , columnIndex: Int ) = s """ value: ${'"' }${row.cells(columnIndex).value}${'"' }"""
6191
62- def toError = s """ $ruleName""" + (if (argProviders.isEmpty) " " else " (" + argProviders.foldLeft(" " )((a, b) => (if (a.isEmpty) " " else a + " , " ) + b.toError) + " )" )
92+
93+ def toValueError (row : Row , columnIndex: Int ) =
94+ s """ value: ${'"' }${row.cells(columnIndex).value}${'"' }"""
95+
96+
97+ def toError =
98+ s """ $ruleName""" + (if (argProviders.isEmpty) " " else " (" + argProviders.foldLeft(" " )((a, b) => (if (a.isEmpty) " " else a + " , " ) + b.toError) + " )" )
99+
63100}
64101
65102abstract class PatternRule (name : String , pattern : String ) extends Rule (name) {
0 commit comments