Service
Work in progress
Basics
VersionAwareResponseService
VersionAwareResponseService is utilized for directly outputting data from a table, where the number of output rows matches the number of records in the table. This service does not perform any complex data aggregation. However, if you need to output data from a view, any necessary data transformations can be handled within the view itself.
Example
@Service
public class InputBasicService extends VersionAwareResponseService<InputBasicDTO, InputBasic> {
private final InputBasicRepository repository;
public InputBasicService(InputBasicRepository repository) {
super(InputBasicDTO.class, InputBasic.class, null, InputBasicMeta.class);
this.repository = repository;
}
// --8<-- [start:doCreateEntity]
@Override
protected CreateResult<InputBasicDTO> doCreateEntity(InputBasic entity, BusinessComponent bc) {
repository.save(entity);
return new CreateResult<>(entityToDto(bc, entity));
}
// --8<-- [end:doCreateEntity]
// --8<-- [start:doUpdateEntity]
@Override
protected ActionResultDTO<InputBasicDTO> doUpdateEntity(InputBasic entity, InputBasicDTO data, BusinessComponent bc) {
if (data.isFieldChanged(InputBasicDTO_.customFieldRO)) {
entity.setCustomFieldRO(data.getCustomFieldRO());
}
if (data.isFieldChanged(InputBasicDTO_.customField)) {
entity.setCustomField(data.getCustomField());
}
return new ActionResultDTO<>(entityToDto(bc, entity));
}
// --8<-- [end:doUpdateEntity]
// --8<-- [start:getActions]
@Override
public Actions<InputBasicDTO> getActions() {
return Actions.<InputBasicDTO>builder()
.action(act -> act
.action("save", "save")
)
.build();
}
// --8<-- [end:getActions]
}
doUpdateEntity
Tips
This method should not perform any actions with side effects, as it is invoked multiple times by the kernel. Any calls to external services that modify data or perform actions should be handled in the button invoker.
AnySourceVersionAwareResponseService
AnySourceVersionAwareResponseService enables data display from any source: external data sources and aggregated data from databases. For directly outputting data from the database, it is recommended to use VersionAwareResponseService.
Example
@Service
public class MyExampleService extends AnySourceVersionAwareResponseService<MyExampleDTO, MyEntityOutServiceDTO> {
public MyExampleService() {
super(MyExampleDTO.class, MyEntityOutServiceDTO.class, MyExampleMeta.class, MyEntityDao.class);
}
@Override
protected CreateResult<MyExampleDTO> doCreateEntity(MyEntityOutServiceDTO entity, BusinessComponent bc) {
return new CreateResult<>(entityToDto(bc, entity));
}
@Override
protected ActionResultDTO<MyExampleDTO> doUpdateEntity(MyEntityOutServiceDTO entity, MyExampleDTO data, BusinessComponent bc) {
if (data.isFieldChanged(MyExampleDTO_.customField)) {
entity.setCustomField(data.getCustomField());
}
return new ActionResultDTO<>(entityToDto(bc, entity));
}
// --8<-- [start:getActions]
@Override
public Actions<MyExampleDTO> getActions() {
return Actions.<MyExampleDTO>builder()
.create(crt -> crt.text("Add"))
.addGroup(
"actions",
"Actions",
0,
Actions.<MyExampleDTO>builder()
.action(act -> act
.action("delete", "delete")
)
.action(act -> act
.action("save", "save")
)
.build()).
build();
}
}
doUpdateEntity
Tips
This method should not perform any actions with side effects, as it is invoked multiple times by the kernel. You are limited to adding actions only to the current transaction. Any calls to external services that modify data or perform actions should be handled in the button invoker.