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,60 @@
|
|||||||
|
/*
|
||||||
|
* 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.utils;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||||
|
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
** Utility methods for working with QRecords (and the values they contain)
|
||||||
|
*******************************************************************************/
|
||||||
|
public class QRecordUtils
|
||||||
|
{
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
** given 2 records, and a collection of fields, identify any fields that are
|
||||||
|
** not equals between the records.
|
||||||
|
*******************************************************************************/
|
||||||
|
public static List<QFieldMetaData> getChangedFields(QRecord a, QRecord b, Collection<QFieldMetaData> fields)
|
||||||
|
{
|
||||||
|
List<QFieldMetaData> changedFields = new ArrayList<>();
|
||||||
|
for(QFieldMetaData field : CollectionUtils.nonNullCollection(fields))
|
||||||
|
{
|
||||||
|
Serializable valueA = ValueUtils.getValueAsFieldType(field.getType(), a == null ? null : a.getValue(field.getName()));
|
||||||
|
Serializable valueB = ValueUtils.getValueAsFieldType(field.getType(), b == null ? null : b.getValue(field.getName()));
|
||||||
|
if(!Objects.equals(valueA, valueB))
|
||||||
|
{
|
||||||
|
changedFields.add(field);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (changedFields);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
* 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.utils;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import com.kingsrook.qqq.backend.core.BaseTest;
|
||||||
|
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.fields.QFieldType;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
** Unit test for QRecordUtils
|
||||||
|
*******************************************************************************/
|
||||||
|
class QRecordUtilsTest extends BaseTest
|
||||||
|
{
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
@Test
|
||||||
|
void testGetChangedFields()
|
||||||
|
{
|
||||||
|
QFieldMetaData id = new QFieldMetaData("id", QFieldType.INTEGER);
|
||||||
|
QFieldMetaData name = new QFieldMetaData("name", QFieldType.STRING);
|
||||||
|
|
||||||
|
assertEquals(Collections.emptyList(), QRecordUtils.getChangedFields(null, null, null));
|
||||||
|
assertEquals(Collections.emptyList(), QRecordUtils.getChangedFields(new QRecord(), null, null));
|
||||||
|
assertEquals(Collections.emptyList(), QRecordUtils.getChangedFields(null, new QRecord(), null));
|
||||||
|
assertEquals(Collections.emptyList(), QRecordUtils.getChangedFields(null, null, List.of(id)));
|
||||||
|
assertEquals(Collections.emptyList(), QRecordUtils.getChangedFields(new QRecord(), new QRecord(), List.of(id)));
|
||||||
|
assertEquals(Collections.emptyList(), QRecordUtils.getChangedFields(new QRecord().withValue("id", 1), new QRecord().withValue("id", 1), List.of(id)));
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////
|
||||||
|
// show that we ignore fields that aren't in the list of fields //
|
||||||
|
//////////////////////////////////////////////////////////////////
|
||||||
|
assertEquals(Collections.emptyList(), QRecordUtils.getChangedFields(new QRecord().withValue("id", 1), new QRecord().withValue("id", 2), List.of(name)));
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
// show that we'll "type-convert" the values, so 1 == "1" //
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
assertEquals(Collections.emptyList(), QRecordUtils.getChangedFields(new QRecord().withValue("id", 1), new QRecord().withValue("id", "1"), List.of(id)));
|
||||||
|
|
||||||
|
assertEquals(List.of(id), QRecordUtils.getChangedFields(new QRecord().withValue("id", 1), new QRecord().withValue("id", 2), List.of(id)));
|
||||||
|
assertEquals(List.of(id), QRecordUtils.getChangedFields(new QRecord(), new QRecord().withValue("id", 2), List.of(id)));
|
||||||
|
assertEquals(List.of(id), QRecordUtils.getChangedFields(null, new QRecord().withValue("id", 2), List.of(id)));
|
||||||
|
assertEquals(List.of(id), QRecordUtils.getChangedFields(new QRecord().withValue("id", 1), new QRecord(), List.of(id)));
|
||||||
|
assertEquals(List.of(id), QRecordUtils.getChangedFields(new QRecord().withValue("id", 1), null, List.of(id)));
|
||||||
|
assertEquals(List.of(id, name), QRecordUtils.getChangedFields(new QRecord().withValue("id", 1).withValue("name", "Bob"), new QRecord().withValue("id", 2).withValue("name", "N."), List.of(id, name)));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user