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

View File

@ -58,17 +58,40 @@ public class HostingAssetEntitySaveProcessor {
/// hashing passwords etc.
@SuppressWarnings("unchecked")
public HostingAssetEntitySaveProcessor prepareForSave() {
step("prepareForSave", "saveUsing");
step("prepareForSave", "save");
validator.prepareProperties(em, entity);
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) {
step("saveUsing", "validateContext");
step("save", "validateContext");
entity = saveFunction.apply(entity);
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 this;
return entity;
});
}
/// validates the entity within it's parent and child hierarchy (e.g. totals validators and other limits)

View File

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