Implementing

Create entities

Entity builders can be used to create an entity (root entity/entity).

  1. Example for injecting and using entity builder.

   import de.knowis.cards.sdk.domain.facade.DomainEntityBuilder; 

   // Declare and inject Domain Entity Builder
   @Autowired
   private DomainEntityBuilder entityBuilder;
     
   // Create a customer entity that exists in a domain namespace prefixed by cc.
   CustomerEntity customer = entityBuilder.cc.customer()
           .setCustomerId("Customer Id")
           .setCustomerName("Joh Doe")
           .setAmixCard(new AmixCard("some id", CardType.debit))
           .build();
Tip: EntityBuilder is also accessible in project generated implementation files (services, commands, agents) derived from their base classes.
  1. Example for using entity builder provided by base class to create entities.

    // Use the DomainEntityBuilder to create an entity that was modelled in a domain namespace with prefix: cc
    OwnerEntity owner = this.domainEntityBuilder.getCc().getOwner().build();

    // Use IntegrationnEntityBuilder to create an entity that was modelled in an integration namespace with prefix: cards
    AmexCardEntity amexCard = this.integrationEntityBuilder.getCards().getAmexCard().build();

Repository

  • For each root entity there will be an associated repository interface generated in the SDK.

  • Each Repository Interface extends JpaRepository <T, ID>

  • The JpaRepository provide access to several functionalities such as findBy, findAll, count, save, delete....etc.

  • You can directly inject root entity repository or use it using repo which is a repository facade that is available in commands, domain services, external entities and agents base classes.

Example of repository usage for different database operations.

    // Build a credit card root entity that exists in a domain namespace prefixed by cc.
    CreditCardEntity creditCard = this.entityBuilder.cc.creditCard()
        .setBankName("MyBank")
        .setCardType(CardType.debit)
        .setCustomer(customer)
        .setIssueDate(OffsetDateTime.now())
        .build();

    // Save a credit card root entity.
    creditCard = this.repo.cc.creditCard.save(creditCard);

    // Retrieve all credit card root entities
    List<CreditCardEntity> cards = this.repo.cc.creditCard.findAll();

    // Update credit card root entity
    creditCard.setCardType(CardType.credit);
    repo.cc.creditCard.save(creditCard);

    // Delete credit card root entity
    repo.cc.creditCard.delete(creditCard);

    //Count all credit card root entities
    long count = this.repo.cc.creditCard.count();

Example of using inside a command implementation using declared repository from command base class.

    //... imports

    @Service
    public class CardCommand extends CardCommandBase {

      private static Logger log = LoggerFactory.getLogger(CardCommand.class);
      
      public CardCommand(DomainEntityBuilder entityBuilder, DomainEventBuilder eventBuilder, EventProducerService eventProducer, Repository repo) {
        super(entityBuilder, eventBuilder, eventProducer, repo);
      }


      @Override
      public Card createCreditCard(CreditCard creditCard) throws CreditCardCreationError {

        log.info("CardCommands.createCreditCard()");
            // ..... command logic

            // Using this.repo to find and save an updated Card root entity
            try {

                Optional<CardEntity> cardRootEntity = 
                this.repo.getCc().getCard().findById(creditCard.getId());
            
              // Update card if it exists
              if(cardRootEntity.isPresent()){

                cardRootEntity.setActive(true);

                log.info("Updated Credit Card Root Entity with Id {}", cardRootEntity.getId());
                
                // Save updated card
                this.repo.getCc().getCard().save(cardRootEntity);
                } else {
                  throw CardNotFoundException("Could not find card with Id {}", creditCard.getid());
                }

        } catch(Exception e) {
          // ... Exception handling logic
        }

        return cardRootEntity;
      }