From e795451c55294833dc51d0cf294690c8d70c43f3 Mon Sep 17 00:00:00 2001
From: RIHTARSIC Joze <joze.rihtarsic@ext.ec.europa.eu>
Date: Sat, 20 May 2023 17:56:30 +0200
Subject: [PATCH] Fix subresource save with the same identifiers

---
 .../resource-dialog/subresource-dialog.component.ts    |  7 ++-----
 .../ec/edelivery/smp/data/model/doc/DBSubresource.java |  3 ++-
 .../smp/services/ui/UISubresourceService.java          | 10 ++++------
 .../smp/error/AbstractErrorControllerAdvice.java       |  5 +++++
 4 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/smp-angular/src/app/edit/edit-resources/subresource-panel/resource-dialog/subresource-dialog.component.ts b/smp-angular/src/app/edit/edit-resources/subresource-panel/resource-dialog/subresource-dialog.component.ts
index daa7301f8..75f730993 100644
--- a/smp-angular/src/app/edit/edit-resources/subresource-panel/resource-dialog/subresource-dialog.component.ts
+++ b/smp-angular/src/app/edit/edit-resources/subresource-panel/resource-dialog/subresource-dialog.component.ts
@@ -10,9 +10,6 @@ import {ResourceDefinitionRo} from "../../../../system-settings/admin-extension/
 import {EditGroupService} from "../../../edit-group/edit-group.service";
 import {SubresourceRo} from "../../../../common/model/subresource-ro.model";
 import {EditResourceService} from "../../edit-resource.service";
-
-
-
 @Component({
   templateUrl: './subresource-dialog.component.html',
   styleUrls: ['./subresource-dialog.component.css']
@@ -56,8 +53,8 @@ export class SubresourceDialogComponent {
 
   get subresource(): SubresourceRo {
     let entity = {...this._subresource};
-    entity.identifierScheme = this.resourceForm.get('identifierValue').value;
-    entity.identifierValue = this.resourceForm.get('identifierScheme').value;
+    entity.identifierScheme = this.resourceForm.get('identifierScheme').value;
+    entity.identifierValue = this.resourceForm.get('identifierValue').value;
     entity.subresourceTypeIdentifier = this.resourceForm.get('subresourceTypeIdentifier').value;
     return entity;
   }
diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/data/model/doc/DBSubresource.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/data/model/doc/DBSubresource.java
index eb2a4594a..1001f1aca 100644
--- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/data/model/doc/DBSubresource.java
+++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/data/model/doc/DBSubresource.java
@@ -46,7 +46,8 @@ import static eu.europa.ec.edelivery.smp.data.dao.QueryNames.*;
 
 @NamedQuery(name = QUERY_SUBRESOURCE_BY_IDENTIFIER_RESOURCE_ID , query = "SELECT d FROM DBSubresource d WHERE d.resource.id = :resource_id " +
         " AND d.identifierValue=:subresource_identifier " +
-        " AND d.identifierScheme=:subresource_scheme"
+        " AND (:subresource_scheme IS NULL AND d.identifierScheme IS NULL " +
+        " OR d.identifierScheme = :subresource_scheme)"
 )
 
 @NamedQuery(name = QUERY_SUBRESOURCE_BY_RESOURCE_ID , query = "SELECT d FROM DBSubresource d WHERE d.resource.id = :resource_id order by id asc")
diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ui/UISubresourceService.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ui/UISubresourceService.java
index 2b2754f65..4aaa10852 100644
--- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ui/UISubresourceService.java
+++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ui/UISubresourceService.java
@@ -93,24 +93,22 @@ public class UISubresourceService {
         if (!optRedef.isPresent()) {
             throw new SMPRuntimeException(ErrorCode.INVALID_REQUEST, ACTION_SUBRESOURCE_CREATE, "Subresource definition [" + subResourceRO.getSubresourceTypeIdentifier() + "] does not exist!");
         }
-        Identifier docId = identifierService.normalizeDocument(subResourceRO.getIdentifierScheme(), subResourceRO.getIdentifierValue());
+        Identifier docId = identifierService.normalizeDocument(subResourceRO.getIdentifierScheme(),
+                subResourceRO.getIdentifierValue());
         Optional<DBSubresource> exists= subresourceDao.getSubResourcesForResource(docId, resParent);
         if (exists.isPresent()) {
             throw new SMPRuntimeException(ErrorCode.INVALID_REQUEST, ACTION_SUBRESOURCE_CREATE, "Subresource definition [val:" + docId.getValue() + " scheme:" + docId.getScheme() + "] already exists for the resource!");
         }
 
         DBSubresource subresource = new DBSubresource();
-        subresource.setIdentifierScheme(docId.getValue());
-        subresource.setIdentifierValue(docId.getScheme());
+        subresource.setIdentifierScheme(docId.getScheme());
+        subresource.setIdentifierValue(docId.getValue());
         subresource.setResource(resParent);
         subresource.setSubresourceDef(optRedef.get());
         DBDocument document = createDocumentForSubresourceDef(optRedef.get());
         subresource.setDocument(document);
         subresourceDao.persist(subresource);
         // create first member as admin user
-
-
-
         return conversionService.convert(subresource, SubresourceRO.class);
     }
 
diff --git a/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/error/AbstractErrorControllerAdvice.java b/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/error/AbstractErrorControllerAdvice.java
index 454cbe57a..a2e4d7e4d 100644
--- a/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/error/AbstractErrorControllerAdvice.java
+++ b/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/error/AbstractErrorControllerAdvice.java
@@ -5,6 +5,7 @@ import eu.europa.ec.edelivery.smp.data.ui.exceptions.ErrorResponseRO;
 import eu.europa.ec.edelivery.smp.error.exceptions.SMPResponseStatusException;
 import eu.europa.ec.edelivery.smp.exceptions.BadRequestException;
 import eu.europa.ec.edelivery.smp.exceptions.ErrorBusinessCode;
+import eu.europa.ec.edelivery.smp.exceptions.MalformedIdentifierException;
 import eu.europa.ec.edelivery.smp.exceptions.SMPRuntimeException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -38,6 +39,10 @@ abstract class AbstractErrorControllerAdvice {
             BadRequestException ex = (BadRequestException)runtimeException;
             response = buildAndLog(UNPROCESSABLE_ENTITY, ex.getErrorBusinessCode(), ex.getMessage(), ex);
         }
+        else if (runtimeException instanceof MalformedIdentifierException){
+            MalformedIdentifierException ex = (MalformedIdentifierException)runtimeException;
+            response = buildAndLog(BAD_REQUEST, ErrorBusinessCode.FORMAT_ERROR, ex.getMessage(), ex);
+        }
         else {
             response = buildAndLog(INTERNAL_SERVER_ERROR, TECHNICAL, "Unexpected technical error occurred.", runtimeException);
         }
-- 
GitLab