mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 13:10:44 +00:00
CE-1072 Add fallbackZoneId in case zoneIdFromFieldName isn't set or valid; More tests & validation
This commit is contained in:
@ -39,13 +39,16 @@ import static com.kingsrook.qqq.backend.core.logging.LogUtils.logPair;
|
|||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
**
|
** Field Display Behavior class for customizing the display values used
|
||||||
|
** in date-time fields
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
public class DateTimeDisplayValueBehavior implements FieldDisplayBehavior<DateTimeDisplayValueBehavior>
|
public class DateTimeDisplayValueBehavior implements FieldDisplayBehavior<DateTimeDisplayValueBehavior>
|
||||||
{
|
{
|
||||||
private static final QLogger LOG = QLogger.getLogger(DateTimeDisplayValueBehavior.class);
|
private static final QLogger LOG = QLogger.getLogger(DateTimeDisplayValueBehavior.class);
|
||||||
|
|
||||||
private String zoneIdFromFieldName;
|
private String zoneIdFromFieldName;
|
||||||
|
private String fallbackZoneId;
|
||||||
|
|
||||||
private String defaultZoneId;
|
private String defaultZoneId;
|
||||||
|
|
||||||
private static DateTimeDisplayValueBehavior NOOP = new DateTimeDisplayValueBehavior();
|
private static DateTimeDisplayValueBehavior NOOP = new DateTimeDisplayValueBehavior();
|
||||||
@ -113,13 +116,34 @@ public class DateTimeDisplayValueBehavior implements FieldDisplayBehavior<DateTi
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
Instant instant = record.getValueInstant(field.getName());
|
Instant instant = record.getValueInstant(field.getName());
|
||||||
String zoneId = record.getValueString(zoneIdFromFieldName);
|
String zoneString = record.getValueString(zoneIdFromFieldName);
|
||||||
ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of(zoneId));
|
|
||||||
|
ZoneId zoneId;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
zoneId = ZoneId.of(zoneString);
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// if the zone string from the other field isn't valid, and we have a fallback, try to use it //
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
if(StringUtils.hasContent(fallbackZoneId))
|
||||||
|
{
|
||||||
|
zoneId = ZoneId.of(fallbackZoneId);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw (e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ZonedDateTime zonedDateTime = instant.atZone(zoneId);
|
||||||
record.setDisplayValue(field.getName(), QValueFormatter.formatDateTimeWithZone(zonedDateTime));
|
record.setDisplayValue(field.getName(), QValueFormatter.formatDateTimeWithZone(zonedDateTime));
|
||||||
}
|
}
|
||||||
catch(Exception e)
|
catch(Exception e)
|
||||||
{
|
{
|
||||||
LOG.info("Error applying zoneIdFromFieldName DateTimeDisplayValueBehavior", logPair("table", table.getName()), logPair("field", field.getName()), logPair("id", record.getValue(table.getPrimaryKeyField())));
|
LOG.info("Error applying zoneIdFromFieldName DateTimeDisplayValueBehavior", e, logPair("table", table.getName()), logPair("field", field.getName()), logPair("id", record.getValue(table.getPrimaryKeyField())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -140,8 +164,16 @@ public class DateTimeDisplayValueBehavior implements FieldDisplayBehavior<DateTi
|
|||||||
errors.add("A DateTimeDisplayValueBehavior was a applied to a non-DATE_TIME" + errorSuffix);
|
errors.add("A DateTimeDisplayValueBehavior was a applied to a non-DATE_TIME" + errorSuffix);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////
|
||||||
|
// validate rules if zoneIdFromFieldName is set //
|
||||||
|
//////////////////////////////////////////////////
|
||||||
if(StringUtils.hasContent(zoneIdFromFieldName))
|
if(StringUtils.hasContent(zoneIdFromFieldName))
|
||||||
{
|
{
|
||||||
|
if(StringUtils.hasContent(defaultZoneId))
|
||||||
|
{
|
||||||
|
errors.add("You may not specify both zoneIdFromFieldName and defaultZoneId in DateTimeDisplayValueBehavior on" + errorSuffix);
|
||||||
|
}
|
||||||
|
|
||||||
if(!tableMetaData.getFields().containsKey(zoneIdFromFieldName))
|
if(!tableMetaData.getFields().containsKey(zoneIdFromFieldName))
|
||||||
{
|
{
|
||||||
errors.add("Unrecognized field name [" + zoneIdFromFieldName + "] for [zoneIdFromFieldName] in DateTimeDisplayValueBehavior on" + errorSuffix);
|
errors.add("Unrecognized field name [" + zoneIdFromFieldName + "] for [zoneIdFromFieldName] in DateTimeDisplayValueBehavior on" + errorSuffix);
|
||||||
@ -156,6 +188,50 @@ public class DateTimeDisplayValueBehavior implements FieldDisplayBehavior<DateTi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////
|
||||||
|
// validate rules if defaultZoneId is set //
|
||||||
|
////////////////////////////////////////////
|
||||||
|
if(StringUtils.hasContent(defaultZoneId))
|
||||||
|
{
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// would check that you didn't specify from zoneIdFromFieldName - but that's covered above //
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
if(StringUtils.hasContent(fallbackZoneId))
|
||||||
|
{
|
||||||
|
errors.add("You may not specify both defaultZoneId and fallbackZoneId in DateTimeDisplayValueBehavior on" + errorSuffix);
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ZoneId.of(defaultZoneId);
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
errors.add("Invalid ZoneId [" + defaultZoneId + "] for [defaultZoneId] in DateTimeDisplayValueBehavior on" + errorSuffix + "; " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/////////////////////////////////////////////
|
||||||
|
// validate rules if fallbackZoneId is set //
|
||||||
|
/////////////////////////////////////////////
|
||||||
|
if(StringUtils.hasContent(fallbackZoneId))
|
||||||
|
{
|
||||||
|
if(!StringUtils.hasContent(zoneIdFromFieldName))
|
||||||
|
{
|
||||||
|
errors.add("You may only set fallbackZoneId if using zoneIdFromFieldName in DateTimeDisplayValueBehavior on" + errorSuffix);
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ZoneId.of(fallbackZoneId);
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
errors.add("Invalid ZoneId [" + fallbackZoneId + "] for [fallbackZoneId] in DateTimeDisplayValueBehavior on" + errorSuffix + "; " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (errors);
|
return (errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,6 +267,7 @@ public class DateTimeDisplayValueBehavior implements FieldDisplayBehavior<DateTi
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
** Getter for defaultZoneId
|
** Getter for defaultZoneId
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
@ -221,4 +298,34 @@ public class DateTimeDisplayValueBehavior implements FieldDisplayBehavior<DateTi
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
** Getter for fallbackZoneId
|
||||||
|
*******************************************************************************/
|
||||||
|
public String getFallbackZoneId()
|
||||||
|
{
|
||||||
|
return (this.fallbackZoneId);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
** Setter for fallbackZoneId
|
||||||
|
*******************************************************************************/
|
||||||
|
public void setFallbackZoneId(String fallbackZoneId)
|
||||||
|
{
|
||||||
|
this.fallbackZoneId = fallbackZoneId;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
** Fluent setter for fallbackZoneId
|
||||||
|
*******************************************************************************/
|
||||||
|
public DateTimeDisplayValueBehavior withFallbackZoneId(String fallbackZoneId)
|
||||||
|
{
|
||||||
|
this.fallbackZoneId = fallbackZoneId;
|
||||||
|
return (this);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,8 @@ package com.kingsrook.qqq.backend.core.model.metadata.fields;
|
|||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Function;
|
||||||
import com.kingsrook.qqq.backend.core.BaseTest;
|
import com.kingsrook.qqq.backend.core.BaseTest;
|
||||||
import com.kingsrook.qqq.backend.core.actions.values.ValueBehaviorApplier;
|
import com.kingsrook.qqq.backend.core.actions.values.ValueBehaviorApplier;
|
||||||
import com.kingsrook.qqq.backend.core.context.QContext;
|
import com.kingsrook.qqq.backend.core.context.QContext;
|
||||||
@ -32,6 +34,7 @@ import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
|
|||||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
|
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
|
||||||
import com.kingsrook.qqq.backend.core.utils.TestUtils;
|
import com.kingsrook.qqq.backend.core.utils.TestUtils;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
|
||||||
@ -45,7 +48,7 @@ class DateTimeDisplayValueBehaviorTest extends BaseTest
|
|||||||
**
|
**
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
@Test
|
@Test
|
||||||
void test()
|
void testZoneIdFromFieldName()
|
||||||
{
|
{
|
||||||
QInstance qInstance = QContext.getQInstance();
|
QInstance qInstance = QContext.getQInstance();
|
||||||
QTableMetaData table = qInstance.getTable(TestUtils.TABLE_NAME_PERSON_MEMORY);
|
QTableMetaData table = qInstance.getTable(TestUtils.TABLE_NAME_PERSON_MEMORY);
|
||||||
@ -58,4 +61,109 @@ class DateTimeDisplayValueBehaviorTest extends BaseTest
|
|||||||
assertEquals("2024-04-04 02:12:00 PM CDT", record.getDisplayValue("createDate"));
|
assertEquals("2024-04-04 02:12:00 PM CDT", record.getDisplayValue("createDate"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
@Test
|
||||||
|
void testZoneIdFromFieldNameWithFallback()
|
||||||
|
{
|
||||||
|
QInstance qInstance = QContext.getQInstance();
|
||||||
|
QTableMetaData table = qInstance.getTable(TestUtils.TABLE_NAME_PERSON_MEMORY);
|
||||||
|
|
||||||
|
table.withField(new QFieldMetaData("timeZone", QFieldType.STRING));
|
||||||
|
table.getField("createDate").withBehavior(new DateTimeDisplayValueBehavior().withZoneIdFromFieldName("timeZone").withFallbackZoneId("America/Denver"));
|
||||||
|
|
||||||
|
QRecord record = new QRecord().withValue("createDate", Instant.parse("2024-04-04T19:12:00Z")).withValue("timeZone", "whodis");
|
||||||
|
ValueBehaviorApplier.applyFieldBehaviors(ValueBehaviorApplier.Action.FORMATTING, qInstance, table, List.of(record), null);
|
||||||
|
assertEquals("2024-04-04 01:12:00 PM MDT", record.getDisplayValue("createDate"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
@Test
|
||||||
|
void testDefaultZoneId()
|
||||||
|
{
|
||||||
|
QInstance qInstance = QContext.getQInstance();
|
||||||
|
QTableMetaData table = qInstance.getTable(TestUtils.TABLE_NAME_PERSON_MEMORY);
|
||||||
|
|
||||||
|
table.withField(new QFieldMetaData("timeZone", QFieldType.STRING));
|
||||||
|
table.getField("createDate").withBehavior(new DateTimeDisplayValueBehavior().withDefaultZoneId("America/Los_Angeles"));
|
||||||
|
|
||||||
|
QRecord record = new QRecord().withValue("createDate", Instant.parse("2024-04-04T19:12:00Z"));
|
||||||
|
ValueBehaviorApplier.applyFieldBehaviors(ValueBehaviorApplier.Action.FORMATTING, qInstance, table, List.of(record), null);
|
||||||
|
assertEquals("2024-04-04 12:12:00 PM PDT", record.getDisplayValue("createDate"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
@Test
|
||||||
|
void testValidation()
|
||||||
|
{
|
||||||
|
QInstance qInstance = QContext.getQInstance();
|
||||||
|
QTableMetaData table = qInstance.getTable(TestUtils.TABLE_NAME_PERSON_MEMORY);
|
||||||
|
QFieldMetaData field = table.getField("createDate");
|
||||||
|
table.withField(new QFieldMetaData("timeZone", QFieldType.STRING));
|
||||||
|
|
||||||
|
Function<Consumer<DateTimeDisplayValueBehavior>, List<String>> testOne = setup ->
|
||||||
|
{
|
||||||
|
DateTimeDisplayValueBehavior dateTimeDisplayValueBehavior = new DateTimeDisplayValueBehavior();
|
||||||
|
setup.accept(dateTimeDisplayValueBehavior);
|
||||||
|
return (dateTimeDisplayValueBehavior.validateBehaviorConfiguration(table, field));
|
||||||
|
};
|
||||||
|
|
||||||
|
///////////////////
|
||||||
|
// valid configs //
|
||||||
|
///////////////////
|
||||||
|
assertThat(testOne.apply(b -> b.toString())).isEmpty(); // default setup (noop use-case) is valid
|
||||||
|
assertThat(testOne.apply(b -> b.withZoneIdFromFieldName("timeZone"))).isEmpty();
|
||||||
|
assertThat(testOne.apply(b -> b.withZoneIdFromFieldName("timeZone").withFallbackZoneId("UTC"))).isEmpty();
|
||||||
|
assertThat(testOne.apply(b -> b.withZoneIdFromFieldName("timeZone").withFallbackZoneId("America/Chicago"))).isEmpty();
|
||||||
|
assertThat(testOne.apply(b -> b.withDefaultZoneId("UTC"))).isEmpty();
|
||||||
|
assertThat(testOne.apply(b -> b.withDefaultZoneId("America/Chicago"))).isEmpty();
|
||||||
|
|
||||||
|
/////////////////////
|
||||||
|
// invalid configs //
|
||||||
|
/////////////////////
|
||||||
|
assertThat(testOne.apply(b -> b.withZoneIdFromFieldName("notAField")))
|
||||||
|
.hasSize(1).first().asString()
|
||||||
|
.contains("Unrecognized field name");
|
||||||
|
|
||||||
|
assertThat(testOne.apply(b -> b.withZoneIdFromFieldName("id")))
|
||||||
|
.hasSize(1).first().asString()
|
||||||
|
.contains("A non-STRING type [INTEGER] was specified as the zoneIdFromFieldName field");
|
||||||
|
|
||||||
|
assertThat(testOne.apply(b -> b.withZoneIdFromFieldName("timeZone").withDefaultZoneId("UTC")))
|
||||||
|
.hasSize(1).first().asString()
|
||||||
|
.contains("You may not specify both zoneIdFromFieldName and defaultZoneId");
|
||||||
|
|
||||||
|
assertThat(testOne.apply(b -> b.withDefaultZoneId("UTC").withFallbackZoneId("UTC")))
|
||||||
|
.hasSize(2)
|
||||||
|
.anyMatch(s -> s.contains("You may not specify both defaultZoneId and fallbackZoneId"))
|
||||||
|
.anyMatch(s -> s.contains("You may only set fallbackZoneId if using zoneIdFromFieldName"));
|
||||||
|
|
||||||
|
assertThat(testOne.apply(b -> b.withFallbackZoneId("UTC")))
|
||||||
|
.hasSize(1).first().asString()
|
||||||
|
.contains("You may only set fallbackZoneId if using zoneIdFromFieldName");
|
||||||
|
|
||||||
|
assertThat(testOne.apply(b -> b.withDefaultZoneId("notAZone")))
|
||||||
|
.hasSize(1).first().asString()
|
||||||
|
.contains("Invalid ZoneId [notAZone] for [defaultZoneId]");
|
||||||
|
|
||||||
|
assertThat(testOne.apply(b -> b.withZoneIdFromFieldName("timeZone").withFallbackZoneId("notAZone")))
|
||||||
|
.hasSize(1).first().asString()
|
||||||
|
.contains("Invalid ZoneId [notAZone] for [fallbackZoneId]");
|
||||||
|
|
||||||
|
assertThat(new DateTimeDisplayValueBehavior().validateBehaviorConfiguration(table, table.getField("firstName")))
|
||||||
|
.hasSize(1).first().asString()
|
||||||
|
.contains("non-DATE_TIME field [firstName]");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user