Skip to content

[RAD-5674] Creating a script to change the Role XID’s #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions change-role-xid.js
Original file line number Diff line number Diff line change
@@ -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);