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

jsinovassinnaik pushed a commit to branch UNOMI-847
in repository https://gitbox.apache.org/repos/asf/unomi.git

commit a9ccb2abafb08ead86d7bb83a9499ea347cd3f81
Author: jsinovassin <jsinovassinn...@jahia.com>
AuthorDate: Wed Oct 9 11:46:26 2024 +0100

    UNOMI-847: fix groovy action deletion
---
 .../groovy/actions/rest/GroovyActionsEndPoint.java |  5 +--
 .../apache/unomi/groovy/actions/GroovyAction.java  |  5 +--
 .../actions/services/GroovyActionsService.java     |  7 ++--
 .../services/impl/GroovyActionsServiceImpl.java    | 38 ++++++++--------------
 4 files changed, 24 insertions(+), 31 deletions(-)

diff --git 
a/extensions/groovy-actions/rest/src/main/java/org/apache/unomi/groovy/actions/rest/GroovyActionsEndPoint.java
 
b/extensions/groovy-actions/rest/src/main/java/org/apache/unomi/groovy/actions/rest/GroovyActionsEndPoint.java
index ae2236c01..a7e6986d8 100644
--- 
a/extensions/groovy-actions/rest/src/main/java/org/apache/unomi/groovy/actions/rest/GroovyActionsEndPoint.java
+++ 
b/extensions/groovy-actions/rest/src/main/java/org/apache/unomi/groovy/actions/rest/GroovyActionsEndPoint.java
@@ -85,10 +85,11 @@ public class GroovyActionsEndPoint {
      * Deletes the rule identified by the specified identifier.
      *
      * @param actionId the identifier of the groovy action that we want to 
delete
+     * @return true if the action was successfully deleted, false otherwise
      */
     @DELETE
     @Path("/{actionId}")
-    public void remove(@PathParam("actionId") String actionId) {
-        groovyActionsService.remove(actionId);
+    public boolean remove(@PathParam("actionId") String actionId) {
+        return groovyActionsService.remove(actionId);
     }
 }
diff --git 
a/extensions/groovy-actions/services/src/main/java/org/apache/unomi/groovy/actions/GroovyAction.java
 
b/extensions/groovy-actions/services/src/main/java/org/apache/unomi/groovy/actions/GroovyAction.java
index 842eb6c8e..ddb48c756 100644
--- 
a/extensions/groovy-actions/services/src/main/java/org/apache/unomi/groovy/actions/GroovyAction.java
+++ 
b/extensions/groovy-actions/services/src/main/java/org/apache/unomi/groovy/actions/GroovyAction.java
@@ -34,10 +34,11 @@ public class GroovyAction extends MetadataItem {
     public GroovyAction() {
     }
 
-    public GroovyAction(String name, String script) {
-        super(new Metadata(name));
+    public GroovyAction(String id, String name, String script) {
+        super(new Metadata(id));
         this.name = name;
         this.script = script;
+
     }
 
     public String getName() {
diff --git 
a/extensions/groovy-actions/services/src/main/java/org/apache/unomi/groovy/actions/services/GroovyActionsService.java
 
b/extensions/groovy-actions/services/src/main/java/org/apache/unomi/groovy/actions/services/GroovyActionsService.java
index 4b6d54505..91de3211f 100644
--- 
a/extensions/groovy-actions/services/src/main/java/org/apache/unomi/groovy/actions/services/GroovyActionsService.java
+++ 
b/extensions/groovy-actions/services/src/main/java/org/apache/unomi/groovy/actions/services/GroovyActionsService.java
@@ -29,17 +29,18 @@ public interface GroovyActionsService {
     /**
      * Save a groovy action from a groovy file
      *
-     * @param actionName   actionName
+     * @param fileName     fileName
      * @param groovyScript script to save
      */
-    void save(String actionName, String groovyScript);
+    void save(String fileName, String groovyScript);
 
     /**
      * Remove a groovy action
      *
      * @param id of the action to remove
+     * @return true if the action was successfully deleted, false otherwise
      */
-    void remove(String id);
+    boolean remove(String id);
 
     /**
      * Get a groovy code source object by an id
diff --git 
a/extensions/groovy-actions/services/src/main/java/org/apache/unomi/groovy/actions/services/impl/GroovyActionsServiceImpl.java
 
b/extensions/groovy-actions/services/src/main/java/org/apache/unomi/groovy/actions/services/impl/GroovyActionsServiceImpl.java
index bee011cd6..10b41363a 100644
--- 
a/extensions/groovy-actions/services/src/main/java/org/apache/unomi/groovy/actions/services/impl/GroovyActionsServiceImpl.java
+++ 
b/extensions/groovy-actions/services/src/main/java/org/apache/unomi/groovy/actions/services/impl/GroovyActionsServiceImpl.java
@@ -144,7 +144,7 @@ public class GroovyActionsServiceImpl implements 
GroovyActionsService {
      * script will be parsed with the GroovyShell (groovyShell.parse(...)), 
the action will extends the base script, so the functions
      * could be called
      *
-     * @throws IOException
+     * @throws IOException if the base script is not found
      */
     private void loadBaseScript() throws IOException {
         URL groovyBaseScriptURL = 
bundleContext.getBundle().getEntry("META-INF/base/BaseScript.groovy");
@@ -179,14 +179,15 @@ public class GroovyActionsServiceImpl implements 
GroovyActionsService {
     }
 
     @Override
-    public void save(String actionName, String groovyScript) {
-        GroovyCodeSource groovyCodeSource = buildClassScript(groovyScript, 
actionName);
+    public void save(String fileName, String groovyScript) {
+        GroovyCodeSource groovyCodeSource = buildClassScript(groovyScript, 
fileName);
         try {
-            
saveActionType(groovyShell.parse(groovyCodeSource).getClass().getMethod("execute").getAnnotation(Action.class));
-            saveScript(actionName, groovyScript);
-            LOGGER.info("The script {} has been loaded.", actionName);
+            Action action = 
groovyShell.parse(groovyCodeSource).getClass().getMethod("execute").getAnnotation(Action.class);
+            saveActionType(action);
+            persistenceService.save(new GroovyAction(action.id(), 
action.name().isEmpty() ? action.id(): action.name(), groovyScript));
+            LOGGER.info("The script {} has been loaded and the action {} has 
been persisted.", fileName, action.id());
         } catch (NoSuchMethodException e) {
-            LOGGER.error("Failed to save the script {}", actionName, e);
+            LOGGER.error("Failed to save the script {}", fileName, e);
         }
     }
 
@@ -196,7 +197,7 @@ public class GroovyActionsServiceImpl implements 
GroovyActionsService {
      * @param action Annotation containing the values to save
      */
     private void saveActionType(Action action) {
-        Metadata metadata = new Metadata(null, action.id(), 
action.name().equals("") ? action.id() : action.name(), action.description());
+        Metadata metadata = new Metadata(null, action.id(), 
action.name().isEmpty() ? action.id() : action.name(), action.description());
         metadata.setHidden(action.hidden());
         metadata.setReadOnly(true);
         metadata.setSystemTags(new HashSet<>(asList(action.systemTags())));
@@ -210,7 +211,7 @@ public class GroovyActionsServiceImpl implements 
GroovyActionsService {
     }
 
     @Override
-    public void remove(String id) {
+    public boolean remove(String id) {
         if (groovyCodeSourceMap.containsKey(id)) {
             try {
                 definitionsService.removeActionType(
@@ -218,8 +219,9 @@ public class GroovyActionsServiceImpl implements 
GroovyActionsService {
             } catch (NoSuchMethodException e) {
                 LOGGER.error("Failed to delete the action type for the id {}", 
id, e);
             }
-            persistenceService.remove(id, GroovyAction.class);
+            return persistenceService.remove(id, GroovyAction.class);
         }
+        return false;
     }
 
     @Override
@@ -238,27 +240,15 @@ public class GroovyActionsServiceImpl implements 
GroovyActionsService {
         return new GroovyCodeSource(groovyScript, actionName, 
"/groovy/script");
     }
 
-    private void saveScript(String actionName, String script) {
-        GroovyAction groovyScript = new GroovyAction(actionName, script);
-        persistenceService.save(groovyScript);
-        LOGGER.info("The script {} has been persisted.", actionName);
-    }
-
     private void refreshGroovyActions() {
         Map<String, GroovyCodeSource> refreshedGroovyCodeSourceMap = new 
HashMap<>();
         
persistenceService.getAllItems(GroovyAction.class).forEach(groovyAction -> 
refreshedGroovyCodeSourceMap
-                .put(groovyAction.getName(), 
buildClassScript(groovyAction.getScript(), groovyAction.getName())));
+                .put(groovyAction.getMetadata().getId(), 
buildClassScript(groovyAction.getScript(), 
groovyAction.getMetadata().getId())));
         groovyCodeSourceMap = refreshedGroovyCodeSourceMap;
     }
 
     private void initializeTimers() {
-        TimerTask task = new TimerTask() {
-            @Override
-            public void run() {
-                refreshGroovyActions();
-            }
-        };
-        scheduledFuture = 
schedulerService.getScheduleExecutorService().scheduleWithFixedDelay(task, 0, 
config.services_groovy_actions_refresh_interval(),
+        scheduledFuture = 
schedulerService.getScheduleExecutorService().scheduleWithFixedDelay(this::refreshGroovyActions,
 0, config.services_groovy_actions_refresh_interval(),
                 TimeUnit.MILLISECONDS);
     }
 }

Reply via email to