introduce HostingAssetEntitySaveProcessor.save using the EntityManager

This commit is contained in:
Michael Hoennig 2024-09-03 09:23:00 +02:00
parent 1cf192a7f0
commit 75c3c6f716
3 changed files with 30 additions and 7 deletions

View File

@ -79,7 +79,7 @@ public class HsHostingAssetController implements HsHostingAssetsApi {
.preprocessEntity() .preprocessEntity()
.validateEntity() .validateEntity()
.prepareForSave() .prepareForSave()
.saveUsing(rbacAssetRepo::save) .save()
.validateContext() .validateContext()
.mapUsing(e -> mapper.map(e, HsHostingAssetResource.class)) .mapUsing(e -> mapper.map(e, HsHostingAssetResource.class))
.revampProperties(); .revampProperties();
@ -140,7 +140,7 @@ public class HsHostingAssetController implements HsHostingAssetsApi {
.preprocessEntity() .preprocessEntity()
.validateEntity() .validateEntity()
.prepareForSave() .prepareForSave()
.saveUsing(rbacAssetRepo::save) .save()
.validateContext() .validateContext()
.mapUsing(e -> mapper.map(e, HsHostingAssetResource.class)) .mapUsing(e -> mapper.map(e, HsHostingAssetResource.class))
.revampProperties(); .revampProperties();

View File

@ -58,19 +58,42 @@ public class HostingAssetEntitySaveProcessor {
/// hashing passwords etc. /// hashing passwords etc.
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public HostingAssetEntitySaveProcessor prepareForSave() { public HostingAssetEntitySaveProcessor prepareForSave() {
step("prepareForSave", "saveUsing"); step("prepareForSave", "save");
validator.prepareProperties(em, entity); validator.prepareProperties(em, entity);
return this; return this;
} }
/**
* Saves the entity using the given `saveFunction`.
*
* <p>`validator.postPersist(em, entity)` is NOT called.
* If any postprocessing is necessary, the saveFunction has to implement this.</p>
* @param saveFunction
* @return
*/
public HostingAssetEntitySaveProcessor saveUsing(final Function<HsHostingAsset, HsHostingAsset> saveFunction) { public HostingAssetEntitySaveProcessor saveUsing(final Function<HsHostingAsset, HsHostingAsset> saveFunction) {
step("saveUsing", "validateContext"); step("save", "validateContext");
entity = saveFunction.apply(entity); entity = saveFunction.apply(entity);
em.flush(); // makes RbacEntity available as RealEntity if needed
validator.postPersist(em, entity);
return this; return this;
} }
/**
* Saves the using the `EntityManager`, but does NOT ever merge the entity.
*
* <p>`validator.postPersist(em, entity)` is called afterwards with the entity guaranteed to be flushed to the database.</p>
* @return
*/
public HostingAssetEntitySaveProcessor save() {
return saveUsing(e -> {
if (!em.contains(entity)) {
em.persist(entity);
}
em.flush(); // makes RbacEntity available as RealEntity if needed
validator.postPersist(em, entity);
return entity;
});
}
/// validates the entity within it's parent and child hierarchy (e.g. totals validators and other limits) /// validates the entity within it's parent and child hierarchy (e.g. totals validators and other limits)
public HostingAssetEntitySaveProcessor validateContext() { public HostingAssetEntitySaveProcessor validateContext() {
step("validateContext", "mapUsing"); step("validateContext", "mapUsing");

View File

@ -43,7 +43,7 @@ class HsManagedWebspaceHostingAssetValidator extends HostingAssetEntityValidator
.preprocessEntity() .preprocessEntity()
.validateEntity() .validateEntity()
.prepareForSave() .prepareForSave()
.saveUsing(emw::persist) .save()
.validateContext(); .validateContext();
webspaceAsset.getConfig().put("groupid", unixUserAsset.getConfig().get("userid")); webspaceAsset.getConfig().put("groupid", unixUserAsset.getConfig().get("userid"));
} }