From 7518677debc4f1d880fab41f1261a4234515a1a7 Mon Sep 17 00:00:00 2001 From: Francisco Cruz Date: Wed, 5 Mar 2025 11:19:22 -0600 Subject: [PATCH] RAD-5674 add script for change role xid --- change-role-xid.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 change-role-xid.js diff --git a/change-role-xid.js b/change-role-xid.js new file mode 100644 index 0000000..8590113 --- /dev/null +++ b/change-role-xid.js @@ -0,0 +1,46 @@ +/** + * This script replaces spaces on XID for underscore for roles + * Was tested MANGO 4.5.X + * Last update Mar 2025 + * + * This steps are: + * 1. Get all roles + * 2. Check if the XID has space + * 3. Replace spaces with underscore on XID + * 4. Update role + */ + +// imports +const Common = Java.type('com.serotonin.m2m2.Common'); +const RoleVO = Java.type('com.serotonin.m2m2.vo.role.RoleVO'); +const RoleDao = Java.type('com.serotonin.m2m2.db.dao.RoleDao'); + +const roleDao = Common.getBean(RoleDao.class); +const roleService = services.roleService; + +// Function to check if a string contains a space +function hasSpace(xid) { + return xid.includes(' '); +} + +// Get roles +var roles = roleService.list(); +var rolesUpdated = 0; +if (roles) { + for (var i = 0; i < roles.length; i++) { + var obj = roles[i]; + var xid = obj.getXid(); + + // update role if XID has spaces + if (hasSpace(xid)) { + const nameWithoutSpace = xid.replace(' ','_'); + const updated = new RoleVO(obj.getId(), nameWithoutSpace, obj.getName()); + roleDao.update(obj.getId(), updated); + console.log('Rol updated with XID: ', xid); + rolesUpdated++; + } + } +} else { + console.log("List of roles is undefined or null."); +} +console.log('Roles updated:', rolesUpdated); \ No newline at end of file