mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 13:10:44 +00:00
ONE-49: added getValueLocalDate to handle different types, updated getValue places to call new method
This commit is contained in:
@ -341,9 +341,9 @@ public class RunBackendStepInput extends AbstractActionInput
|
|||||||
** Getter for a single field's date value
|
** Getter for a single field's date value
|
||||||
**
|
**
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
public LocalDate getValueDate(String fieldName)
|
public LocalDate getValueLocalDate(String fieldName)
|
||||||
{
|
{
|
||||||
return ((LocalDate) getValue(fieldName));
|
return (ValueUtils.getValueAsLocalDate(getValue(fieldName)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -280,13 +280,11 @@ public class QRecord implements Serializable
|
|||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
** Getter for a single field's value
|
|
||||||
**
|
**
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
public LocalDate getValueDate(String fieldName)
|
public LocalDate getValueLocalDate(String fieldName)
|
||||||
{
|
{
|
||||||
// todo - rewrite using ValueUtils...
|
return (ValueUtils.getValueAsLocalDate(values.get(fieldName)));
|
||||||
return ((LocalDate) values.get(fieldName));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -23,6 +23,12 @@ package com.kingsrook.qqq.backend.core.utils;
|
|||||||
|
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.TimeZone;
|
||||||
import com.kingsrook.qqq.backend.core.exceptions.QValueException;
|
import com.kingsrook.qqq.backend.core.exceptions.QValueException;
|
||||||
|
|
||||||
|
|
||||||
@ -31,6 +37,9 @@ import com.kingsrook.qqq.backend.core.exceptions.QValueException;
|
|||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
public class ValueUtils
|
public class ValueUtils
|
||||||
{
|
{
|
||||||
|
private static final DateTimeFormatter localDateDefaultFormatter = DateTimeFormatter.ofPattern("yyyy-MMM-dd");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
** Type-safely make a String from any Object.
|
** Type-safely make a String from any Object.
|
||||||
@ -176,6 +185,59 @@ public class ValueUtils
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
** Type-safely make a LocalDate from any Object.
|
||||||
|
** null and empty-string inputs return null.
|
||||||
|
** We may throw if the input can't be converted to a LocalDate
|
||||||
|
*******************************************************************************/
|
||||||
|
public static LocalDate getValueAsLocalDate(Object value) throws QValueException
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if(value == null)
|
||||||
|
{
|
||||||
|
return (null);
|
||||||
|
}
|
||||||
|
else if(value instanceof java.sql.Date d)
|
||||||
|
{
|
||||||
|
return d.toLocalDate();
|
||||||
|
}
|
||||||
|
else if(value instanceof java.util.Date d)
|
||||||
|
{
|
||||||
|
return d.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
|
||||||
|
}
|
||||||
|
else if(value instanceof Calendar c)
|
||||||
|
{
|
||||||
|
TimeZone tz = c.getTimeZone();
|
||||||
|
ZoneId zid = (tz == null) ? ZoneId.systemDefault() : tz.toZoneId();
|
||||||
|
return LocalDateTime.ofInstant(c.toInstant(), zid).toLocalDate();
|
||||||
|
}
|
||||||
|
else if(value instanceof LocalDateTime ldt)
|
||||||
|
{
|
||||||
|
return ldt.toLocalDate();
|
||||||
|
}
|
||||||
|
else if(value instanceof String s)
|
||||||
|
{
|
||||||
|
if(!StringUtils.hasContent(s))
|
||||||
|
{
|
||||||
|
return (null);
|
||||||
|
}
|
||||||
|
|
||||||
|
return LocalDate.parse(s, localDateDefaultFormatter);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw (new IllegalArgumentException("Unsupported class " + value.getClass().getName() + " for converting to BigDecimal."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
throw (new QValueException("Value [" + value + "] could not be converted to an BigDecimal.", e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
** Type-safely make a BigDecimal from any Object.
|
** Type-safely make a BigDecimal from any Object.
|
||||||
** null and empty-string inputs return null.
|
** null and empty-string inputs return null.
|
||||||
|
@ -43,7 +43,7 @@ public class GetAgeStatistics implements BackendStep
|
|||||||
LocalDate now = LocalDate.now();
|
LocalDate now = LocalDate.now();
|
||||||
for(QRecord record : runBackendStepInput.getRecords())
|
for(QRecord record : runBackendStepInput.getRecords())
|
||||||
{
|
{
|
||||||
LocalDate birthDate = record.getValueDate("birthDate");
|
LocalDate birthDate = record.getValueLocalDate("birthDate");
|
||||||
Period until = birthDate.until(now);
|
Period until = birthDate.until(now);
|
||||||
int age = until.getYears();
|
int age = until.getYears();
|
||||||
record.setValue("age", age);
|
record.setValue("age", age);
|
||||||
|
Reference in New Issue
Block a user