Class TrackingCodeBuilder
Provides a unique random tracking code builder.
The build method will ensure the tracking number is globally unique across both transaction and transaction history
tables. If an existing tracking code is found, the build method will continue to generate a new tracking codes
until it has created a globally unique value. If a unique value has not been generated after 100 attempts a
IllegalArgumentException
will be thrown.
Examples
Please find the tracking code builder examples below.
Create Transaction Tracking Code
The example below is creating an unique transaction tracking code and prefixing it with the form code and postfixing it with a test environment indicator.
class FluentTrackingNumberService { String invoke(SvcDef svcDef, Form form, HttpServletRequest request, User user) { return new TrackingCodeBuilder() .setPrefix(form.formCode + "-") .setPostfix("-TEST") .build() } }
- Since:
- 5.0.0
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionbuild()
Return a new random transaction tracking code (tracking number) for the specified properties.setPostfix
(String postfix) Set the tracking code value to postfix random component.Set the tracking code value to prefix random component.setRandomChars
(String randomChars) Set the character values to be used in the random component.setRandomLength
(int randomLength) Set the random component character length.boolean
trackingCodeExists
(String trackingCode) Return true if the specified tracking code is already used.
-
Constructor Details
-
TrackingCodeBuilder
public TrackingCodeBuilder()
-
-
Method Details
-
setPrefix
Set the tracking code value to prefix random component.- Parameters:
prefix
- the tracking code prefix- Returns:
- the tracking code builder
-
setPostfix
Set the tracking code value to postfix random component.- Parameters:
postfix
- the tracking code post- Returns:
- the tracking code builder
-
setRandomLength
Set the random component character length. The default length is 7.- Parameters:
randomLength
- the random component character length- Returns:
- the tracking code builder
-
setRandomChars
Set the character values to be used in the random component. The default values include: BCDFGHJKLMNPQRSTVWXYZ23456789- Parameters:
randomChars
- the character values to be used in the random component.- Returns:
- the tracking code builder
-
trackingCodeExists
Return true if the specified tracking code is already used.- Parameters:
trackingCode
- the tracking code value to check (must not be null or an empty string)- Returns:
- true if the tracking code value is already in use or there was an error during lookup
-
build
Return a new random transaction tracking code (tracking number) for the specified properties.
This method will ensure the tracking number is globally unique across both transaction and transaction history tables. If an existing tracking code is found, this method will continue to generate a new tracking codes until a globally unique value is found. If this method cannot generate a unique value after 10 attempts it will throw an
IllegalArgumentException
If a unique tracking code is found, the code is also stored in the tracking_number table that records all unique codes.
- Returns:
- new random transaction tracking code (tracking number) for the specified properties.
-