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 if the portal service is restarted, the highest allocated unique number will be determined and used as the basis for calculating the subsequent 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 aborted, i.e. the page is closed without saving the new record, the number generated during opening will still be 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 data group contained there. Since values with Date, Integer and String format can be included in a unique number, please use data fields with the "Short text" data type only. Do not place a unique number in a system data field. Only use data fields that you have created yourself. Assign the title "Unique number" here.
Display the data fields in the application structure by selecting "Show data fields" from the Edit menu. Please activate the expert options if you have not done so already.
Open the properties dialog of the Unique Number data field by double-clicking on the data field in the application structure and there go to the "Expert" tab. Enter
de.uplanet.lucy.server.util.numbergenerator.SystemFieldNumberGenerator
as the value of the "datahanlder" attribute and click on "OK".
Now you can still include the Unique Number field in 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".
A GUID for the unique number can be generated easily via the 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 the example shown here, the new number would start with "INTREXX-" followed by the four-digit year, month, 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 with 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 hash signs (#). format="###" thus creates a three-digit number part for the unique number in the syntax 001, 002, 003, etc. Once the highest possible number has been reached (here in the example 999), it is started 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 used with the date/time part types (year, month, day, hour, minute and second), then the number part of the unique number will restart from 1 if one of the corresponding date/time parts change. When using such a condition, however, it is important to ensure that the date/time part types are placed in sequence from the highest (year) to the lowest (second).
<part type="year" condition="true"/>
<part type="month" condition="true"/>
<part type="day" condition="true"/>
The "format" option allows you to customize the format of the date/time parts.
<part type="year" format="yyyy"/>
<part type="month" format="mm"/>
<part type="day" format="dd"/>