Tips & Tricks - Unique Number Generator
The unique number generator makes it possible to generate automatically incrementing numbers according to predefined criteria. In these cases, the number can consist of any required combination of parts (number, date, time, word). The best known examples of unique numbers are probably inventory numbers. When creating inventory in an Intrexx application, a consecutive number structured according to a defined syntax can be generated with the Unique Number Generator (e.g. Inv-2007-06-0001, Inv-2007-06-0002, etc.).
It also makes sense to use the unique number generator for orders, parts lists or customer numbers. Unique numbers can be generated via the data handler of a data field. A check is made before the record is saved, to determine which unique number in the previously saved data records has the highest allocated number at that point in time. When the data record is saved, a new number – the previous highest plus 1 – will be written to the data field. A sequential number assignment is guaranteed with this method. Even when the portal service is restarted, the highest unique number assigned is determined and used as the start number for the following numbers.
Automatically incrementing numbers can also be generated via Groovy. JavaScript and Velocity are not suitable for this because when the page is loaded, it is not certain whether it will also be saved.
The Unique Number Generator can be used in various locations, such as when a page is opened to create a new record. In this case, please note that a new number is always generated when the page is opened. If the process is canceled, i.e. the page is closed without saving the new data record, the number generated when it was opened is still used. This can lead to gaps in consecutive numbering.
To work around this, the UniqueNumberGenerator can be used when saving to the process. However, the new number is also displayed only after saving.
Configure the datahandler
First create a new application in the Portal Manager based on the "Basic application" application template. Then create a new data field in the contained data group. As a unique number can contain date, integer and string values, please only use data fields with the data type "Short text". Do not place a unique number in a system data field. Only use data fields that you have created yourself. Enter the title "Unique Number".
Show the data fields of the data group via the main menu "Edit / Show data fields". If you have not already done so, activate the options for experts.
Open the properties dialog of the unique number data field by double-clicking on the data field in the application structure and switch to the "Expert" tab. For the "datahandler" attribute, enter the value
de.uplanet.lucy.server.util.numbergenerator.SystemFieldNumberGenerator
and click on "OK".
You can now add the "Unique number" field to the view table on the "All entries" page. Save the application.
Create the UniqueNumber.xml file
The UniqueNumber.xml file contains the definition of all unique numbers in the current portal.
<?xml version="1.0" encoding="UTF-8"?>
<uniquenumbers>
<uniquenumber name="myUniqueNumber1" guid="guid" link-datafield="guid">
<part type="constant" value="UP-"/>
<part type="year" condition="true" format="yyyy"/>
<part type="month" condition="true" format="mm"/>
<part type="day" condition="true" format="dd"/>
<part type="constant" value="-"/>
<part type="number" format="###"/>
</uniquenumber>
</uniquenumbers>
Create a new file with a standard text editor, and copy the code above into the file. Amend the values below as follows:
-
name
Name of the relevant unique number (freely selectable, must be unique)
-
guid
GUID of the relevant unique number (freely selectable, must be unique)
-
link-datafield
GUID of the string data field where the unique number will be generated
-
<part .../>
Definition of a part of the unique number
Enter a name for your unique number in "name".
You can easily create a GUID for the unique number via the main menu "Extras / Create GUID".
To identify the GUID of the data field, where the unique number will be generated, select the data field in the application structure and press the F4 key.
In the UniqueNumber.xml file, you can define as many unique numbers as you require. For additional unique number definitions, copy this section
<uniquenumber name=" myUniqueNumber1" guid="guid" link-datafield="guid">...
</uniquenumber>
and replace the entries for ""name"", ""guid"", ""link-datafield"" and the ""part types"" with the corresponding new values.
In the ""part-type"" tags that follow, you can specify one part of the unique number in each tag. In our example here, the new number would begin with "INTREXX-", for example, followed by the four-digit year, the month, the day, a hyphen and a three-digit number that is automatically incremented. More information about this is available in the next chapter. Save the file under the name "UniqueNumber.xml" in the portal directory "internal/cfg".
Now the portal service needs to be restarted. You can see the result in the browser below.
Part types
The individual parts of a unique number and their ordering are defined by the different ""part type"" elements.
part type: constant
<part type="constant" value="INTREXX-"/<
Generates a fixed string value of your choice.
part type: number
<part type="number" format="###"/<
Generates an incremental number that increases by 1 each time. The length of the number part is controlled by the number of diamonds (#). format="###" generates a three-digit number part for the unique number in the syntax 001, 002, 003, etc. If the highest possible number is reached (999 in this example), the process starts again at 001.
part type: time/date
Each part of the date/time must be created individually. All of the required part types from year through month, day, hour, minute to second are available here.
<part type="year"/>
<part type="month"/>
<part type="day"/>
<part type="hour"/>
<part type="minute"/>
<part type="second"/>
If a condition is also used for the date/time part types (year, month, day, hour, minute and second), the number part of the unique number starts counting again at 1 when the corresponding date/time part is changed. When using the condition, make sure that the date/time part types are arranged in sequence from the strongest (year) to the weakest (second) value.
<part type="year" condition="true"/>
<part type="month" condition="true"/>
<part type="day" condition="true"/>
Format="Format specification" can be used to format the date/time output.
<part type="year" format="yyyy"/>
<part type="month" format="mm"/>
<part type="day" format="dd"/>