Java Low-Code Implementation Examples
In the following sections there are stub classes implementation examples of the most commonly used cases.
Factory Commands
The main purpose of a factory command is to set values to the properties of the entity and then persist those changes to the database, thus, creating a new instance.
The factory command to create an instance of a MasterCard root entity is the following:
@Service
public class MasterCardCommand extends MasterCardCommandBase {
@Override
public void createCreditCard(MasterCard instance, CreateCreditCardInput createCreditCardInput) {
// Fill instance with values from input entity
instance.setBankName(createCreditCardInput.getBankName());
// save entity to repository
repo.cc.masterCard.save(instance);
}
}
Instance commands
The implementation of an instance command called ActivateCard which manipulates the state of an instance of the root entity MasterCard is shown below:
@Service
public class MasterCardCommand extends MasterCardCommandBase {
@Override
public void activateCard(MasterCard instance) {
// Change root entity data
instance.setActive(true);
// Save to repository
repo.cc.masterCard.save(instance);
}
Events
@Service
public class MasterCardCommand extends MasterCardCommandBase {
@Override
public void activateCard(MasterCard instance) {
// Change root entity data
instance.setActive(true);
// Save to repository
repo.cc.masterCard.save(instance);
// Create payload entity
SuccessEventPayload payloadEntity = entityBuilder.cc.successEventPayload().build();
// Create domain event
SucessEvent event = eventBuilder.cc.sucessEvent().setPayload(payloadEntity).build();
// Publish event via publishSucessEvent() that exists defined in MasterCardCommandBase class.
publishSucessEvent(event);
}
// Declare event builder
@Autowired
EventBuilderFacade eventBuilder;
// Declare entity builder
@Autowired
EntityBuilderFacade entityBuilder;
// Declare Event Prodcuer Service
@Autowired
EventProducerService eventProdcuer;
// Create payload entity
SuccessEventPayload payloadEntity = entityBuilder.cc.successEventPayload().build();
// Create domain event
SucessEvent event = eventBuilder.cc.sucessEvent().setPayload(payloadEntity).build();
// Publish a domain event.
eventProdcuer.publish(event);
Agents
An event that was triggered, will call an agent. This agent will receive the payload of the event as input and then it can call a service or publish another event.
public class SucessEventAgent extends SucessEventAgentBase {
private static Logger log = LoggerFactory.getLogger(SucessEventAgent.class);
@Override
public void onMessage(SucessEventPayload sucessEventPayload) {
log.info("Agent SucessEventAgent received event cc:SucessEvent");
// call service to notify customer passing the sucessEventPayload to the service as input
serviceFacade.cc.notifyCustomer(sucessEventPayload);
}
}
Domain Services
The implementation of a domain service GetCustomer is shown below:
@Service
public class GetCustomer extends GetCustomerBase {
@NewSpan
@Override
public Customer execute(GetCustomerInputEntity input){
// Call repository to get all customers
return repo.cc.customer.findById(input.getCustomerId());
}
}
External Entity Service
The external entity service is comprised of 2 functions: create and validate.
Create
The create function is used to construct a local instance of the External Entity and initialize the associated properties with certain pre-specified values that are provided as input parameters.
The input parameters is also used to identify and load external entity from external system.
@Service
public class AmixCardService extends AmixCardServiceBase {
@Override
public AmixCard create(String amixCardId, String customerId) {
// Call your external system and use parameters to identify and load external entity
// Create external entity instance and set its properties
AmixCard amixCard = new AmixCard(amixCardId, "Card type retrived from extenral entity");
return amixCard;
}
Validate
The validate function gives information regarding the existence of a specific instance of an External Entity within external system.
It usually returns external entity instance
if the external instance exists in
external system and null
if it does not exist.
The validator can also be used for updating the local instance of an External Entity if the
update
flag is set to true
.
In a validator it is recommended to try to load the instance. If the instance is returned successfully, then update and return external entity instance.
/**
* validate the existence of the external entity in the external system.
*
* @param externalEntity instance of AmixCard.
* @param update flag to indicate whether should an updated
* AmixCard instance be created and returned or not.
* @return Optional AmixCard instance to indicate whether
* external entity still exists in external system
*/
@Override
public Optional<AmixCard> validate(AmixCard externalEntity, boolean update) {
// Call external system to validate existence of external entity in external system
// Also you can update external entity properties
AmixCard amixCard = new AmixCard(externalEntity.getAmixCardId(),
"Updated Card type info retrived from extenral entity");
return Optional.of(amixCard);
}