| | 1 | | using Newtonsoft.Json.Linq; |
| | 2 | | using ValidateLib.ErrorsAndWarnings.Errors; |
| | 3 | | using ValidateLib.IRINormalization; |
| | 4 | | using ValidateLib.Metadata.MetdataLocation; |
| | 5 | | using ValidateLib.Metadata.ParsingAndValidation; |
| | 6 | | using ValidateLib.Metadata.Unification; |
| | 7 | | using ValidateLib.Results; |
| | 8 | | using ValidateLib.TableCompatibility; |
| | 9 | | using ValidateLib.TabularData.Parsing; |
| | 10 | | using ValidateLib.TabularData.Validation; |
| | 11 | | using ValidateLib.UtilityClasses; |
| | 12 | |
|
| | 13 | | namespace ValidateLib.Control |
| | 14 | | { |
| | 15 | | public class Controller : IController |
| | 16 | | { |
| | 17 | | #region Public |
| | 18 | | public IResult ProcessMetadata(string metadataIRI) |
| | 19 | | { |
| 1 | 20 | | ITableGroupValidationDetail validationDetails = new TableGroupValidationResultDetails(); |
| | 21 | | try |
| | 22 | | { |
| 1 | 23 | | ProcessMetadataUnsafe(metadataIRI, validationDetails); |
| 1 | 24 | | } |
| 1 | 25 | | catch (Error error) |
| | 26 | | { |
| 1 | 27 | | validationDetails.GeneralErrors.Add(error); |
| 1 | 28 | | } |
| 1 | 29 | | catch (Exception ex) |
| | 30 | | { |
| 1 | 31 | | validationDetails.GeneralErrors.Add(ErrorFactory.GetUnknownError(ex.Message)); |
| 1 | 32 | | return ResultCreator.CreateResult(validationDetails); |
| | 33 | | } |
| | 34 | |
|
| 1 | 35 | | return ResultCreator.CreateResult(validationDetails); |
| 1 | 36 | | } |
| | 37 | | public IResult ProcessTabularData(string tabularDataIRI) |
| | 38 | | { |
| 1 | 39 | | ITableGroupValidationDetail validationDetails = new TableGroupValidationResultDetails(); |
| | 40 | | try |
| | 41 | | { |
| 1 | 42 | | ProcessTabularDataUnsafe(tabularDataIRI, validationDetails); |
| 1 | 43 | | } |
| 0 | 44 | | catch (Error error) |
| | 45 | | { |
| 0 | 46 | | validationDetails.GeneralErrors.Add(error); |
| 0 | 47 | | } |
| 0 | 48 | | catch (Exception ex) |
| | 49 | | { |
| 0 | 50 | | validationDetails.GeneralErrors.Add(ErrorFactory.GetUnknownError(ex.Message)); |
| 0 | 51 | | return ResultCreator.CreateResult(validationDetails); |
| | 52 | | } |
| | 53 | |
|
| 1 | 54 | | return ResultCreator.CreateResult(validationDetails); |
| 0 | 55 | | } |
| | 56 | | public IResult ProcessOverriding(string metadataIRI, params string[] tabularDataIRIs) |
| | 57 | | { |
| 1 | 58 | | ITableGroupValidationDetail validationDetails = new TableGroupValidationResultDetails(); |
| | 59 | | try |
| | 60 | | { |
| 1 | 61 | | ProcessOverridingUnsafe(metadataIRI, validationDetails, tabularDataIRIs); |
| 1 | 62 | | } |
| 0 | 63 | | catch (Error error) |
| | 64 | | { |
| 0 | 65 | | validationDetails.GeneralErrors.Add(error); |
| 0 | 66 | | } |
| 0 | 67 | | catch (Exception e) |
| | 68 | | { |
| 0 | 69 | | validationDetails.GeneralErrors.Add(ErrorFactory.GetUnknownError(e.Message)); |
| 0 | 70 | | } |
| 1 | 71 | | return ResultCreator.CreateResult(validationDetails); |
| | 72 | | } |
| | 73 | | #endregion |
| | 74 | |
|
| | 75 | | #region Metadata |
| | 76 | | void ProcessMetadataUnsafe(string metadataIRI, ITableGroupValidationDetail validationDetails) |
| | 77 | | { |
| 1 | 78 | | if (IriUtilityClass.IsRemoteIri(metadataIRI)) |
| | 79 | | { |
| 1 | 80 | | ProcessMetadataRemote(metadataIRI, validationDetails); |
| | 81 | | } |
| | 82 | | else |
| | 83 | | { |
| 1 | 84 | | ProcessMetadataLocal(metadataIRI, validationDetails); |
| | 85 | | } |
| 1 | 86 | | } |
| | 87 | | void ProcessMetadataRemote(string metadataURL, ITableGroupValidationDetail validationDetails) |
| | 88 | | { |
| | 89 | |
|
| 1 | 90 | | JObject? descriptorObject = ObjectPropertyUtilityClass.GetDescriptor(metadataURL); |
| 1 | 91 | | if (descriptorObject is not null) |
| | 92 | | { |
| 1 | 93 | | HandleMetadataFoundCase(metadataURL, descriptorObject, validationDetails); |
| | 94 | | } |
| | 95 | | else |
| | 96 | | { |
| 1 | 97 | | validationDetails.GeneralErrors.Add(ErrorFactory.GetMetadataFileCouldNotBeLoadedError(metadataURL)); |
| | 98 | | } |
| 1 | 99 | | } |
| | 100 | | void ProcessMetadataLocal(string metadataPath, ITableGroupValidationDetail validationDetails) |
| | 101 | | { |
| | 102 | |
|
| 1 | 103 | | JObject? descriptorObject = ObjectPropertyUtilityClass.GetDescriptor(metadataPath); |
| 1 | 104 | | if (descriptorObject is not null) |
| | 105 | | { |
| 1 | 106 | | HandleMetadataFoundCase(metadataPath, descriptorObject, validationDetails); |
| | 107 | | } |
| | 108 | | else |
| | 109 | | { |
| 1 | 110 | | validationDetails.GeneralErrors.Add(ErrorFactory.GetMetadataFileCouldNotBeLoadedError(metadataPath)); |
| | 111 | | } |
| | 112 | |
|
| 1 | 113 | | } |
| | 114 | | #endregion |
| | 115 | |
|
| | 116 | | #region Tabular |
| | 117 | | void ProcessTabularDataUnsafe(string tabularDataIRI, ITableGroupValidationDetail validationDetails) |
| | 118 | | { |
| 1 | 119 | | if (IriUtilityClass.IsRemoteIri(tabularDataIRI)) |
| | 120 | | { |
| 1 | 121 | | ProcessTabularDataRemote(tabularDataIRI, validationDetails); |
| | 122 | | } |
| | 123 | | else |
| | 124 | | { |
| 1 | 125 | | ProcessTabularDataLocal(tabularDataIRI, validationDetails); |
| | 126 | | } |
| 1 | 127 | | } |
| | 128 | | void ProcessTabularDataLocal(string tabularDataPath, ITableGroupValidationDetail validationDetails) |
| | 129 | | { |
| 1 | 130 | | var metadataLocator = new MetadataLocator() |
| 1 | 131 | | { |
| 1 | 132 | | warnings = validationDetails.GeneralWarnings |
| 1 | 133 | | }; |
| 1 | 134 | | tabularDataPath = IRINormalizator.NormalizeIri(tabularDataPath); |
| 1 | 135 | | string? metadataPath = metadataLocator.LocateMetadataForLocalFile(tabularDataPath); |
| | 136 | |
|
| 1 | 137 | | if (metadataPath is not null) |
| | 138 | | { |
| 1 | 139 | | JObject? descriptorObject = ObjectPropertyUtilityClass.GetDescriptor(metadataPath); |
| 1 | 140 | | if (descriptorObject is not null) |
| | 141 | | { |
| 1 | 142 | | HandleMetadataFoundCase(metadataPath, descriptorObject, validationDetails); |
| | 143 | |
|
| | 144 | | } |
| | 145 | | else |
| | 146 | | { |
| 0 | 147 | | validationDetails.GeneralErrors.Add(ErrorFactory.GetMetadataFileCouldNotBeLoadedError(tabularDataPat |
| | 148 | | } |
| | 149 | | } |
| | 150 | | else |
| | 151 | | { |
| 1 | 152 | | ProcessTabularDataFileWithoutMetadata(tabularDataPath, validationDetails); |
| | 153 | | } |
| 1 | 154 | | } |
| | 155 | | void ProcessTabularDataRemote(string tabularDataURL, ITableGroupValidationDetail validationDetails) |
| | 156 | | { |
| 1 | 157 | | MetadataLocator metadataLocator = new MetadataLocator() |
| 1 | 158 | | { |
| 1 | 159 | | warnings = validationDetails.GeneralWarnings |
| 1 | 160 | |
|
| 1 | 161 | | }; |
| 1 | 162 | | string? metadataURL = metadataLocator.LocateMetadataForRemoteFileAsync(tabularDataURL).Result; |
| | 163 | |
|
| 1 | 164 | | if (metadataURL is not null) |
| | 165 | | { |
| 1 | 166 | | JObject? descriptorObject = ObjectPropertyUtilityClass.GetDescriptor(metadataURL); |
| 1 | 167 | | if (descriptorObject is not null) |
| | 168 | | { |
| 1 | 169 | | HandleMetadataFoundCase(metadataURL, descriptorObject, validationDetails); |
| | 170 | | } |
| | 171 | | else |
| | 172 | | { |
| 0 | 173 | | validationDetails.GeneralErrors.Add(ErrorFactory.GetMetadataFileCouldNotBeLoadedError(metadataURL)); |
| | 174 | | } |
| | 175 | | } |
| | 176 | | else |
| | 177 | | { |
| 1 | 178 | | ProcessTabularDataFileWithoutMetadata(tabularDataURL, validationDetails); |
| | 179 | | } |
| 1 | 180 | | } |
| | 181 | | /// <summary> |
| | 182 | | /// This handles case when metadata file has not been provided and located. We use embedded metadata instead. |
| | 183 | | /// </summary> |
| | 184 | | /// <param name="tabularDataFileUrlOrPath"></param> |
| | 185 | | /// <param name="validationDetails"></param> |
| | 186 | | void ProcessTabularDataFileWithoutMetadata(string tabularDataFileUrlOrPath, ITableGroupValidationDetail validati |
| | 187 | | { |
| | 188 | | try |
| | 189 | | { |
| 1 | 190 | | tabularDataFileUrlOrPath = IRINormalizator.NormalizeIri(tabularDataFileUrlOrPath); |
| 1 | 191 | | var tableGroupDescriptor = MetadataUnifier.UnifyTableWithoutMetadata(tabularDataFileUrlOrPath); |
| 1 | 192 | | ResultUtility.AddTablesToResult(tableGroupDescriptor, validationDetails); |
| | 193 | |
|
| 1 | 194 | | FlagsCreator.CreateFlags(tableGroupDescriptor); |
| 1 | 195 | | ICompatibilityChecker compatibilityChecker = CompatibilityCheckerFactory.GetCompatibilityChecker(); |
| 1 | 196 | | compatibilityChecker.CheckSchemesCompatible(tableGroupDescriptor, validationDetails); |
| | 197 | |
|
| 1 | 198 | | TabluarDataTableGroupValidator tableGroupValidator = new TabluarDataTableGroupValidator(tableGroupDescri |
| 1 | 199 | | tableGroupValidator.ValidateTableGroup(validationDetails); |
| | 200 | |
|
| 1 | 201 | | } |
| 0 | 202 | | catch (Error e) |
| | 203 | | { |
| 0 | 204 | | if (validationDetails.TableValidationDetails.Count > 0) |
| | 205 | | { |
| 0 | 206 | | validationDetails.TableValidationDetails[0].Errors.Add(e); |
| | 207 | | } |
| | 208 | | else |
| | 209 | | { |
| 0 | 210 | | validationDetails.GeneralErrors.Add(e); |
| | 211 | | } |
| 0 | 212 | | } |
| | 213 | |
|
| 1 | 214 | | } |
| | 215 | | #endregion |
| | 216 | |
|
| | 217 | | #region Commmon |
| | 218 | | /// <summary> |
| | 219 | | /// This handles table group with the descriptor object already downloaded in the previous steps. |
| | 220 | | /// </summary> |
| | 221 | | /// <param name="metadataUrlOrPath"></param> |
| | 222 | | /// <param name="descriptorObject"></param> |
| | 223 | | /// <param name="validationDetails"></param> |
| | 224 | | void ProcessTableGroup(string metadataUrlOrPath, JObject descriptorObject, ITableGroupValidationDetail validatio |
| | 225 | | { |
| 1 | 226 | | var tableGroupDescriptor = MetadataParserValidator.ProcessTableGroup( |
| 1 | 227 | | validationDetails.GeneralWarnings, metadataUrlOrPath, descriptorObject); |
| 1 | 228 | | MetadataUnifier.UnifyTableGroup(tableGroupDescriptor); |
| 1 | 229 | | ResultUtility.AddTablesToResult(tableGroupDescriptor, validationDetails); |
| | 230 | |
|
| 1 | 231 | | FlagsCreator.CreateFlags(tableGroupDescriptor); |
| 1 | 232 | | ICompatibilityChecker compatibilityChecker = CompatibilityCheckerFactory.GetCompatibilityChecker(); |
| 1 | 233 | | compatibilityChecker.CheckSchemesCompatible(tableGroupDescriptor, validationDetails); |
| | 234 | |
|
| 1 | 235 | | TabluarDataTableGroupValidator tableGroupValidator = new TabluarDataTableGroupValidator(tableGroupDescriptor |
| 1 | 236 | | tableGroupValidator.ValidateTableGroup(validationDetails); |
| 1 | 237 | | } |
| | 238 | | /// <summary> |
| | 239 | | /// This handles table with the descriptor object already downloaded in the previous steps. |
| | 240 | | /// </summary> |
| | 241 | | /// <param name="metadataUrlOrPath"></param> |
| | 242 | | /// <param name="descriptorObject"></param> |
| | 243 | | /// <param name="validationDetails"></param> |
| | 244 | | void ProcessTable(string metadataUrlOrPath, JObject descriptorObject, ITableGroupValidationDetail validationDeta |
| | 245 | | { |
| | 246 | | // the metadata descriptor is Table |
| 1 | 247 | | var tableDescriptor = MetadataParserValidator.ProcessTableFromJObject(validationDetails.GeneralWarnings, des |
| 1 | 248 | | var tableGroupDescriptor = MetadataUnifier.UnifyTable(tableDescriptor); |
| 1 | 249 | | ResultUtility.AddTablesToResult(tableGroupDescriptor, validationDetails); |
| | 250 | |
|
| 1 | 251 | | FlagsCreator.CreateFlags(tableGroupDescriptor); |
| 1 | 252 | | ICompatibilityChecker compatibilityChecker = CompatibilityCheckerFactory.GetCompatibilityChecker(); |
| 1 | 253 | | compatibilityChecker.CheckSchemesCompatible(tableGroupDescriptor, validationDetails); |
| | 254 | |
|
| 1 | 255 | | TabluarDataTableGroupValidator tableGroupValidator = new TabluarDataTableGroupValidator(tableGroupDescriptor |
| 1 | 256 | | tableGroupValidator.ValidateTableGroup(validationDetails); |
| | 257 | |
|
| 1 | 258 | | } |
| | 259 | | /// <summary> |
| | 260 | | /// Handles non-overriding cases when the metadata file could be found and loaded. |
| | 261 | | /// </summary> |
| | 262 | | /// <param name="metadataUrlOrPath"></param> |
| | 263 | | /// <param name="descriptorObject"></param> |
| | 264 | | /// <param name="validationDetails"></param> |
| | 265 | | void HandleMetadataFoundCase(string metadataUrlOrPath, JObject descriptorObject, ITableGroupValidationDetail val |
| | 266 | | { |
| 1 | 267 | | validationDetails.MetadataIRI = metadataUrlOrPath; |
| 1 | 268 | | if (JsonUtilityClass.IsObjectTableGroupDescriptor(descriptorObject)) |
| | 269 | | { |
| | 270 | | // the metadata descriptor is TableGroup |
| 1 | 271 | | ProcessTableGroup(metadataUrlOrPath, descriptorObject, validationDetails); |
| | 272 | | } |
| | 273 | | else |
| | 274 | | { |
| 1 | 275 | | ProcessTable(metadataUrlOrPath, descriptorObject, validationDetails); |
| | 276 | | } |
| 1 | 277 | | } |
| | 278 | | #endregion |
| | 279 | |
|
| | 280 | | #region Overriding |
| | 281 | | void ProcessOverridingUnsafe(string metadataIRI, ITableGroupValidationDetail validationDetails, params string[] |
| | 282 | | { |
| 1 | 283 | | JObject? descriptorObject = ObjectPropertyUtilityClass.GetDescriptor(metadataIRI); |
| 1 | 284 | | if (descriptorObject is not null) |
| | 285 | | { |
| 1 | 286 | | if (JsonUtilityClass.IsObjectTableGroupDescriptor(descriptorObject)) |
| | 287 | | { |
| 1 | 288 | | ProcessTableGroupOR(metadataIRI, validationDetails, tabularDataIRIs); |
| | 289 | | } |
| | 290 | | else |
| | 291 | | { |
| 1 | 292 | | if (tabularDataIRIs.Length == 1) |
| 1 | 293 | | ProcessTableOR(metadataIRI, validationDetails, tabularDataIRIs[0]); |
| | 294 | | else |
| | 295 | | { |
| 0 | 296 | | validationDetails.GeneralErrors.Add(ErrorFactory.GetWrongNumberOfTabularIRIsProvidedError(tabula |
| | 297 | | } |
| | 298 | | } |
| | 299 | | } |
| | 300 | | else |
| | 301 | | { |
| 0 | 302 | | validationDetails.GeneralErrors.Add(ErrorFactory.GetMetadataFileCouldNotBeLoadedError(metadataIRI)); |
| | 303 | | } |
| 0 | 304 | | } |
| | 305 | | void ProcessTableOR(string metadataIRI, ITableGroupValidationDetail validationDetails, string tabularDataIRI) |
| | 306 | | { |
| 1 | 307 | | JObject? descriptorObject = ObjectPropertyUtilityClass.GetDescriptor(metadataIRI); |
| 1 | 308 | | metadataIRI = IRINormalizator.NormalizeIri(metadataIRI); |
| 1 | 309 | | tabularDataIRI = IRINormalizator.NormalizeIri(tabularDataIRI); |
| 1 | 310 | | if (descriptorObject is not null) |
| | 311 | | { |
| 1 | 312 | | descriptorObject["url"] = tabularDataIRI; |
| 1 | 313 | | ProcessTable(metadataIRI, descriptorObject, validationDetails); |
| | 314 | | } |
| | 315 | | else |
| | 316 | | { |
| 0 | 317 | | validationDetails.GeneralErrors.Add(ErrorFactory.GetMetadataFileCouldNotBeLoadedError(metadataIRI)); |
| | 318 | | } |
| 0 | 319 | | } |
| | 320 | | void ProcessTableGroupOR(string metadataIRI, ITableGroupValidationDetail validationDetails, params string[] tabu |
| | 321 | | { |
| 1 | 322 | | JObject? descriptorObject = ObjectPropertyUtilityClass.GetDescriptor(metadataIRI); |
| | 323 | |
|
| 1 | 324 | | if (descriptorObject is not null) |
| | 325 | | { |
| 1 | 326 | | if (descriptorObject.ContainsKey("tables") && descriptorObject["tables"]!.Type == JTokenType.Array) |
| | 327 | | { |
| | 328 | |
|
| 1 | 329 | | ProcessCorrectDescriptorOverridingMetadata(metadataIRI, validationDetails, tabularDataIRIs, descript |
| | 330 | | } |
| | 331 | | else |
| | 332 | | { |
| 0 | 333 | | ProcessTableGroup(metadataIRI, descriptorObject, validationDetails); |
| | 334 | | } |
| | 335 | | } |
| | 336 | | else |
| | 337 | | { |
| 0 | 338 | | validationDetails.GeneralErrors.Add(ErrorFactory.GetMetadataFileCouldNotBeLoadedError(metadataIRI)); |
| | 339 | | } |
| 0 | 340 | | } |
| | 341 | |
|
| | 342 | | /// <summary> |
| | 343 | | /// This manually sets the references from the metadata table group descriptor to the overriding tables user pro |
| | 344 | | /// </summary> |
| | 345 | | /// <param name="tabularDataIRIs"></param> |
| | 346 | | /// <param name="tables"></param> |
| | 347 | | void ReplaceTabularDataIRIsInTableDescriptions(string[] tabularDataIRIs, JArray tables) |
| | 348 | | { |
| 1 | 349 | | for (int i = 0; i < tabularDataIRIs.Length; i++) |
| | 350 | | { |
| 1 | 351 | | tables[i]["url"] = IRINormalizator.IsAbsoluteUrl(tabularDataIRIs[i]) ? IRINormalizator.NormalizeIri(tabu |
| | 352 | | } |
| 1 | 353 | | } |
| | 354 | | /// <summary> |
| | 355 | | /// This transforms the case of overriding metadata to not overriding case as we just add the references to tabu |
| | 356 | | /// to the tablegroup description ourselves. |
| | 357 | | /// </summary> |
| | 358 | | /// <param name="metadataIRI"></param> |
| | 359 | | /// <param name="validationDetails"></param> |
| | 360 | | /// <param name="tabularDataIRIs"></param> |
| | 361 | | /// <param name="descriptorObject"></param> |
| | 362 | | void ProcessCorrectDescriptorOverridingMetadata(string metadataIRI, ITableGroupValidationDetail validationDetail |
| | 363 | | { |
| 1 | 364 | | var tables = (JArray)descriptorObject["tables"]!; |
| 1 | 365 | | if (tabularDataIRIs.Length == tables.Count) |
| | 366 | | { |
| 1 | 367 | | ReplaceTabularDataIRIsInTableDescriptions(tabularDataIRIs, tables); |
| 1 | 368 | | ProcessTableGroup(metadataIRI, descriptorObject, validationDetails); |
| | 369 | | } |
| | 370 | | else |
| | 371 | | { |
| 0 | 372 | | validationDetails.GeneralErrors.Add(ErrorFactory.GetUserMetadataTablesIrisMismatchMetdataError(tabularDa |
| | 373 | | } |
| 0 | 374 | | } |
| | 375 | |
|
| | 376 | | #endregion |
| | 377 | | } |
| | 378 | | } |