Implement Domain External Entity Service

Idea

  • External Entity modelled in designer acts as a pointer / refrence to an Entity that may reside in another system.

  • Using generated external entity service we can implement the logic for:

    1. create, creates the external entity pointer / refernce.

    2. load, loads the full details of that external system Entity and Map it to one of modelled Domain Entities

    3. validate, valdiate that the referenced Entity still exists in the external system and optionaly update the Extenral Entity data.

External Entity Service Base

  • For each external entity there will be an abstract class External Entity Service Base generated in the SDK.

  • The External Entity Service Base provide access to the repository and entity builder.

  • The External Entity Service Base contains three abstract methods create, load, and validate.

  • These three methods needs to be implemented in the generated implementation file for the External Entity.

Create

  • create method will take modelled constrcutor properties as a paramters.

  • constrcutor properties acts as identifiers to be able to retrive full data external entity that might reside in an external system.

  • create method will return modelled External Entity as a return type.

Load

  • load method will take modelled External Entity as a paramter.

  • Depending on Logic, load can connect to external system, and retrive full data of Entity, that is refernced by External Entity then map it to one of Domain Entity.

  • load method will return an instance of an Entity.

Validate

  • validate method will take modelled External Entity as first paramter.

  • validate method update flag helps to determine whether to update referneced External Entity or not.

  • validate method should connect to external system, to validate extistance of Entity that its referenced by External Entity.

Implementation Example

Example of Balance External Entity service implemenation file.

    //... imports

    @Service
    public class BalanceService extends BalanceServiceBase {

      private static Logger log = LoggerFactory.getLogger(BalanceService.class);

      public BalanceService(DomainEntityBuilder entityBuilder, Repository repo){
        super(entityBuilder,repo);
      }

      @Override
      public Balance create(String customerId) {

        log.info("BalanceService.create()");
        
        // Make external calls using the customerId , to get the data of External Entity
        RestTemplate restTemplate = new RestTemplate();
        JsonObject responseObject = restTemplate
        .getForObject("https://some-url/balance/"+ customerId, JsonObject.class);

        Balance balance = this.entityBuilder.getCc().getBalance().build();
        balance.setCustomerId(customerId);

        // Extract properties from responseObject and set it to balance entity

        return balance;
      }

      /**
      * Load external Entity and return as an Entity
      *
      * @param externalEntity instance of Balance
      * @return class that extends base type Entity
      */
      @Override
      public Entity load(Balance externalEntity) {

        log.info("BalanceService.load()");

        return null;
      }

      /**
      * validate the existence of the external entity in the external system.
      *
      * @param externalEntity instance of Balance.
      * @param update          flag to indicate whether should an updated
      *                        Balance instance be created and
      *                        returned or not.
      * @return Optional Balance instance to indicate whether
      *         external entity still exists in external system
      */
      @Override
      public Optional<Balance> validate(Balance externalEntity, boolean update) {

        log.info("BalanceService.validate()");

        // Make external calls to validate if external entity still exists
        RestTemplate restTemplate = new RestTemplate();
        JsonObject responseObject = restTemplate
        .getForObject("https://some-url/has-balance/"+ externalEntity.getCustomerId(), JsonObject.class);

        // Need to handle cases:
        // 1. external entity no longer exists
        // 2. If update is set to true, should update the @param *externalEntity* data and return it.

        return Optional.of(externalEntity);
      }
    }