Skip to main content
Groovy Script - Auto Incrementing Revision
Updated over a month ago

Description: Sample groovy script to increment revisions alphabetically or numerically.

Context: Document

Remark: Supported in Post Mode Only

Script:

// If Document is first revision then script will reset revision with (-) and discards which was applied.

// If Previous revision is (-) then script will set revision (A).

// If previous revision is (A) then it will incremented till Z. and for next revision AA will be assiged and incremented till ZZ.

// If previous revision is (1) then it will be incremented to 2.

def execute() {

DocumentVO document = documentService.getDocumentDetails();

HashMap<Object, Object> optionMap = new HashMap();

optionMap.put(RevisionOption.GET_REVISIONS, RevisionOption.PREVIOUS_REVISION);

optionMap.put(RevisionDetailsOption.BASIC_DETAILS, true);

optionMap.put(RevisionOption.INCLUDE_DEACTIVATED, true);

ArrayList<DocumentVO> documentsList = new ArrayList<DocumentVO>();

Map<String, DocumentVO> revisionsMap =

documentService.getDocumentRevisionDetail(document, optionMap);

if (revisionsMap.size() == 1) {

String calculatedVer =

String.valueOf(Integer.parseInt(document.getVer())-1);

String revision = getIncrementedRevision(revisionsMap.get(calculatedVer).getRevision(),

document.getRevision());

if (!document.getRevision().equals(revision)) {

document.setRevision(revision);

documentsList.add(document);

}

} else {

document.setRevision("-");

documentsList.add(document);

}

if (!documentsList.isEmpty())

documentService.updateDocuments(documentsList);

}

public String getIncrementedRevision(String previousRev, String currnetRev) {

String revision = currnetRev;

if (previousRev.equals("-")) {

revision = "A";

} else if (previousRev.matches("[A-Z]{1,2}")) {

revision = incrementAlphabets(previousRev);

} else if (previousRev.matches("[0-9]{1,}")) {

revision = incrementNumbers(previousRev);

}

return revision;

}

public static String incrementAlphabets(String revision) {

if (revision.length() == 1) {

if (revision.charAt(0) == 'Z') {

return "AA";

} else {

return String.valueOf((char)(revision.charAt(0) + 1));

}

} else if (revision.length() == 2) {

if("ZZ".equals(revision)) {

return revision;

} else if (revision.charAt(1) == 'Z') {

return String.valueOf((char)(revision.charAt(0) + 1)) + "A";

} else {

return String.valueOf(revision.charAt(0)) + String.valueOf((char)(revision.charAt(1) + 1));

}

} else {

return revision;

}

}

public static String incrementNumbers(String revision) {

return String.format("%0" + revision.length() + "d",

Integer.parseInt(revision) + 1);

}



Did this answer your question?