Customise ID Generation

Overview

The IDGen service is designed to eliminate redundant code for ID generation by providing a centralized mechanism for managing ID formats. This reduces the maintenance burden on developers and allows for a configuration-driven approach, enabling usage without writing any additional code.

When is Customisation Required?

The IDGen service supports a set of out-of-the-box replaceable tokens for ID format generation. For most use cases, these defaults are sufficient. Customization is required only in the following scenarios:

  • When ID formats must follow complex logic not covered by existing tokens.

  • When integration with external systems is necessary.

  • When new ID types are introduced that are not natively supported by the IDGen service.

Refer to the official documentation for the list of supported replaceable tokens.

Steps

If your requirements extend beyond the supported configuration, you can implement customization in the following way:

  1. Create a Wrapper API or Wrapper Logic

    • Implement a wrapper in your service to invoke the IDGen service.

    • Retrieve the default ID generated using the standard configuration.

  2. Apply Custom Logic

    • Add your custom rules or transformations.

    • Replace or modify placeholders based on your specific requirements.

  3. Return Final ID

    • Once the custom logic is applied, the wrapper should return the final ID to the calling system.

This approach ensures that the core IDGen service remains generic, while custom business rules are encapsulated within your wrapper.

package org.egov.ccrs.wrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.egov.idgen.contract.IdGenerationRequest;
import org.egov.idgen.contract.IdGenerationResponse;
import org.egov.idgen.contract.IdRequest;
import org.egov.idgen.contract.IdResponse;
import org.egov.idgen.service.IdGenService;

import java.util.Collections;
import java.util.List;

@Component
public class CustomIdGenWrapper {

    @Autowired
    private IdGenService idGenService; // Service that connects to IDGen

    /**
     * Generates a custom ID by applying business logic on top of IDGen defaults.
     *
     * @param tenantId The tenant ID for which ID is generated
     * @param idName   The ID name configured in IDGen
     * @param format   The format key configured in IDGen
     * @return The final custom ID
     */
    public String generateCustomId(String tenantId, String idName, String format) {
        // Step 1: Request ID from IDGen
        IdRequest idRequest = IdRequest.builder()
                .idName(idName)
                .tenantId(tenantId)
                .format(format)
                .build();

        IdGenerationRequest request = IdGenerationRequest.builder()
                .idRequests(Collections.singletonList(idRequest))
                .build();

        IdGenerationResponse response = idGenService.generateId(request);
        List<IdResponse> idResponses = response.getIdResponses();

        if (idResponses == null || idResponses.isEmpty()) {
            throw new RuntimeException("Failed to generate ID from IDGen");
        }

        // Base ID generated by IDGen
        String baseId = idResponses.get(0).getId();

        // Step 2: Apply custom logic (example: prefix + baseId + checksum)
        String customPrefix = "CUST-";  

        String finalId = customPrefix + baseId;

        return finalId;
    }
}

Last updated

Was this helpful?