introduce HostingAssetEntitySaveProcessor.save using the EntityManager
This commit is contained in:
parent
1cf192a7f0
commit
75c3c6f716
@ -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();
|
||||||
|
@ -58,17 +58,40 @@ 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);
|
||||||
|
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
|
em.flush(); // makes RbacEntity available as RealEntity if needed
|
||||||
validator.postPersist(em, entity);
|
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)
|
/// validates the entity within it's parent and child hierarchy (e.g. totals validators and other limits)
|
||||||
|
@ -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"));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user