Skip to content

Commit

Permalink
Merge pull request #37 from swisscom/feature/33-action-runner-scripts
Browse files Browse the repository at this point in the history
Feature/33 action runner scripts
  • Loading branch information
SgiobairOg authored Feb 10, 2025
2 parents 1b67323 + eda667b commit 2481459
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 1 deletion.
54 changes: 54 additions & 0 deletions src/main/frontend/model/samples/change-value.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"logLevel": "info",
"hops": [
{
"type": "each",
"expression": "args.transformRulesFile",
"hops": [
{
"type": "runScript",
"code": "/*\n The transformRulesFile file should be in the form <oldValue>;<newValue> ex.:\n oldtext1;newtext1\n oldtext2;newtext2\n*/\n\nsplit = str:split(vals, ';');\n\nif(arr:getLength(split) == 2) {\n oldValue = split[0];\n newValue = split[1];\n} else {\n oldValue = \"\";\n newValue = \"\";\n}\n",
"extension": "jexl",
"putLocalsBackIntoScope": true
},
{
"type": "nodeQuery",
"query": "SELECT * FROM [nt:base] AS node\nWHERE ISDESCENDANTNODE([${args.path}]) AND node.* LIKE '%${oldValue}%'",
"queryType": "JCR-SQL2",
"hops": [
{
"type": "each",
"expression": "node.properties",
"hops": [
{
"type": "runScript",
"code": "propertyValue = jcr:vals(node,property.name);\nnewPropertyValue = propertyValue;\nflag = false;\nif (propertyValue != null) {\n if (arr:getLength(propertyValue) == 1) {\n // Handle case when propertyValue is a single string\n if (propertyValue[0].class.name == 'java.lang.String' && propertyValue[0].contains(oldValue)) {\n newPropertyValue = propertyValue[0].replaceAll(oldValue, newValue);\n flag = true;\n }\n } else {\n // Handle case when propertyValue is an array (or iterable)\n newPropertyValue = [...];\n for (item : propertyValue){\n if (item.class.name == 'java.lang.String' && item.contains(oldValue)) {\n newItem = item.replaceAll(oldValue, newValue);\n newPropertyValue.add(newItem);\n flag = true;\n }else{\n newPropertyValue.add(item);\n }\n }\n }\n}\n",
"extension": "jexl",
"putLocalsBackIntoScope": true
},
{
"type": "filterNode",
"expression": "flag == true",
"hops": [
{
"type": "setProperty",
"conflict": "force",
"propertyName": "${property.name}",
"value": "newPropertyValue"
}
]
}
],
"iterator": "property"
}
]
}
],
"iterator": "vals"
}
],
"parameters": [
{ "name": "path", "defaultValue": "/content/project/de", "type": "text", "evaluation": "STRING" },
{ "name": "transformRulesFile", "defaultValue": "", "type": "file", "evaluation": "LINES" }
]
}
11 changes: 10 additions & 1 deletion src/main/frontend/model/samples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import renameProperty from './rename-property.json';
import addOrReplaceProperty from './add-or-replace-property.json';
import addNode from './add-node.json';
import renameNode from './rename-node.json';

import valueFinder from './value-finder.json';
import changePropertyValue from './change-value.json';
import { Hop, HOP_DEFINITIONS } from '../hops';
import { Script } from '../Script';

Expand Down Expand Up @@ -85,6 +86,14 @@ export const SAMPLES: Sample[] = [
config: renameProperty as Script,
label: 'Sample: Rename property',
},
{
config: valueFinder as Script,
label: 'Sample: Value Finder',
},
{
config: changePropertyValue as Script,
label: 'Sample: Change Property Value',
},
{
config: addNode as Script,
label: 'Sample: Add node',
Expand Down
87 changes: 87 additions & 0 deletions src/main/frontend/model/samples/value-finder.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"logLevel": "info",
"hops": [
{
"type": "declare",
"declarations": {
"output": "file:csv(\"value_finder_results\")"
}
},
{
"type": "runScript",
"code": "output.line(\"path\", \"property\", \"value\");",
"extension": "jexl",
"putLocalsBackIntoScope": true
},
{
"type": "nodeQuery",
"query": "SELECT * FROM [nt:base] AS node WHERE ISDESCENDANTNODE([${args.path}])\n${(args.resourceTypeInclude != null && args.resourceTypeInclude.trim() != '') ? ` AND node.[sling:resourceType] LIKE \"${args.resourceTypeInclude}\"` : ''}\n${(args.resourceTypeExclude != null && args.resourceTypeExclude.trim() != '') ? ` AND node.[sling:resourceType] LIKE \"${args.resourceTypeExclude}\"` : ''}",
"queryType": "JCR-SQL2",
"hops": [
{
"type": "each",
"expression": "node.properties",
"hops": [
{
"type": "filterNode",
"expression": "str:contains(property.name, args.propertyName)",
"hops": [
{
"type": "declare",
"declarations": {
"joinedPropertyValue": "str:join(jcr:vals(node, property.name))"
}
},
{
"type": "filterNode",
"expression": "str:contains(joinedPropertyValue, args.propertyValue)",
"hops": [
{
"type": "runScript",
"code": "output.line(node.path, property.name, joinedPropertyValue);",
"extension": "jexl",
"putLocalsBackIntoScope": true
}
]
}
]
}
],
"iterator": "property"
}
]
}
],
"parameters": [
{
"name": "path",
"defaultValue": "/content/project/de",
"type": "text",
"evaluation": "STRING"
},
{
"name": "resourceTypeInclude",
"defaultValue": "",
"type": "text",
"evaluation": "STRING"
},
{
"name": "resourceTypeExclude",
"defaultValue": "",
"type": "text",
"evaluation": "STRING"
},
{
"name": "propertyName",
"defaultValue": "textIsTrue",
"type": "text",
"evaluation": "STRING"
},
{
"name": "propertyValue",
"defaultValue": "false",
"type": "text",
"evaluation": "STRING"
}
]
}

0 comments on commit 2481459

Please sign in to comment.