mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-20 06:00:44 +00:00
Add toQRecordOnlyChangedFields
This commit is contained in:
@ -31,8 +31,11 @@ import java.time.LocalDate;
|
||||
import java.time.LocalTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
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 Map<String, Serializable> originalRecordValues;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
@ -80,11 +85,13 @@ public abstract class QRecordEntity
|
||||
try
|
||||
{
|
||||
List<QRecordEntityField> fieldList = getFieldList(this.getClass());
|
||||
originalRecordValues = new HashMap<>();
|
||||
for(QRecordEntityField qRecordEntityField : fieldList)
|
||||
{
|
||||
Serializable value = qRecord.getValue(qRecordEntityField.getFieldName());
|
||||
Object typedValue = qRecordEntityField.convertValueType(value);
|
||||
qRecordEntityField.getSetter().invoke(this, typedValue);
|
||||
originalRecordValues.put(qRecordEntityField.getFieldName(), value);
|
||||
}
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
|
Reference in New Issue
Block a user