The IdGenerator interface includes methods for generating IDs that are strings or longs:

generateLongId(String pIdSpace)
generateStringId(String pIdSpace)

When you want to get a new ID, use these IdGenerator methods of the interface.

Normally, applications access the standard ID generator service at /atg/dynamo/service/IdGenerator, which starts up when your application is started. In the following examples, we will construct and use our own ID generator to demonstrate the IdGenerator APIs. You can see these examples in context in the sample class located at <ATG2007.3dir>/DAS/src/Java/atg/service/idgen/sample/Example1.java.

First, we construct an IdGenerator and get some IDs. Note that you don’t need to specify a name for the IdSpace. The default IdSpace is used:

TransientIdGenerator gen = new TransientIdGenerator();
gen.initialize();

    for (int i=0; i<3; i++)
      {
       gen.generateLongId();
      }
Generating an ID

The next line shows how you might generate a long ID in an IdSpace named foo. Note that, with the IdGenerator component’s autoCreate property set to true, as it is by default, we do not have to create the fooIdSpace -- it will be created for us.

gen.generateLongId("foo");

Given a seed of 1, this generates the ID 1.

Creating a New IdSpace

In most cases, your application will use the SQLIdGenerator and configure IdSpaces for it in an XML configuration file. Here we show how to create an IdSpace using the Java API:

IdSpace barSpace = new IdSpace("bar",  // name of id space
                               100,    // starting id (seed)
                               "Bar",    // prefix
                               null);  // suffix

gen.addIdSpace(barSpace);
Generating More IDs

Now, let’s generate more IDs in the bar and fooIdSpaces:

gen.generateLongId("bar"); //generates ID=100
gen.generateLongId("bar"); //generates ID=101

// see how the "foo" space is independent of the bar space
gen.generateLongId("foo");  //generates ID=2
Generating String IDs

Now generate some String IDs. String IDs use the prefix and suffix properties of the IdSpace. These properties are not consulted when long IDs are generated. Within an IdSpace, the same pool of IDs is used for String and long IDs.

gen.generateStringId("bar"); //generates ID=Bar102
gen.generateStringId("bar"); //generates ID=Bar103
gen.generateStringId("bar"); //generates ID=Bar104
gen.generateLongId("bar");   //generates ID=105
gen.generateLongId("bar");   //generates ID=106
gen.generateStringId("bar"); //generates ID=Bar107
IdGeneratorException

IdGenerator methods throw the checked exception IdGeneratorException. This exception indicates an ID could not be generated. Common causes include database trouble for the SQLIdGenerator and asking for an ID in a name space that doesn’t exist when autoCreate is false. Production applications should catch this exception. The following example forces an exception for demonstration purposes:

gen.setAutoCreate(false);
try
  {
    gen.generateStringId("bogus");
  }
catch (IdGeneratorException ige)
  {
    System.out.println("rats, couldn't get an id");
    ige.printStackTrace();
  }
 
loading table of contents...