Tips & Tricks - Unique Number Generator
The Unique Number Generator allows you to generate automatically incrementing numbers based on predefined criteria. The number can consist of freely combinable parts (number, date, time, word). The best-known example of a unique number is probably inventory numbers. When creating inventory items in a corresponding Intrexx application, the Unique Number Generator can be used to generate sequential numbers based on a defined syntax (e.g., Inv-2007-06-0001, Inv-2007-06-0002, etc.).
The Unique Number Generator can also be useful for orders, bills of materials, or customer numbers. Unique numbers can be generated using a data field's data handler. Before saving, the system determines which unique number is the highest number currently assigned in any of the records that have already been entered. When the record is saved, a new number—increased by 1—is written to the data field. This method ensures that numbers are assigned sequentially. Even when the portal service is restarted, the highest unique number assigned is determined and used as the starting number for subsequent numbers.
Continuous, sequential numbering can also be generated using Groovy. JavaScript or Velocity are not suitable, because when the page loads, it is not certain whether the data will actually be saved.
The Unique Number Generator can be used in various contexts, such as when opening a page to create a new record. Please note that in this case, a new number is always generated when the page is opened. If the process is canceled—that is, if the page is closed without saving the new record—the number generated when the page was opened is still used. This can result in gaps in the sequential numbering.
To work around this, the UniqueNumberGenerator can be used within the process when saving. However, the new number won't appear until after you save it.
Configure Datahandler
First, create a new application in Portal Manager based on the "Base Application" application template. Then create a new data field in the contained data group. Since a Unique Number can contain date, integer, and string values, please use only data fields with the "Short Text" data type. Do not use the unique number in system fields. Use only data fields that you have created yourself. Enter the title "Unique Number."
Use the "Edit / Show Data Fields" option in the main menu to display the data fields in the data group. If you haven't already, enable the advanced options.
Open the Properties dialog box for the "Unique Number" data field by double-clicking the data field in the application structure, and then switch to the "Expert" tab. For the "datahandler" attribute, enter the value
de.uplanet.lucy.server.util.numbergenerator.SystemFieldNumberGenerator
and click "OK."
Now you can add the "Unique Number" field to the table view on the "All Entries" page. Save the application.
Creating the UniqueNumber.xml file
The UniqueNumber.xml file contains the definitions 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 using a standard text editor and copy the code above into the file. Change the following values as shown below:
-
name
Name of the respective unique number (can be chosen freely, must be unique)
-
guid
GUID of the respective unique number (can be chosen freely, must be unique)
-
link-datafield
GUID of the string data field in which the unique number is generated
-
<part .../>
Definition of a Part of the Unique Number
Enter a name for your unique number in the "name" field.
You can easily generate a GUID for the unique number via the main menu under "Tools / Create GUID. "
You can determine the GUID of the data field in which the unique number is to be generated by pressing the F4 key after selecting it in the application structure.
Any number of unique numbers can be defined in the UniqueNumber.xml file. For additional definitions of "Unique Numbers," see the section
<uniquenumber name=" myUniqueNumber1" guid="guid" link-datafield="guid">...
</uniquenumber>
and modify the "name," "guid," "link-datafield," and part types accordingly.
In the Part Type tags that follow, you can specify a portion of the unique number in each one. In our example here, the new number would start with "INTREXX-," followed by the four-digit year, the month, the day, a hyphen, and a three-digit number that is automatically incremented. You can learn more about this topic in the following chapter. Save the file as "UniqueNumber.xml" in the "internal/cfg" directory of the portal.
Now the portal service must be restarted. Here you can see the result in the browser.
Part Types
The individual components of a unique number and their order are defined by the different part types.
part type: constant
<part type="constant" value="INTREXX-"/<
This part type can be used to generate any fixed string value.
part type: number
<part type="number" format="###"/<
This part type can be used to generate a number that increments by 1. The number of hash symbols (#) determines the length of the number portion. format="###" therefore generates a three-digit number for the unique number in the format 001, 002, 003, etc. Once the highest possible number has been reached (999 in this example), the count starts over at 001.
Part type: Time/Date
For part types of the "Time/Date" type, each part of a date must be created individually. All the necessary time units—from year to month, day, hour, minute, and even second—are available for this purpose.
<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 date/time part types (year, month, day, hour, minute, and second), the number portion of the unique number will start counting from 1 again when the corresponding date/time part changes. When using the "condition," make sure that the date/time part types are arranged in order from the most significant (year) to the least significant (second) value.
<part type="year" condition="true"/>
<part type="month" condition="true"/>
<part type="day" condition="true"/>
You can format the date/time output using format="format specification".
<part type="year" format="yyyy"/>
<part type="month" format="mm"/>
<part type="day" format="dd"/>




