From dd63e8d4e2997945eb1e42bc16820f57e6fed140 Mon Sep 17 00:00:00 2001 From: Darin Kelkhoff Date: Fri, 12 May 2023 16:48:27 -0500 Subject: [PATCH] Initial checkin --- .../RecordCustomizerUtilityInterface.java | 149 ++++++++++++++++++ .../RecordCustomizerUtilityInterfaceTest.java | 135 ++++++++++++++++ 2 files changed, 284 insertions(+) create mode 100644 qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/actions/customizers/RecordCustomizerUtilityInterface.java create mode 100644 qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/actions/customizers/RecordCustomizerUtilityInterfaceTest.java diff --git a/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/actions/customizers/RecordCustomizerUtilityInterface.java b/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/actions/customizers/RecordCustomizerUtilityInterface.java new file mode 100644 index 00000000..ff8c2d82 --- /dev/null +++ b/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/actions/customizers/RecordCustomizerUtilityInterface.java @@ -0,0 +1,149 @@ +/* + * QQQ - Low-code Application Framework for Engineers. + * Copyright (C) 2021-2023. Kingsrook, LLC + * 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States + * contact@kingsrook.com + * https://github.com/Kingsrook/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.kingsrook.qqq.backend.core.actions.customizers; + + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import com.kingsrook.qqq.backend.core.context.QContext; +import com.kingsrook.qqq.backend.core.logging.QLogger; +import com.kingsrook.qqq.backend.core.model.data.QRecord; +import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData; +import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData; +import com.kingsrook.qqq.backend.core.model.statusmessages.BadInputStatusMessage; +import com.kingsrook.qqq.backend.core.utils.StringUtils; +import com.kingsrook.qqq.backend.core.utils.ValueUtils; +import static com.kingsrook.qqq.backend.core.logging.LogUtils.logPair; + + +/******************************************************************************* + ** Interface with utility methods that pre insert/update/delete customizers + ** may want to use. + *******************************************************************************/ +public interface RecordCustomizerUtilityInterface +{ + QLogger LOG = QLogger.getLogger(RecordCustomizerUtilityInterface.class); + + + + /******************************************************************************* + ** Container for an old value and a new value. + *******************************************************************************/ + @SuppressWarnings("checkstyle:MethodName") + record Change(Serializable oldValue, Serializable newValue) + { + } + + + /******************************************************************************* + ** + *******************************************************************************/ + default Map getChanges(String tableName, QRecord oldRecord, QRecord newRecord) + { + Map rs = new HashMap<>(); + + QTableMetaData table = QContext.getQInstance().getTable(tableName); + for(Map.Entry entry : newRecord.getValues().entrySet()) + { + String fieldName = entry.getKey(); + Serializable newValue = entry.getValue(); + Serializable oldValue = oldRecord.getValue(fieldName); + + try + { + QFieldMetaData field = table.getField(fieldName); + Serializable newTypedValue = ValueUtils.getValueAsFieldType(field.getType(), newValue); + Serializable oldTypedValue = ValueUtils.getValueAsFieldType(field.getType(), oldValue); + + if(!Objects.equals(oldTypedValue, newTypedValue)) + { + rs.put(fieldName, new Change(oldTypedValue, newTypedValue)); + } + } + catch(Exception e) + { + LOG.info("Error getting a value as field's type", e, logPair("fieldName", fieldName), logPair("oldValue", oldValue), logPair("newValue", newValue)); + } + } + + return (rs); + } + + /******************************************************************************* + ** + *******************************************************************************/ + default void errorIfNoValue(Serializable value, QRecord record, String errorMessage) + { + errorIf(!StringUtils.hasContent(ValueUtils.getValueAsString(value)), record, errorMessage); + } + + + /******************************************************************************* + ** + *******************************************************************************/ + default void errorIfEditedValue(QRecord oldRecord, QRecord newRecord, String fieldName, String errorMessage) + { + if(newRecord.getValues().containsKey(fieldName)) + { + errorIf(isChangedValue(oldRecord.getValue(fieldName), newRecord.getValue(fieldName)), newRecord, errorMessage); + } + } + + + /******************************************************************************* + ** + *******************************************************************************/ + default boolean isChangedValue(Serializable oldValue, Serializable newValue) + { + ////////////////////////////////////////////// + // todo - probably ... some type "coercion" // + ////////////////////////////////////////////// + return (!Objects.equals(oldValue, newValue)); + } + + + /******************************************************************************* + ** + *******************************************************************************/ + default void errorIfAnyValue(Serializable value, QRecord record, String errorMessage) + { + if(StringUtils.hasContent(ValueUtils.getValueAsString(value))) + { + record.addError(new BadInputStatusMessage(errorMessage)); + } + } + + + /******************************************************************************* + ** + *******************************************************************************/ + default void errorIf(boolean condition, QRecord record, String errorMessage) + { + if(condition) + { + record.addError(new BadInputStatusMessage(errorMessage)); + } + } + +} diff --git a/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/actions/customizers/RecordCustomizerUtilityInterfaceTest.java b/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/actions/customizers/RecordCustomizerUtilityInterfaceTest.java new file mode 100644 index 00000000..ad28afcd --- /dev/null +++ b/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/actions/customizers/RecordCustomizerUtilityInterfaceTest.java @@ -0,0 +1,135 @@ +/* + * QQQ - Low-code Application Framework for Engineers. + * Copyright (C) 2021-2023. Kingsrook, LLC + * 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States + * contact@kingsrook.com + * https://github.com/Kingsrook/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.kingsrook.qqq.backend.core.actions.customizers; + + +import java.util.Map; +import com.kingsrook.qqq.backend.core.BaseTest; +import com.kingsrook.qqq.backend.core.model.data.QRecord; +import com.kingsrook.qqq.backend.core.utils.TestUtils; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + + +/******************************************************************************* + ** Unit test for RecordCustomizerUtilityInterface + *******************************************************************************/ +class RecordCustomizerUtilityInterfaceTest extends BaseTest implements RecordCustomizerUtilityInterface +{ + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void testGetChanges() + { + Map changes = getChanges(TestUtils.TABLE_NAME_PERSON_MEMORY, + new QRecord().withValue("id", 1).withValue("firstName", "Homer"), + new QRecord().withValue("id", 2).withValue("firstName", "Homer")); + + assertEquals(1, changes.size()); + assertEquals(new Change(1, 2), changes.get("id")); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void testErrorIfNoValue() + { + { + QRecord record = new QRecord(); + errorIfNoValue(null, record, "no value"); + assertEquals(1, record.getErrors().size()); + assertEquals("no value", record.getErrors().get(0).getMessage()); + } + { + QRecord record = new QRecord(); + errorIfNoValue("", record, "no value"); + assertEquals(1, record.getErrors().size()); + assertEquals("no value", record.getErrors().get(0).getMessage()); + } + { + QRecord record = new QRecord(); + errorIfNoValue("hi", record, "no value"); + assertEquals(0, record.getErrors().size()); + } + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void testErrorIfAnyValue() + { + { + QRecord record = new QRecord(); + errorIfAnyValue(null, record, "any value"); + assertEquals(0, record.getErrors().size()); + } + { + QRecord record = new QRecord(); + errorIfAnyValue("", record, "any value"); + assertEquals(0, record.getErrors().size()); + } + { + QRecord record = new QRecord(); + errorIfAnyValue("hi", record, "any value"); + assertEquals(1, record.getErrors().size()); + assertEquals("any value", record.getErrors().get(0).getMessage()); + } + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void testErrorIfEditedValue() + { + { + QRecord oldRecord = new QRecord().withValue("id", 1).withValue("firstName", "Homer"); + QRecord newRecord = new QRecord().withValue("id", 1).withValue("firstName", "Marge"); + errorIfEditedValue(oldRecord, newRecord, "firstName", "changed firstName"); + assertEquals(1, newRecord.getErrors().size()); + assertEquals("changed firstName", newRecord.getErrors().get(0).getMessage()); + } + { + QRecord oldRecord = new QRecord().withValue("id", 1).withValue("firstName", "Homer"); + QRecord newRecord = new QRecord().withValue("id", 1).withValue("firstName", "Homer"); + errorIfEditedValue(oldRecord, newRecord, "firstName", "changed firstName"); + assertEquals(0, newRecord.getErrors().size()); + } + { + QRecord oldRecord = new QRecord().withValue("id", 1).withValue("firstName", "Homer"); + QRecord newRecord = new QRecord().withValue("id", 1); + errorIfEditedValue(oldRecord, newRecord, "firstName", "changed firstName"); + assertEquals(0, newRecord.getErrors().size()); + } + } + +} \ No newline at end of file