This is an automated email from the ASF dual-hosted git repository.

zehnder pushed a commit to branch fork/dev
in repository https://gitbox.apache.org/repos/asf/streampipes.git

commit af2f621311f20b4098d8411d646a21a61d0d9232
Author: Philipp Zehnder <[email protected]>
AuthorDate: Mon Jan 29 10:19:47 2024 +0100

    fix(#2144): Fix label generation for migrated elements
---
 .../migration/MigrateExtensionsResource.java       | 83 ++++++++++++++--------
 1 file changed, 54 insertions(+), 29 deletions(-)

diff --git 
a/streampipes-rest-extensions/src/main/java/org/apache/streampipes/rest/extensions/migration/MigrateExtensionsResource.java
 
b/streampipes-rest-extensions/src/main/java/org/apache/streampipes/rest/extensions/migration/MigrateExtensionsResource.java
index 07fc157f6c..749fba5749 100644
--- 
a/streampipes-rest-extensions/src/main/java/org/apache/streampipes/rest/extensions/migration/MigrateExtensionsResource.java
+++ 
b/streampipes-rest-extensions/src/main/java/org/apache/streampipes/rest/extensions/migration/MigrateExtensionsResource.java
@@ -21,6 +21,7 @@ package org.apache.streampipes.rest.extensions.migration;
 import org.apache.streampipes.extensions.api.extractor.IParameterExtractor;
 import org.apache.streampipes.extensions.api.migration.IModelMigrator;
 import org.apache.streampipes.extensions.management.init.DeclarersSingleton;
+import org.apache.streampipes.extensions.management.locales.LabelGenerator;
 import org.apache.streampipes.model.base.VersionedNamedStreamPipesEntity;
 import org.apache.streampipes.model.extensions.migration.MigrationRequest;
 import org.apache.streampipes.model.migration.MigrationResult;
@@ -31,12 +32,13 @@ import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.IOException;
 import java.util.Optional;
 
 public abstract class MigrateExtensionsResource<
-        T extends VersionedNamedStreamPipesEntity,
-        ExT extends IParameterExtractor,
-        MmT extends IModelMigrator<T, ExT>> extends AbstractExtensionsResource 
{
+    T extends VersionedNamedStreamPipesEntity,
+    ExT extends IParameterExtractor,
+    MmT extends IModelMigrator<T, ExT>> extends AbstractExtensionsResource {
 
   private static final Logger LOG = 
LoggerFactory.getLogger(MigrateExtensionsResource.class);
 
@@ -49,11 +51,14 @@ public abstract class MigrateExtensionsResource<
    * @return Optional model migrator which is empty in case no appropriate 
migrator is found among the registered.
    */
   public Optional<MmT> getMigrator(ModelMigratorConfig modelMigratorConfig) {
-    return 
DeclarersSingleton.getInstance().getServiceDefinition().getMigrators()
-            .stream()
-            .filter(modelMigrator -> 
modelMigrator.config().equals(modelMigratorConfig))
-            .map(modelMigrator -> (MmT) modelMigrator)
-            .findFirst();
+    return DeclarersSingleton.getInstance()
+                             .getServiceDefinition()
+                             .getMigrators()
+                             .stream()
+                             .filter(modelMigrator -> modelMigrator.config()
+                                                                   
.equals(modelMigratorConfig))
+                             .map(modelMigrator -> (MmT) modelMigrator)
+                             .findFirst();
   }
 
   /**
@@ -62,6 +67,7 @@ public abstract class MigrateExtensionsResource<
    * The result is always part of the response.
    * Independent, of the migration outcome, the returned response always has 
OK as status code.
    * It is the responsibility of the recipient to interpret the migration 
result and act accordingly.
+   *
    * @param migrationRequest Request that contains both the pipeline element 
to be migrated and the migration config.
    * @return A response with status code ok, that contains a migration result 
reflecting the outcome of the operation.
    */
@@ -70,10 +76,11 @@ public abstract class MigrateExtensionsResource<
     var pipelineElementDescription = migrationRequest.migrationElement();
     var migrationConfig = migrationRequest.modelMigratorConfig();
 
-    LOG.info("Received migration request for pipeline element '{}' to migrate 
from version {} to {}",
-            pipelineElementDescription.getElementId(),
-            migrationConfig.fromVersion(),
-            migrationConfig.toVersion()
+    LOG.info(
+        "Received migration request for pipeline element '{}' to migrate from 
version {} to {}",
+        pipelineElementDescription.getElementId(),
+        migrationConfig.fromVersion(),
+        migrationConfig.toVersion()
     );
 
     var migratorOptional = getMigrator(migrationConfig);
@@ -84,23 +91,24 @@ public abstract class MigrateExtensionsResource<
     }
     LOG.error("Migrator for migration config {} could not be found. Migration 
is cancelled.", migrationConfig);
     return MigrationResult.failure(
-            pipelineElementDescription,
-            String.format(
-                    "The given migration config '%s' could not be mapped to a 
registered migrator.",
-                    migrationConfig
-            )
+        pipelineElementDescription,
+        String.format(
+            "The given migration config '%s' could not be mapped to a 
registered migrator.",
+            migrationConfig
+        )
     );
   }
 
   /**
    * Executes the migration for the given pipeline element based on the given 
migrator.
-   * @param migrator migrator that executes the migration
+   *
+   * @param migrator                   migrator that executes the migration
    * @param pipelineElementDescription pipeline element to be migrated
    * @return the migration result containing either the migrated element or 
the original one in case of a failure
    */
   protected MigrationResult<T> executeMigration(
-          MmT migrator,
-          T pipelineElementDescription
+      MmT migrator,
+      T pipelineElementDescription
   ) {
 
     var extractor = getPropertyExtractor(pipelineElementDescription);
@@ -111,12 +119,18 @@ public abstract class MigrateExtensionsResource<
       if (result.success()) {
         LOG.info("Migration successfully finished.");
 
+        var migratedElement = result.element();
+
+        migratedElement = updateLabels(migratedElement);
+
         // Since adapter migration was successful, version can be adapted to 
the target version.
         // this step is explicitly performed here and not left to the 
migration itself to
         // prevent leaving this step out
-        var migratedProcessor = result.element();
-        migratedProcessor.setVersion(migrator.config().toVersion());
-        return MigrationResult.success(migratedProcessor);
+        migratedElement.setVersion(
+            migrator.config()
+                    .toVersion());
+
+        return MigrationResult.success(migratedElement);
 
       } else {
         LOG.error("Migration failed with the following reason: {}", 
result.message());
@@ -126,16 +140,27 @@ public abstract class MigrateExtensionsResource<
       }
     } catch (RuntimeException e) {
       LOG.error("An unexpected exception caused the migration to fail - "
-              + "sending exception report in migration result");
+                    + "sending exception report in migration result");
       return MigrationResult.failure(
-              pipelineElementDescription,
-              String.format(
-                      "Migration failed due to an unexpected exception: %s",
-                      StringUtils.join(e.getStackTrace(), "\n")
-              )
+          pipelineElementDescription,
+          String.format(
+              "Migration failed due to an unexpected exception: %s",
+              StringUtils.join(e.getStackTrace(), "\n")
+          )
       );
     }
   }
 
+  private static <T extends VersionedNamedStreamPipesEntity> T updateLabels(T 
migratedElement) {
+    if (migratedElement.isIncludesLocales()) {
+      try {
+        migratedElement = new 
LabelGenerator<>(migratedElement).generateLabels();
+      } catch (IOException e) {
+        LOG.error("Failed to generate labels for migrated element: 
%s".formatted(migratedElement.getAppId()), e);
+      }
+    }
+    return migratedElement;
+  }
+
   protected abstract ExT getPropertyExtractor(T pipelineElementDescription);
 }

Reply via email to