mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 05:01:07 +00:00
Add toQRecordOnlyChangedFields
This commit is contained in:
@ -31,8 +31,11 @@ import java.time.LocalDate;
|
|||||||
import java.time.LocalTime;
|
import java.time.LocalTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||||
import com.kingsrook.qqq.backend.core.exceptions.QRuntimeException;
|
import com.kingsrook.qqq.backend.core.exceptions.QRuntimeException;
|
||||||
@ -49,6 +52,8 @@ public abstract class QRecordEntity
|
|||||||
|
|
||||||
private static final ListingHash<Class<? extends QRecordEntity>, QRecordEntityField> fieldMapping = new ListingHash<>();
|
private static final ListingHash<Class<? extends QRecordEntity>, QRecordEntityField> fieldMapping = new ListingHash<>();
|
||||||
|
|
||||||
|
private Map<String, Serializable> originalRecordValues;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
@ -80,11 +85,13 @@ public abstract class QRecordEntity
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
List<QRecordEntityField> fieldList = getFieldList(this.getClass());
|
List<QRecordEntityField> fieldList = getFieldList(this.getClass());
|
||||||
|
originalRecordValues = new HashMap<>();
|
||||||
for(QRecordEntityField qRecordEntityField : fieldList)
|
for(QRecordEntityField qRecordEntityField : fieldList)
|
||||||
{
|
{
|
||||||
Serializable value = qRecord.getValue(qRecordEntityField.getFieldName());
|
Serializable value = qRecord.getValue(qRecordEntityField.getFieldName());
|
||||||
Object typedValue = qRecordEntityField.convertValueType(value);
|
Object typedValue = qRecordEntityField.convertValueType(value);
|
||||||
qRecordEntityField.getSetter().invoke(this, typedValue);
|
qRecordEntityField.getSetter().invoke(this, typedValue);
|
||||||
|
originalRecordValues.put(qRecordEntityField.getFieldName(), value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch(Exception e)
|
catch(Exception e)
|
||||||
@ -121,6 +128,41 @@ public abstract class QRecordEntity
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
public QRecord toQRecordOnlyChangedFields()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
QRecord qRecord = new QRecord();
|
||||||
|
|
||||||
|
List<QRecordEntityField> fieldList = getFieldList(this.getClass());
|
||||||
|
for(QRecordEntityField qRecordEntityField : fieldList)
|
||||||
|
{
|
||||||
|
Serializable thisValue = (Serializable) qRecordEntityField.getGetter().invoke(this);
|
||||||
|
Serializable originalValue = null;
|
||||||
|
if(originalRecordValues != null)
|
||||||
|
{
|
||||||
|
originalValue = originalRecordValues.get(qRecordEntityField.getFieldName());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!Objects.equals(thisValue, originalValue))
|
||||||
|
{
|
||||||
|
qRecord.setValue(qRecordEntityField.getFieldName(), thisValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (qRecord);
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
throw (new QRuntimeException("Error building qRecord from entity.", e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
**
|
**
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
@ -67,6 +67,35 @@ class QRecordEntityTest extends BaseTest
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
@Test
|
||||||
|
void testItemToQRecordOnlyChangedFields() throws QException
|
||||||
|
{
|
||||||
|
Item item = new Item(new QRecord()
|
||||||
|
.withValue("sku", "ABC-123")
|
||||||
|
.withValue("description", null)
|
||||||
|
.withValue("quantity", 47)
|
||||||
|
.withValue("price", new BigDecimal("3.50"))
|
||||||
|
.withValue("isFeatured", true));
|
||||||
|
|
||||||
|
QRecord qRecordOnlyChangedFields = item.toQRecordOnlyChangedFields();
|
||||||
|
assertTrue(qRecordOnlyChangedFields.getValues().isEmpty());
|
||||||
|
|
||||||
|
item.setDescription("My Changed Item");
|
||||||
|
qRecordOnlyChangedFields = item.toQRecordOnlyChangedFields();
|
||||||
|
assertEquals(1, qRecordOnlyChangedFields.getValues().size());
|
||||||
|
assertEquals("My Changed Item", qRecordOnlyChangedFields.getValueString("description"));
|
||||||
|
|
||||||
|
item.setPrice(null);
|
||||||
|
qRecordOnlyChangedFields = item.toQRecordOnlyChangedFields();
|
||||||
|
assertEquals(2, qRecordOnlyChangedFields.getValues().size());
|
||||||
|
assertNull(qRecordOnlyChangedFields.getValueString("price"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
**
|
**
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
@ -24,6 +24,7 @@ package com.kingsrook.qqq.backend.core.model.data.testentities;
|
|||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import com.kingsrook.qqq.backend.core.model.data.QField;
|
import com.kingsrook.qqq.backend.core.model.data.QField;
|
||||||
|
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||||
import com.kingsrook.qqq.backend.core.model.data.QRecordEntity;
|
import com.kingsrook.qqq.backend.core.model.data.QRecordEntity;
|
||||||
import com.kingsrook.qqq.backend.core.model.metadata.fields.DisplayFormat;
|
import com.kingsrook.qqq.backend.core.model.metadata.fields.DisplayFormat;
|
||||||
|
|
||||||
@ -49,6 +50,27 @@ public class Item extends QRecordEntity
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
** Constructor
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
public Item()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
** Constructor
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
public Item(QRecord qRecord)
|
||||||
|
{
|
||||||
|
populateFromQRecord(qRecord);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
** Getter for sku
|
** Getter for sku
|
||||||
**
|
**
|
||||||
|
Reference in New Issue
Block a user