From 65b2accd22cf74c921acf83fb267319fa46e1ad8 Mon Sep 17 00:00:00 2001
From: RIHTARSIC Joze <joze.rihtarsic@ext.ec.europa.eu>
Date: Thu, 5 Sep 2024 14:39:12 +0200
Subject: [PATCH] [EDELIVERY-13860] Fix DNS tool identifier validation to use
 default identifier settings.

---
 .../tools/dns-tools/dns-tools.component.html  | 23 +++++++++
 .../tools/dns-tools/dns-tools.component.ts    | 47 ++++++++++++++++---
 smp-angular/src/assets/i18n/en.json           |  6 ++-
 ...entifierServicePropertyUpdateListener.java |  7 ++-
 4 files changed, 74 insertions(+), 9 deletions(-)

diff --git a/smp-angular/src/app/tools/dns-tools/dns-tools.component.html b/smp-angular/src/app/tools/dns-tools/dns-tools.component.html
index b168b0bda..dd1037548 100644
--- a/smp-angular/src/app/tools/dns-tools/dns-tools.component.html
+++ b/smp-angular/src/app/tools/dns-tools/dns-tools.component.html
@@ -17,12 +17,28 @@
           <input matInput name="ResourceIdentifier"
                  formControlName="resourceIdentifier"
                  id="resource-identifier-id">
+          <div
+            *ngIf="(dnsToolsForm.controls['resourceIdentifier'].touched ) &&  dnsToolsForm.controls['resourceIdentifier'].hasError('required')"
+            style="color:red; font-size: 70%">
+            {{ "dns.tools.resource.id.mandatory" | translate }}
+          </div>
         </mat-form-field>
         <mat-form-field class="smp-data-panel-field">
           <mat-label>{{ "dns.tools.label.resource.scheme" | translate }}</mat-label>
           <input matInput name="ResourceScheme"
                  formControlName="resourceScheme"
                  id="resource-scheme-id">
+          <div
+            *ngIf="(dnsToolsForm.controls['resourceScheme'].touched ) &&  dnsToolsForm.controls['resourceScheme'].hasError('required')"
+            style="color:red; font-size: 70%">
+            {{ "dns.tools.label.resource.scheme.mandatory" | translate }}
+          </div>
+          <div
+            *ngIf="(dnsToolsForm.controls['resourceScheme'].touched ) &&
+               dnsToolsForm.controls['resourceScheme'].hasError('pattern')"
+            style="color:red; font-size: 70%">
+            {{participantSchemeMessage}}
+          </div>
         </mat-form-field>
         <mat-form-field class="smp-data-panel-field">
           <mat-label>{{ "dns.tools.label.resource.top.domain" | translate }}</mat-label>
@@ -39,6 +55,13 @@
           <mat-icon>input</mat-icon>
           <span>{{ "dns.tools.button.generate.lookup.query" | translate }}</span>
         </button>
+
+        <tool-button-spacer></tool-button-spacer>
+        <button mat-raised-button color="primary" (click)="clearData()"
+                [disabled]="!dnsToolsForm.dirty" id="clearallbutton_id">
+          <mat-icon>cancel</mat-icon>
+          <span>{{ "form.button.clear" | translate }}</span>
+        </button>
       </div>
     </ng-template>
   </form>
diff --git a/smp-angular/src/app/tools/dns-tools/dns-tools.component.ts b/smp-angular/src/app/tools/dns-tools/dns-tools.component.ts
index 43d912f39..6164ce376 100644
--- a/smp-angular/src/app/tools/dns-tools/dns-tools.component.ts
+++ b/smp-angular/src/app/tools/dns-tools/dns-tools.component.ts
@@ -1,8 +1,11 @@
 import {Component} from "@angular/core";
 import {DnsToolsService} from "./dns-tools.service";
 import {DnsQueryRo} from "../../common/model/dns-query-ro.model";
-import {FormBuilder, FormGroup, Validators} from "@angular/forms";
+import {FormBuilder, FormControl, FormGroup, Validators} from "@angular/forms";
 import {GlobalLookups} from "../../common/global-lookups";
+import {
+  AlertMessageService
+} from "../../common/alert-message/alert-message.service";
 
 @Component({
   templateUrl: './dns-tools.component.html',
@@ -14,18 +17,33 @@ export class DnsToolsComponent {
   dnsToolsForm: FormGroup;
   private _result: DnsQueryRo[];
 
+  participantSchemePattern: string = '^[a-z0-9]+-[a-z0-9]+-[a-z0-9]+$';
+  participantSchemeMessage: string;
+  submitInProgress: boolean = false;
+
   constructor(private dnsToolsService: DnsToolsService,
               private lookups: GlobalLookups,
+              private alertService: AlertMessageService,
               private formBuilder: FormBuilder) {
 
     this.dnsToolsForm = formBuilder.group({
       // common values
-      'resourceIdentifier': ['', Validators.required],
-      'resourceScheme': [''],
+      'resourceIdentifier': new FormControl({value: null}, [Validators.required]),
+      'resourceScheme': new FormControl({value: null}, [Validators.pattern(this.participantSchemePattern)]),
       'dnsTopDomain': [''],
     });
-  }
+    this.clearData();
+    // set the default system validation values
+    if (this.lookups.cachedApplicationConfig) {
+      this.participantSchemePattern = this.lookups.cachedApplicationConfig.participantSchemaRegExp != null ?
+        this.lookups.cachedApplicationConfig.participantSchemaRegExp : ".*"
+      this.participantSchemeMessage = this.lookups.cachedApplicationConfig.participantSchemaRegExpMessage;
+    }
 
+    if (!!lookups.cachedApplicationConfig.partyIDSchemeMandatory) {
+      this.dnsToolsForm.controls['resourceScheme'].addValidators(Validators.required);
+    }
+  }
 
 
   get result(): DnsQueryRo[] {
@@ -38,8 +56,25 @@ export class DnsToolsComponent {
       this.dnsToolsForm.get('dnsTopDomain').value,
       this.dnsToolsForm.get('resourceIdentifier').value,
       this.dnsToolsForm.get('resourceScheme').value
-    ).subscribe((res: DnsQueryRo[]) => {
-      this._result = res;
+    ).subscribe({
+      next: (res: DnsQueryRo[]): void => {
+        this._result = res;
+      }, error: (err: any): void => {
+        this.alertService.error(err);
+      }
     })
   }
+
+  get submitButtonEnabled(): boolean {
+    return this.dnsToolsForm.valid && this.dnsToolsForm.dirty && !this.submitInProgress;
+  }
+
+  clearData(): void {
+    this.dnsToolsForm.get('dnsTopDomain').setValue("");
+    this.dnsToolsForm.get('resourceIdentifier').setValue("");
+    this.dnsToolsForm.get('resourceScheme').setValue("");
+    this._result = [];
+    this.dnsToolsForm.markAsPristine();
+    this.dnsToolsForm.markAsUntouched();
+  }
 }
diff --git a/smp-angular/src/assets/i18n/en.json b/smp-angular/src/assets/i18n/en.json
index 66def9da4..13af0755d 100644
--- a/smp-angular/src/assets/i18n/en.json
+++ b/smp-angular/src/assets/i18n/en.json
@@ -757,6 +757,8 @@
   "dns.tools.label.resource.top.domain": "Top domain",
   "dns.tools.description": "Enter the DNS data to create DNS query",
   "dns.tools.title": "DNS lookup",
+  "dns.tools.resource.id.mandatory": "Resource identifier is required!",
+  "dns.tools.resource.scheme.mandatory": "Resource scheme is required!",
 
   "access.token.panel.button.delete": "Delete",
   "access.token.panel.button.save": "Save",
@@ -848,5 +850,7 @@
   "navigation.label.user.settings.membership": "Membership",
   "navigation.tooltip.search.resources": "Search registered resources",
   "navigation.tooltip.search.tools": "Search tools",
-  "navigation.tooltip.search.dns.tools": "DNS lookup tools"
+  "navigation.tooltip.search.dns.tools": "DNS lookup tools",
+
+  "form.button.clear": "Clear"
 }
diff --git a/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/config/properties/SMPIdentifierServicePropertyUpdateListener.java b/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/config/properties/SMPIdentifierServicePropertyUpdateListener.java
index 32e039f9e..e86c9855c 100644
--- a/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/config/properties/SMPIdentifierServicePropertyUpdateListener.java
+++ b/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/config/properties/SMPIdentifierServicePropertyUpdateListener.java
@@ -22,6 +22,7 @@ import eu.europa.ec.edelivery.smp.config.PropertyUpdateListener;
 import eu.europa.ec.edelivery.smp.config.enums.SMPPropertyEnum;
 import eu.europa.ec.edelivery.smp.logging.SMPLogger;
 import eu.europa.ec.edelivery.smp.logging.SMPLoggerFactory;
+import eu.europa.ec.edelivery.smp.services.IdentifierFormatterService;
 import org.springframework.cache.Cache;
 import org.springframework.cache.CacheManager;
 import org.springframework.stereotype.Component;
@@ -51,7 +52,9 @@ import static eu.europa.ec.edelivery.smp.config.enums.SMPPropertyEnum.*;
 public class SMPIdentifierServicePropertyUpdateListener implements PropertyUpdateListener {
     private static final SMPLogger LOG = SMPLoggerFactory.getLogger(SMPIdentifierServicePropertyUpdateListener.class);
 
-    private static final List<String> namedCachesToClear = Arrays.asList("participantIdentifiers", "documentIdentifiers");
+    private static final List<String> namedCachesToClear = Arrays.asList(
+            IdentifierFormatterService.CACHE_NAME_DOMAIN_RESOURCE_IDENTIFIER_FORMATTER,
+            IdentifierFormatterService.CACHE_NAME_DOMAIN_SUBRESOURCE_IDENTIFIER_FORMATTER);
 
 
     private final CacheManager cacheManager;
@@ -66,7 +69,7 @@ public class SMPIdentifierServicePropertyUpdateListener implements PropertyUpdat
         // reset formatter cache on shared property update
         this.cacheManager.getCacheNames().stream()
                 .filter(namedCachesToClear::contains)
-                .map(cacheName -> this.cacheManager.getCache(cacheName))
+                .map(this.cacheManager::getCache)
                 .filter(Objects::nonNull)
                 .forEach(Cache::clear);
     }
-- 
GitLab