Skip to content

Commit

Permalink
Improved hook system for PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed May 19, 2024
1 parent 48ae76e commit 4d29885
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ public class DefEntityType implements AnnotatedConfigurable<Void, Void> {
*/
@ConfigurableField(editor = EditorList.class, optional = true,
label = "PM Hooks", description = "Persistence Manager Hooks")
@EditorList.EdOptsList(editor = EditorSubclass.class)
@EditorSubclass.EdOptsSubclass(iface = DefPmHook.class, merge = true, nameField = "@class", shortenClassNames = true)
@EditorList.EdOptsList(editor = EditorClass.class)
@EditorClass.EdOptsClass(clazz = DefPmHook.class)
private List<DefPmHook> hooks = new ArrayList<>();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import de.fraunhofer.iosb.ilt.frostserver.model.core.Entity;
import de.fraunhofer.iosb.ilt.frostserver.model.core.PkValue;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.JooqPersistenceManager;
import de.fraunhofer.iosb.ilt.frostserver.service.UpdateMode;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.IncompleteEntityException;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.NoSuchEntityException;

Expand All @@ -31,5 +32,5 @@
*/
public interface HookPostUpdate extends JooqPmHook {

public void postUpdateInDatabase(JooqPersistenceManager pm, Entity entity, PkValue entityId) throws NoSuchEntityException, IncompleteEntityException;
public void postUpdateInDatabase(JooqPersistenceManager pm, Entity entity, PkValue entityId, UpdateMode updateMode) throws NoSuchEntityException, IncompleteEntityException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import de.fraunhofer.iosb.ilt.frostserver.model.core.Entity;
import de.fraunhofer.iosb.ilt.frostserver.model.core.PkValue;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.JooqPersistenceManager;
import de.fraunhofer.iosb.ilt.frostserver.service.UpdateMode;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.IncompleteEntityException;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.NoSuchEntityException;

Expand All @@ -31,5 +32,5 @@
*/
public interface HookPreUpdate extends JooqPmHook {

public void preUpdateInDatabase(JooqPersistenceManager pm, Entity entity, PkValue entityId) throws NoSuchEntityException, IncompleteEntityException;
public void preUpdateInDatabase(JooqPersistenceManager pm, Entity entity, PkValue entityId, UpdateMode updateMode) throws NoSuchEntityException, IncompleteEntityException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public boolean insertIntoDatabase(JooqPersistenceManager pm, Entity entity, Upda
* @throws NoSuchEntityException If the entity to be updated does not exist.
* @throws IllegalStateException If something else goes wrong.
*/
protected void updateNavigationPropertySet(Entity entity, EntitySet linkedSet, JooqPersistenceManager pm, UpdateMode updateMode) throws IncompleteEntityException, NoSuchEntityException {
private void updateNavigationPropertySet(Entity entity, EntitySet linkedSet, JooqPersistenceManager pm, UpdateMode updateMode) throws IncompleteEntityException, NoSuchEntityException {
final NavigationPropertyEntitySet navProp = linkedSet.getNavigationProperty();
final Relation relation = findRelation(navProp.getName());
if (relation == null) {
Expand Down Expand Up @@ -378,6 +378,10 @@ protected void updateNavigationPropertySet(Entity entity, EntitySet linkedSet, J
relation.link(pm, entity, linkedSet, navProp);
} else {
for (Entity child : linkedSet) {
EntityFactories ef = pm.getEntityFactories();
if (!ef.entityExists(pm, child, false)) {
throw new NoSuchEntityException("Can not link " + child.getEntityType() + " with no id.");
}
relation.link(pm, entity, child, navProp);
}
}
Expand All @@ -392,7 +396,7 @@ public EntityChangedMessage updateInDatabase(JooqPersistenceManager pm, Entity e
final EntityChangedMessage message = new EntityChangedMessage();

for (SortingWrapper<Double, HookPreUpdate> hookWrapper : hooksPreUpdate) {
hookWrapper.getObject().preUpdateInDatabase(pm, entity, entityId);
hookWrapper.getObject().preUpdateInDatabase(pm, entity, entityId, updateMode);
}

for (NavigationPropertyMain<Entity> np : entityType.getNavigationEntities()) {
Expand Down Expand Up @@ -445,7 +449,7 @@ public EntityChangedMessage updateInDatabase(JooqPersistenceManager pm, Entity e
}

for (SortingWrapper<Double, HookPostUpdate> hookWrapper : hooksPostUpdate) {
hookWrapper.getObject().postUpdateInDatabase(pm, entity, entityId);
hookWrapper.getObject().postUpdateInDatabase(pm, entity, entityId, updateMode);
}

return message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void registerHooks(StaMainTable mainTable, JooqPersistenceManager ppm) {
}
if (getCheckUpdate() != null) {
LOGGER.info(" - update: {}", getCheckUpdate());
mainTable.registerHookPreUpdate(-10, (pm, entity, id) -> {
mainTable.registerHookPreUpdate(-10, (pm, entity, id, updateMode) -> {
if (PrincipalExtended.getLocalPrincipal().isAdmin()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public void initProperties(final EntityFactories entityFactories) {
return true;
});
obsPropsTable.registerHookPreUpdate(-1,
(pm, entity, entityId) -> {
(pm, entity, entityId, updateMode) -> {
EntitySet mds = entity.getProperty(pluginMultiDatastream.npMultiDatastreamsObsProp);
if (mds != null && !mds.isEmpty()) {
throw new IllegalArgumentException("Adding a MultiDatastream to an ObservedProperty is not allowed.");
Expand Down Expand Up @@ -385,7 +385,7 @@ public void initProperties(final EntityFactories entityFactories) {
return true;
});
// On update, make sure we still have either a DS or MDS, but not both.
observationsTable.registerHookPreUpdate(-1, (pm, entity, entityId) -> {
observationsTable.registerHookPreUpdate(-1, (pm, entity, entityId, updateMode) -> {
Entity oldObservation = pm.get(pluginCoreModel.etObservation, entityId);
boolean newHasDatastream = checkDatastreamSet(oldObservation, entity, pm);
boolean newHasMultiDatastream = checkMultiDatastreamSet(oldObservation, entity, pm);
Expand Down

0 comments on commit 4d29885

Please sign in to comment.