public static String incrementBase64Local(String base64Number) {
String encodeTable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_';
Integer indexOfLastChar = base64Number.length() - 1;
Integer lastCharOfBase64Number = base64Number.charAt(indexOfLastChar);
Integer index = encodeTable.indexOfChar(lastCharOfBase64Number);
if (index == 63) {
if (base64Number.length() == 1) {
return '10';
}
String firstCharacters = incrementBase64Local(base64Number.left(indexOfLastChar));
return firstCharacters + '0';
}
base64Number = base64Number.left(indexOfLastChar) + encodeTable.substring(index + 1, index + 2);
return base64Number;
}
// Reset the resource numbering.
B25__System_Setting__c resourceIdSetting = [SELECT B25__String_Value__c FROM B25__System_Setting__c WHERE Name = 'ResourceIdNumber' FOR UPDATE];
resourceIdSetting.B25__String_Value__c = '00000';
Set<Id> parentIds = new Set<Id>{null}; // first layer
while (!parentIds.isEmpty()) {
List<B25__Resource__c> resources = [SELECT B25__Materialized_Path__c, B25__Booker25_Id__c, B25__Parent__r.B25__Materialized_Path__c, B25__Parent__r.B25__Booker25_Id__c FROM B25__Resource__c WHERE B25__Parent__c IN:parentIds AND B25__IsVirtual__c = false];
// Update resource ids
String currentResourceId = resourceIdSetting.B25__String_Value__c;
for (B25__Resource__c resource : resources) {
resource.B25__Booker25_Id__c = currentResourceId;
currentResourceId = incrementBase64Local(currentResourceId);
if (currentResourceId.length() > 5) {
System.assert(false, 'Too many resources');
}
}
resourceIdSetting.B25__String_Value__c = currentResourceId;
for (B25__Resource__c resource : resources) {
if (resource.B25__Parent__c == null) {
resource.B25__Materialized_Path__c = '';
continue;
}
resource.B25__Materialized_Path__c = resource.B25__Parent__r.B25__Materialized_Path__c != null
? resource.B25__Parent__r.B25__Materialized_Path__c + resource.B25__Parent__r.B25__Booker25_Id__c
: resource.B25__Parent__r.B25__Booker25_Id__c;
}
B25.Trigger_Resource_c.blockExecution = true;
Database.update(resources);
B25.Trigger_Resource_c.blockExecution = false;
parentIds = new Set<Id>(new Map<Id, B25__Resource__c>(resources).keySet());
}
Database.update(resourceIdSetting);
|