Skip to content

Commit 5442ea1

Browse files
Brian-O-TNABrian-O-TNA
authored andcommitted
Parliamentary archive references now saved into KBS database in Parliamentary atrchive format (whilst still being validated against Advance Order service in TNA format).
1 parent 4bcb08e commit 5442ea1

1 file changed

Lines changed: 27 additions & 14 deletions

File tree

book-a-reading-room-visit.web/Helper/ValidateDocumentOrder.cs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace book_a_reading_room_visit.web.Helper
1515
public class ValidateDocumentOrder
1616
{
1717
private const string PARLY_ARCHIVES_CLASS_NO = "1/";
18-
private static readonly Regex _documentReferenceRegex = new Regex(Constants.Doc_Ref_Regex_General);
18+
private static readonly Regex _standardDocumentReferenceRegex = new Regex(Constants.Doc_Ref_Regex_General);
1919
private static readonly Regex _parlyArchivesReferenceRegex = new Regex(Constants.Doc_Ref_Regex_Parly_Archives);
2020

2121
private IAdvancedOrderService _advancedOrderService;
@@ -181,7 +181,7 @@ public void ValidateBulkOrderDocumentReferences(ModelStateDictionary modelStateD
181181
}
182182
}
183183

184-
private void ValidateReference(ModelStateDictionary modelStateDictionary, string docRerefenceName, string docReferenceVal, bool isReserved = false)
184+
private void ValidateReference(ModelStateDictionary modelStateDictionary, string docReferenceName, string docReferenceVal, bool isReserved = false)
185185
{
186186
if (!string.IsNullOrEmpty(docReferenceVal))
187187
{
@@ -195,16 +195,17 @@ private void ValidateReference(ModelStateDictionary modelStateDictionary, string
195195

196196
if (!string.IsNullOrEmpty(errMessage) && !documentIsOffsite)
197197
{
198-
modelStateDictionary.AddModelError(docRerefenceName, $"{docReferenceVal} - {errMessage}");
198+
modelStateDictionary.AddModelError(docReferenceName, $"{docReferenceVal} - {errMessage}");
199199
}
200200
else
201201
{
202-
var (letterCode, classNumber) = GetLetterCodeAndClassNumberFromReference(standardisedReference);
202+
try
203+
{
204+
var (letterCode, classNumber, isParliamentaryArchiveReference) = GetLetterCodeAndClassNumberFromReference(standardisedReference);
203205

204-
_validatedDocuments.Add(
205-
new DocumentViewModel()
206+
var docViewModel = new DocumentViewModel()
206207
{
207-
Reference = docRef.DocParts.DocumentReferenceText,
208+
Reference = isParliamentaryArchiveReference ? docReferenceVal : docRef.DocParts.DocumentReferenceText,
208209
Description = docRef.DocParts.Scope,
209210
LetterCode = letterCode,
210211
ClassNumber = classNumber,
@@ -214,7 +215,15 @@ private void ValidateReference(ModelStateDictionary modelStateDictionary, string
214215
SubClassNumber = docRef.DocParts.SubClass,
215216
IsOffsite = documentIsOffsite,
216217
IsReserved = isReserved
217-
});
218+
};
219+
220+
_validatedDocuments.Add(docViewModel);
221+
}
222+
catch (Exception)
223+
{
224+
modelStateDictionary.AddModelError(docReferenceName, $"{docReferenceVal} - Reference could not be validated.");
225+
}
226+
218227
}
219228
}
220229
}
@@ -245,13 +254,15 @@ public void ValidateNotOrderableSeries(ModelStateDictionary modelStateDictionary
245254
}
246255
}
247256

248-
private (string, int) GetLetterCodeAndClassNumberFromReference(string docReferenceVal)
257+
private (string, int, bool) GetLetterCodeAndClassNumberFromReference(string docReferenceVal)
249258
{
250-
Match match = _documentReferenceRegex.Match(docReferenceVal);
259+
bool isParliamentaryArchiveRef = false;
260+
Match match = _standardDocumentReferenceRegex.Match(docReferenceVal);
251261

252262
if (!match.Success)
253263
{
254264
match = _parlyArchivesReferenceRegex.Match(docReferenceVal);
265+
isParliamentaryArchiveRef = match.Success;
255266
}
256267

257268
var letterCode = match.Groups[1].Value;
@@ -261,11 +272,11 @@ public void ValidateNotOrderableSeries(ModelStateDictionary modelStateDictionary
261272
classNumber = number;
262273
}
263274

264-
return (letterCode, classNumber);
275+
return (letterCode, classNumber, isParliamentaryArchiveRef);
265276
}
266277
private string GetStandardisedDocReference(string docReferenceVal)
267278
{
268-
Match match = _parlyArchivesReferenceRegex.Match(docReferenceVal);
279+
Match match = _parlyArchivesReferenceRegex.Match(docReferenceVal);
269280

270281
if(match.Success && !IsTnaFormatReference(match))
271282
{
@@ -274,8 +285,10 @@ private string GetStandardisedDocReference(string docReferenceVal)
274285
(docReferenceVal.Substring(firstSlash + 1).StartsWith(PARLY_ARCHIVES_CLASS_NO) ? "" : PARLY_ARCHIVES_CLASS_NO) +
275286
docReferenceVal.Substring(firstSlash +1);
276287
}
277-
278-
return docReferenceVal;
288+
else
289+
{
290+
return docReferenceVal;
291+
}
279292
}
280293

281294
private bool IsTnaFormatReference(Match match)

0 commit comments

Comments
 (0)