mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 13:10:44 +00:00
Initial checkin
This commit is contained in:
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<String, Change> getChanges(String tableName, QRecord oldRecord, QRecord newRecord)
|
||||
{
|
||||
Map<String, Change> rs = new HashMap<>();
|
||||
|
||||
QTableMetaData table = QContext.getQInstance().getTable(tableName);
|
||||
for(Map.Entry<String, Serializable> 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));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<String, Change> 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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user