Skip to content

Commit

Permalink
Reworked special handling of tables into hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed May 18, 2024
1 parent 2eaabff commit 85754d4
Show file tree
Hide file tree
Showing 16 changed files with 459 additions and 232 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,22 @@ public NavigationPropertyMain getNavigationProperty(String name) {
return null;
}

public NavigationPropertyEntity getNavigationPropertyEntity(String name) {
Property property = propertiesByName.get(name);
if (property instanceof NavigationPropertyEntity npe) {
return npe;
}
return null;
}

public NavigationPropertyEntitySet getNavigationPropertyEntitySet(String name) {
Property property = propertiesByName.get(name);
if (property instanceof NavigationPropertyEntitySet npes) {
return npes;
}
return null;
}

/**
* The Set of PROPERTIES that Entities of this type have.
*
Expand Down
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 @@ -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
@@ -0,0 +1,48 @@
/*
* 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.plugin.coremodel;

import de.fraunhofer.iosb.ilt.frostserver.model.core.PkValue;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.JooqPersistenceManager;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.factories.HookPostDelete;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.tables.TableCollection;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.NoSuchEntityException;
import org.jooq.TableField;
import org.jooq.impl.DSL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* @author hylke
*/
class HookPostDeleteLocation implements HookPostDelete {

private static final Logger LOGGER = LoggerFactory.getLogger(HookPostDeleteLocation.class.getName());

@Override
public void postDelete(JooqPersistenceManager pm, PkValue entityId) throws NoSuchEntityException {
final TableCollection tables = pm.getTableCollection();
// Also postDelete all historicalLocations that no longer reference any location
TableImpHistLocations thl = tables.getTableForClass(TableImpHistLocations.class);
TableImpLocationsHistLocations tlhl = tables.getTableForClass(TableImpLocationsHistLocations.class);
int count = pm.getDslContext().delete(thl).where(((TableField) thl.getId()).in(DSL.select(thl.getId()).from(thl).leftJoin(tlhl).on(((TableField) thl.getId()).eq(tlhl.getHistLocationId())).where(tlhl.getLocationId().isNull()))).execute();
LOGGER.debug("Deleted {} HistoricalLocations", count);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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.plugin.coremodel;

import de.fraunhofer.iosb.ilt.frostserver.model.EntityType;
import de.fraunhofer.iosb.ilt.frostserver.model.core.Entity;
import de.fraunhofer.iosb.ilt.frostserver.model.ext.TimeInstant;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.JooqPersistenceManager;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.factories.EntityFactories;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.factories.HookPostInsert;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.tables.TableCollection;
import de.fraunhofer.iosb.ilt.frostserver.property.EntityPropertyMain;
import de.fraunhofer.iosb.ilt.frostserver.property.NavigationPropertyMain.NavigationPropertyEntity;
import de.fraunhofer.iosb.ilt.frostserver.property.NavigationPropertyMain.NavigationPropertyEntitySet;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.IncompleteEntityException;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.NoSuchEntityException;
import java.util.Collections;
import java.util.Map;
import net.time4j.Moment;
import org.jooq.DSLContext;
import org.jooq.Field;
import org.jooq.Record;
import org.jooq.TableField;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* @author hylke
*/
class HookPostInsertHistLoc implements HookPostInsert {

private static final Logger LOGGER = LoggerFactory.getLogger(HookPostInsertHistLoc.class.getName());

@Override
public boolean postInsertIntoDatabase(JooqPersistenceManager pm, Entity histLoc, Map<Field, Object> insertFields) throws NoSuchEntityException, IncompleteEntityException {
final EntityFactories ef = pm.getEntityFactories();
final TableCollection tc = pm.getTableCollection();
final EntityType etHistLoc = histLoc.getEntityType();
final NavigationPropertyEntity npThing = (NavigationPropertyEntity) etHistLoc.getNavigationProperty("Thing");
final NavigationPropertyEntitySet npLocations = (NavigationPropertyEntitySet) etHistLoc.getNavigationProperty("Locations");
final EntityPropertyMain<TimeInstant> epTime = etHistLoc.getEntityProperty("time");
Entity thing = histLoc.getProperty(npThing);
Object thingId = thing.getPrimaryKeyValues().get(0);
DSLContext dslContext = pm.getDslContext();
TableImpHistLocations thl = tc.getTableForClass(TableImpHistLocations.class);
final TimeInstant hlTime = histLoc.getProperty(epTime);
Moment newTime = hlTime.getDateTime();
// https://github.com/opengeospatial/sensorthings/issues/30
// Check the time of the latest HistoricalLocation of our thing.
// If this time is earlier than our time, set the Locations of our Thing to our Locations.
Record lastHistLocation = dslContext.select(Collections.emptyList()).from(thl).where(((TableField) thl.getThingId()).eq(thingId).and(thl.time.gt(newTime))).orderBy(thl.time.desc()).limit(1).fetchOne();
if (lastHistLocation == null) {
// We are the newest.
// Unlink old Locations from Thing.
TableImpThingsLocations qtl = tc.getTableForClass(TableImpThingsLocations.class);
long count = dslContext.delete(qtl).where(((TableField) qtl.getThingId()).eq(thingId)).execute();
LOGGER.debug(EntityFactories.UNLINKED_L_FROM_T, count, thingId);
// Link new locations to Thing.
for (Entity l : histLoc.getProperty(npLocations)) {
if (!l.getPrimaryKeyValues().isFullySet() || !ef.entityExists(pm, l, true)) {
throw new NoSuchEntityException("Location with no id.");
}
Object locationId = l.getPrimaryKeyValues().get(0);
dslContext.insertInto(qtl).set((TableField) qtl.getThingId(), thingId).set(qtl.getLocationId(), locationId).execute();
LOGGER.debug(EntityFactories.LINKED_L_TO_T, locationId, thingId);
}
}
return true;
}

}
Loading

0 comments on commit 85754d4

Please sign in to comment.