|
15 | 15 | using Altinn.Studio.Designer.Helpers;
|
16 | 16 | using Altinn.Studio.Designer.Models;
|
17 | 17 | using Altinn.Studio.Designer.Models.App;
|
| 18 | +using Altinn.Studio.Designer.Models.Dto; |
18 | 19 | using Altinn.Studio.Designer.TypedHttpClients.Exceptions;
|
19 | 20 | using LibGit2Sharp;
|
20 | 21 | using JsonSerializer = System.Text.Json.JsonSerializer;
|
@@ -504,6 +505,94 @@ public async Task<JsonNode> GetLayoutSettingsAndCreateNewIfNotFound(string layou
|
504 | 505 | return layoutSettings;
|
505 | 506 | }
|
506 | 507 |
|
| 508 | + /// <summary> |
| 509 | + /// Finds all <see cref="RefToOptionListSpecifier"/> in a given layout. |
| 510 | + /// </summary> |
| 511 | + /// <param name="layout">The layout.</param> |
| 512 | + /// <param name="refToOptionListSpecifiers">A list of occurrences to append any optionListIdRefs in the layout to.</param> |
| 513 | + /// <param name="layoutSetName">The layoutSetName the layout belongs to.</param> |
| 514 | + /// <param name="layoutName">The name of the given layout.</param> |
| 515 | + /// <returns>A list of <see cref="RefToOptionListSpecifier"/>.</returns> |
| 516 | + public List<RefToOptionListSpecifier> FindOptionListReferencesInLayout(JsonNode layout, List<RefToOptionListSpecifier> refToOptionListSpecifiers, string layoutSetName, string layoutName) |
| 517 | + { |
| 518 | + var optionListIds = GetOptionsListIds(); |
| 519 | + var layoutArray = layout["data"]?["layout"] as JsonArray; |
| 520 | + if (layoutArray == null) |
| 521 | + { |
| 522 | + return refToOptionListSpecifiers; |
| 523 | + } |
| 524 | + |
| 525 | + foreach (var item in layoutArray) |
| 526 | + { |
| 527 | + string optionListId = item["optionsId"]?.ToString(); |
| 528 | + |
| 529 | + if (!optionListIds.Contains(optionListId)) |
| 530 | + { |
| 531 | + continue; |
| 532 | + } |
| 533 | + |
| 534 | + if (!String.IsNullOrEmpty(optionListId)) |
| 535 | + { |
| 536 | + if (OptionListIdAlreadyOccurred(refToOptionListSpecifiers, optionListId, out var existingRef)) |
| 537 | + { |
| 538 | + if (OptionListIdAlreadyOccurredInLayout(existingRef, layoutSetName, layoutName, out var existingSource)) |
| 539 | + { |
| 540 | + existingSource.ComponentIds.Add(item["id"]?.ToString()); |
| 541 | + } |
| 542 | + else |
| 543 | + { |
| 544 | + AddNewOptionListIdSource(existingRef, layoutSetName, layoutName, item["id"]?.ToString()); |
| 545 | + } |
| 546 | + } |
| 547 | + else |
| 548 | + { |
| 549 | + AddNewRefToOptionListSpecifier(refToOptionListSpecifiers, optionListId, layoutSetName, layoutName, item["id"]?.ToString()); |
| 550 | + } |
| 551 | + } |
| 552 | + } |
| 553 | + return refToOptionListSpecifiers; |
| 554 | + } |
| 555 | + |
| 556 | + private bool OptionListIdAlreadyOccurred(List<RefToOptionListSpecifier> refToOptionListSpecifiers, string optionListId, out RefToOptionListSpecifier existingRef) |
| 557 | + { |
| 558 | + existingRef = refToOptionListSpecifiers.FirstOrDefault(refToOptionList => refToOptionList.OptionListId == optionListId); |
| 559 | + return existingRef != null; |
| 560 | + } |
| 561 | + |
| 562 | + private bool OptionListIdAlreadyOccurredInLayout(RefToOptionListSpecifier refToOptionListSpecifier, string layoutSetName, string layoutName, out OptionListIdSource existingSource) |
| 563 | + { |
| 564 | + existingSource = refToOptionListSpecifier.OptionListIdSources |
| 565 | + .FirstOrDefault(optionListIdSource => optionListIdSource.LayoutSetId == layoutSetName && optionListIdSource.LayoutName == layoutName); |
| 566 | + return existingSource != null; |
| 567 | + } |
| 568 | + |
| 569 | + private void AddNewRefToOptionListSpecifier(List<RefToOptionListSpecifier> refToOptionListSpecifiers, string optionListId, string layoutSetName, string layoutName, string componentId) |
| 570 | + { |
| 571 | + refToOptionListSpecifiers.Add(new() |
| 572 | + { |
| 573 | + OptionListId = optionListId, |
| 574 | + OptionListIdSources = |
| 575 | + [ |
| 576 | + new OptionListIdSource |
| 577 | + { |
| 578 | + LayoutSetId = layoutSetName, |
| 579 | + LayoutName = layoutName, |
| 580 | + ComponentIds = [componentId] |
| 581 | + } |
| 582 | + ] |
| 583 | + }); |
| 584 | + } |
| 585 | + |
| 586 | + private void AddNewOptionListIdSource(RefToOptionListSpecifier refToOptionListSpecifier, string layoutSetName, string layoutName, string componentId) |
| 587 | + { |
| 588 | + refToOptionListSpecifier.OptionListIdSources.Add(new OptionListIdSource |
| 589 | + { |
| 590 | + LayoutSetId = layoutSetName, |
| 591 | + LayoutName = layoutName, |
| 592 | + ComponentIds = [componentId] |
| 593 | + }); |
| 594 | + } |
| 595 | + |
507 | 596 | private async Task CreateLayoutSettings(string layoutSetName)
|
508 | 597 | {
|
509 | 598 | string layoutSetPath = GetPathToLayoutSet(layoutSetName);
|
|
0 commit comments