From 75c3c6f71687462f399b24b0f6268131e075a8c9 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Tue, 3 Sep 2024 09:23:00 +0200 Subject: [PATCH] introduce HostingAssetEntitySaveProcessor.save using the EntityManager --- .../asset/HsHostingAssetController.java | 4 +-- .../HostingAssetEntitySaveProcessor.java | 31 ++++++++++++++++--- ...sManagedWebspaceHostingAssetValidator.java | 2 +- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetController.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetController.java index 4ae94c00..cb4e3446 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetController.java @@ -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(); diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HostingAssetEntitySaveProcessor.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HostingAssetEntitySaveProcessor.java index 09628919..3e5850e5 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HostingAssetEntitySaveProcessor.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HostingAssetEntitySaveProcessor.java @@ -58,19 +58,42 @@ 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`. + * + *

`validator.postPersist(em, entity)` is NOT called. + * If any postprocessing is necessary, the saveFunction has to implement this.

+ * @param saveFunction + * @return + */ public HostingAssetEntitySaveProcessor saveUsing(final Function saveFunction) { - step("saveUsing", "validateContext"); + step("save", "validateContext"); entity = saveFunction.apply(entity); - em.flush(); // makes RbacEntity available as RealEntity if needed - validator.postPersist(em, entity); return this; } + /** + * Saves the using the `EntityManager`, but does NOT ever merge the entity. + * + *

`validator.postPersist(em, entity)` is called afterwards with the entity guaranteed to be flushed to the database.

+ * @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) public HostingAssetEntitySaveProcessor validateContext() { step("validateContext", "mapUsing"); diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsManagedWebspaceHostingAssetValidator.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsManagedWebspaceHostingAssetValidator.java index 52a29dc5..d39b052b 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsManagedWebspaceHostingAssetValidator.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsManagedWebspaceHostingAssetValidator.java @@ -43,7 +43,7 @@ class HsManagedWebspaceHostingAssetValidator extends HostingAssetEntityValidator .preprocessEntity() .validateEntity() .prepareForSave() - .saveUsing(emw::persist) + .save() .validateContext(); webspaceAsset.getConfig().put("groupid", unixUserAsset.getConfig().get("userid")); }