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 13, 2024
1 parent bf8e359 commit 2eaabff
Show file tree
Hide file tree
Showing 13 changed files with 377 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,20 @@ public class DefEntityType implements AnnotatedConfigurable<Void, Void> {
* Validators that are used to validate entities of this type.
*/
@ConfigurableField(editor = EditorList.class, optional = true,
label = "NavProps")
label = "Validators", description = "Entity Validators")
@EditorList.EdOptsList(editor = EditorSubclass.class)
@EditorSubclass.EdOptsSubclass(iface = DefValidator.class, merge = true, nameField = "@class", shortenClassNames = true)
private List<DefValidator> validators = new ArrayList<>();

/**
* Hooks that are used to validate entities of this type.
*/
@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)
private List<DefPmHook> hooks = new ArrayList<>();

/**
* The (OData)annotations for this Element.
*/
Expand Down Expand Up @@ -165,6 +174,10 @@ public EntityType getEntityType(ModelRegistry modelRegistry) {
return entityType;
}

public List<DefPmHook> getHooks() {
return hooks;
}

public void linkProperties(ModelRegistry modelRegistry) {
LOGGER.debug(" {} ({})", name, plural);
for (DefEntityProperty defEp : entityProperties) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2023 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131
* Karlsruhe, Germany.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.fraunhofer.iosb.ilt.frostserver.model.loader;

import de.fraunhofer.iosb.ilt.configurable.AnnotatedConfigurable;
import de.fraunhofer.iosb.ilt.configurable.annotations.ConfigurableField;
import de.fraunhofer.iosb.ilt.configurable.editor.EditorDouble;
import de.fraunhofer.iosb.ilt.configurable.editor.EditorSubclass;

/**
* Pair of PmHook and Priority for said hook.
*/
public class DefPmHook implements AnnotatedConfigurable<Void, Void> {

@ConfigurableField(editor = EditorDouble.class, optional = true,
label = "Priority", description = "Priority of the Hook. Hooks with lower priority are executed earlier.")
@EditorDouble.EdOptsDouble(max = 100, min = -100, step = 0.1)
private double priority;

@ConfigurableField(editor = EditorSubclass.class,
label = "PM Hooks", description = "Persistence Manager Hooks")
@EditorSubclass.EdOptsSubclass(iface = PmHook.class, merge = true, nameField = "@class", shortenClassNames = true)
private PmHook hook;

public double getPriority() {
return priority;
}

public void setPriority(double priority) {
this.priority = priority;
}

public PmHook getHook() {
return hook;
}

public void setHook(PmHook hook) {
this.hook = hook;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2023 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131
* Karlsruhe, Germany.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.fraunhofer.iosb.ilt.frostserver.model.loader;

/**
* Interface for PersistenceManager-specific hooks.
*/
public interface PmHook {

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,21 @@
import de.fraunhofer.iosb.ilt.frostserver.model.loader.DefEntityType;
import de.fraunhofer.iosb.ilt.frostserver.model.loader.DefModel;
import de.fraunhofer.iosb.ilt.frostserver.model.loader.DefNavigationProperty;
import de.fraunhofer.iosb.ilt.frostserver.model.loader.DefPmHook;
import de.fraunhofer.iosb.ilt.frostserver.model.loader.PmHook;
import de.fraunhofer.iosb.ilt.frostserver.model.loader.PropertyPersistenceMapper;
import de.fraunhofer.iosb.ilt.frostserver.path.PathElement;
import de.fraunhofer.iosb.ilt.frostserver.path.PathElementEntity;
import de.fraunhofer.iosb.ilt.frostserver.path.PathElementEntitySet;
import de.fraunhofer.iosb.ilt.frostserver.path.ResourcePath;
import de.fraunhofer.iosb.ilt.frostserver.persistence.AbstractPersistenceManager;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.factories.EntityFactories;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.factories.HookPostDelete;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.factories.HookPostInsert;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.factories.HookPostUpdate;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.factories.HookPreDelete;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.factories.HookPreInsert;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.factories.HookPreUpdate;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.relations.Relation;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.tables.StaLinkTableDynamic;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.tables.StaMainTable;
Expand Down Expand Up @@ -617,6 +625,10 @@ private void loadMapping() {
for (DefModel modelDefinition : modelDefinitions) {
registerModelMappings(modelDefinition);
}

for (DefModel modelDefinition : modelDefinitions) {
registerHooks(modelDefinition);
}
// Done, release the model definitions.
tableCollection.clearModelDefinitions();
}
Expand Down Expand Up @@ -683,6 +695,34 @@ private void registerModelMappings(DefModel modelDefinition) {
}
}

private void registerHooks(DefModel modelDefinition) {
for (DefEntityType entityTypeDef : modelDefinition.getEntityTypes()) {
final EntityType entityType = entityTypeDef.getEntityType(settings.getModelRegistry());
final StaMainTable table = getOrCreateMainTable(entityType, entityTypeDef.getTable());
for (DefPmHook hookDef : entityTypeDef.getHooks()) {
PmHook hook = hookDef.getHook();
if (hook instanceof HookPreInsert h) {
table.registerHookPreInsert(hookDef.getPriority(), h);
}
if (hook instanceof HookPostInsert h) {
table.registerHookPostInsert(hookDef.getPriority(), h);
}
if (hook instanceof HookPreUpdate h) {
table.registerHookPreUpdate(hookDef.getPriority(), h);
}
if (hook instanceof HookPostUpdate h) {
table.registerHookPostUpdate(hookDef.getPriority(), h);
}
if (hook instanceof HookPreDelete h) {
table.registerHookPreDelete(hookDef.getPriority(), h);
}
if (hook instanceof HookPostDelete h) {
table.registerHookPostDelete(hookDef.getPriority(), h);
}
}
}
}

private void registerMappingForEntityProperties(DefEntityProperty propertyDef, StaMainTable orCreateTable) {
for (PropertyPersistenceMapper handler : propertyDef.getHandlers()) {
maybeRegisterMapping(handler, orCreateTable);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2023 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131
* Karlsruhe, Germany.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.factories;

import de.fraunhofer.iosb.ilt.frostserver.model.core.PkValue;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.JooqPersistenceManager;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.NoSuchEntityException;

/**
* A hook that can be registered on a table and will get executed after a
* postDelete has happened.
*
* @author hylke
*/
public interface HookPostDelete extends JooqPmHook {

/**
*
* @param pm The PersistenceManager for database access.
* @param entityId The entity ID that is going to be deleted.
* @throws NoSuchEntityException if something is wrong. This will cancel the
* action.
*/
public void postDelete(JooqPersistenceManager pm, PkValue entityId) throws NoSuchEntityException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2023 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131
* Karlsruhe, Germany.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.factories;

import de.fraunhofer.iosb.ilt.frostserver.model.core.Entity;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.JooqPersistenceManager;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.IncompleteEntityException;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.NoSuchEntityException;
import java.util.Map;
import org.jooq.Field;

/**
* A hook that can be registered on a table and will get executed before an
* insert happens.
*
* @author hylke
*/
public interface HookPostInsert extends JooqPmHook {

/**
*
* @param pm The Persistence Manager to use for database queries.
* @param entity The entity that is being inserted.
* @param insertFields The fields being inserted.
* @return true if the insert can continue, false if the insert should be
* skipped without error.
* @throws NoSuchEntityException If a related entity does not exist.
* @throws IncompleteEntityException If the entity is not complete.
*/
public boolean postInsertIntoDatabase(JooqPersistenceManager pm, Entity entity, Map<Field, Object> insertFields) throws NoSuchEntityException, IncompleteEntityException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2023 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131
* Karlsruhe, Germany.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.factories;

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.util.exception.IncompleteEntityException;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.NoSuchEntityException;

/**
* A hook that can be registered on a table and will get executed after an
* update has happened.
*
* @author hylke
*/
public interface HookPostUpdate extends JooqPmHook {

public void postUpdateInDatabase(JooqPersistenceManager pm, Entity entity, PkValue entityId) throws NoSuchEntityException, IncompleteEntityException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
import de.fraunhofer.iosb.ilt.frostserver.util.exception.NoSuchEntityException;

/**
* A hook that can be registered on a table and will get executed after a delete
* has happened.
* A hook that can be registered on a table and will get executed before a
* preDelete has happened.
*
* @author hylke
*/
public interface HookPreDelete {
public interface HookPreDelete extends JooqPmHook {

/**
*
Expand All @@ -36,5 +36,5 @@ public interface HookPreDelete {
* @throws NoSuchEntityException if something is wrong. This will cancel the
* action.
*/
public void delete(JooqPersistenceManager pm, PkValue entityId) throws NoSuchEntityException;
public void preDelete(JooqPersistenceManager pm, PkValue entityId) throws NoSuchEntityException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*
* @author hylke
*/
public interface HookPreInsert {
public interface HookPreInsert extends JooqPmHook {

/**
* The fase the pre-insert hook is running in.
Expand All @@ -44,7 +44,11 @@ public enum Phase {
* Phase after relations are loaded / created but before the entity
* itself is created.
*/
POST_RELATIONS
POST_RELATIONS,
/**
* Phase after the insert has been done.
*/
POST_INSERT
}

/**
Expand All @@ -58,6 +62,6 @@ public enum Phase {
* @throws NoSuchEntityException If a related entity does not exist.
* @throws IncompleteEntityException If the entity is not complete.
*/
public boolean insertIntoDatabase(Phase fase, JooqPersistenceManager pm, Entity entity, Map<Field, Object> insertFields) throws NoSuchEntityException, IncompleteEntityException;
public boolean preInsertIntoDatabase(Phase fase, JooqPersistenceManager pm, Entity entity, Map<Field, Object> insertFields) throws NoSuchEntityException, IncompleteEntityException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*
* @author hylke
*/
public interface HookPreUpdate {
public interface HookPreUpdate extends JooqPmHook {

public void updateInDatabase(JooqPersistenceManager pm, Entity entity, PkValue entityId) throws NoSuchEntityException, IncompleteEntityException;
public void preUpdateInDatabase(JooqPersistenceManager pm, Entity entity, PkValue entityId) throws NoSuchEntityException, IncompleteEntityException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2023 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131
* Karlsruhe, Germany.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.factories;

import de.fraunhofer.iosb.ilt.frostserver.model.loader.PmHook;

/**
* Interface for JooqPersistenceManager compatible Hooks.
*/
public interface JooqPmHook extends PmHook {

}
Loading

0 comments on commit 2eaabff

Please sign in to comment.