ONE-49: added getValueLocalDate to handle different types, updated getValue places to call new method

This commit is contained in:
Tim Chamberlain
2022-07-26 16:29:56 -05:00
parent f1b2eef3b8
commit 7dc8643857
4 changed files with 67 additions and 7 deletions

View File

@ -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)));
} }

View File

@ -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));
} }

View File

@ -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.

View File

@ -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);