Skip to main content

Groovy Script - Stop the Upload if the Doc Ref doesn't match a Defined Pattern & Display a Message

Description: Sample Groovy Script to stop the upload if the document reference doesn't match a defined pattern and display a message.

Context: Document

Document Type: File

Remark: Accessible in 'Pre' task mode only.

Script:

[Pattern: Doc Ref should start with one and only one alphabet +
Minimum 2 digit number... it can be more +
A .(dot) +
Should end with minimum 1 digit number... it can be more.
E.g. A0001.555]
def execute(){
DocumentVO document = documentService.getDocumentDetails();
if (!document.getDocRef().matches("^[A-Za-z][0-9]{2,}[.][0-9]{1,}")) {
document.setResponseStatus(false);
document.setResponseMessage("DocRef pattern does not match with expected pattern : A001.555, please update.");
}
return document;
}

[To validate a Doc Ref by applying a numbering rule]
def execute(){
DocumentVO document = documentService.getDocumentDetails();

String separator = "-";
String Project_Code = "A1234";
String Originator = "[A-Za-z]{3}";
String Site_Code = "(0101|0102|0103|0104|0105|0106)";
String Asset_Code = "(001|002|003|004|A12|B23|C24)";
String Discipline = "(ARCH|STR|INT|EXT|CVL|MECH)";
String Doc_Type = "(ABC|DEF|GHI|JKL)";
String Sequence = "[0-9]{4}";

String Doc_Ref_Rule = Project_Code + separator + Originator + separator + Site_Code + separator + Asset_Code + separator + Discipline + separator + Doc_Type + separator + Sequence;

if (!document.getDocRef().matches(Doc_Ref_Rule)) {
document.setResponseStatus(false);
document.setResponseMessage("DocRef pattern does not match with expected pattern : A1234-JOHN-0101-001-ARCH-ABC-0001, please update accordingly.");
}
return document;
}
Did this answer your question?