Skip to content

Commit

Permalink
Merge branch 'zonedatetime_support' into graalvm21_java20
Browse files Browse the repository at this point in the history
  • Loading branch information
ckramp committed Apr 12, 2024
2 parents fda2aa2 + 72ecf78 commit b376f65
Show file tree
Hide file tree
Showing 18 changed files with 790 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2010-2024 Structr GmbH
*
* This file is part of Structr <http://structr.org>.
*
* Structr is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Structr is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Structr. If not, see <http://www.gnu.org/licenses/>.
*/
package org.structr.common.error;

import org.structr.core.property.PropertyKey;

import java.time.ZonedDateTime;
import java.util.Date;

/**
* Indicates that a given date property has the wrong format.
*
*
*/
public class ZonedDateTimeFormatToken extends SemanticErrorToken {

public ZonedDateTimeFormatToken(final String type, PropertyKey<ZonedDateTime> propertyKey) {
super(type, propertyKey, "invalid_date_format");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2010-2024 Structr GmbH
*
* This file is part of Structr <http://structr.org>.
*
* Structr is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Structr is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Structr. If not, see <http://www.gnu.org/licenses/>.
*/
package org.structr.core.converter;

import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Date;

/**
* Converts java temporal to a regular date object
*/
public abstract class TemporalDateConverter {

public static Date convert(final Object inst) {

if (inst == null) {
return null;
}

if (inst instanceof Date date) {

return date;
} else if (inst instanceof ZonedDateTime zdt) {

return Date.from(zdt.toInstant());
} else if (inst instanceof Instant i) {

return Date.from(i);
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.structr.common.error.ArgumentCountException;
import org.structr.common.error.ArgumentNullException;
import org.structr.common.error.FrameworkException;
import org.structr.core.converter.TemporalDateConverter;
import org.structr.schema.action.ActionContext;

import java.text.ParseException;
Expand Down Expand Up @@ -62,14 +63,19 @@ public Object apply(final ActionContext ctx, final Object caller, final Object[]

} else {

try {
// parse with format from IS
date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(sources[0].toString());
date = TemporalDateConverter.convert(sources[0]);

} catch (ParseException ex) {
if (date == null) {

logger.warn("{}: Could not parse string \"{}\" with pattern {} in element \"{}\". Parameters: {}", new Object[] { getReplacement(), sources[0].toString(), "yyyy-MM-dd'T'HH:mm:ssZ", caller, getParametersAsString(sources) });
return "";
try {
// parse with format from IS
date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(sources[0].toString());

} catch (ParseException ex) {

logger.warn("{}: Could not parse string \"{}\" with pattern {} in element \"{}\". Parameters: {}", new Object[]{getReplacement(), sources[0].toString(), "yyyy-MM-dd'T'HH:mm:ssZ", caller, getParametersAsString(sources)});
return "";
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.structr.common.error.ArgumentCountException;
import org.structr.common.error.ArgumentNullException;
import org.structr.common.error.FrameworkException;
import org.structr.core.converter.TemporalDateConverter;
import org.structr.schema.action.ActionContext;

import java.text.ParseException;
Expand Down Expand Up @@ -66,15 +67,19 @@ public Object apply(final ActionContext ctx, final Object caller, final Object[]

} else {

try {
date = TemporalDateConverter.convert(sources[0]);

// parse with format from IS
date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(sources[0].toString());
if (date == null) {
try {

} catch (ParseException ex) {
// parse with format from IS
date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(sources[0].toString());

logger.warn("{}: Could not parse string \"{}\" with pattern {} in element \"{}\". Parameters: {}", new Object[] { getReplacement(), sources[0].toString(), "yyyy-MM-dd'T'HH:mm:ssZ", caller, getParametersAsString(sources) });
return sources[0];
} catch (ParseException ex) {

logger.warn("{}: Could not parse string \"{}\" with pattern {} in element \"{}\". Parameters: {}", new Object[]{getReplacement(), sources[0].toString(), "yyyy-MM-dd'T'HH:mm:ssZ", caller, getParametersAsString(sources)});
return sources[0];
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.structr.common.SecurityContext;
import org.structr.common.error.FrameworkException;
import org.structr.core.app.Query;
import org.structr.core.converter.PropertyConverter;
import org.structr.core.property.PropertyKey;

/**
Expand All @@ -44,13 +45,29 @@ public RangePredicate(final Object rangeStart, final Object rangeEnd, final bool
@Override
public void configureQuery(final SecurityContext securityContext, final Class type, final PropertyKey key, final Query query, final boolean exact) throws FrameworkException {

Object effectiveRangeStart = rangeStart;
if (key != null && rangeStart != null && !key.valueType().isAssignableFrom(rangeStart.getClass())) {
Object converted = key.inputConverter(securityContext).convert(rangeStart);
if (converted != null) {
effectiveRangeStart = converted;
};
}

Object effectiveRangeEnd = rangeEnd;
if (key != null && rangeEnd != null && !key.valueType().isAssignableFrom(rangeEnd.getClass())) {
Object converted = key.inputConverter(securityContext).convert(rangeEnd);
if (converted != null) {
effectiveRangeEnd = converted;
}
}

if (Occurrence.OPTIONAL.equals(query.getCurrentOccurrence())) {

query.orRange(key, rangeStart, rangeEnd, includeStart, includeEnd);
query.orRange(key, effectiveRangeStart, effectiveRangeEnd, includeStart, includeEnd);

} else {

query.andRange(key, rangeStart, rangeEnd, includeStart, includeEnd);
query.andRange(key, effectiveRangeStart, effectiveRangeEnd, includeStart, includeEnd);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import org.structr.common.error.FrameworkException;
import org.structr.core.GraphObject;
import org.structr.core.converter.PropertyConverter;
import org.structr.core.converter.TemporalDateConverter;
import org.structr.schema.parser.DatePropertyParser;

import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
Expand Down Expand Up @@ -165,10 +167,14 @@ public Date convert(Object source) throws FrameworkException {

if (source != null) {

if (source instanceof Date) {
final Date convertedDate = TemporalDateConverter.convert(source);

return (Date)source;
if (convertedDate != null) {

return convertedDate;
} else if (source instanceof Long l) {

return Date.from(Instant.ofEpochMilli(l));
} else if (source instanceof String) {

if (StringUtils.isNotBlank((String)source)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import org.structr.common.error.FrameworkException;
import org.structr.core.GraphObject;
import org.structr.core.converter.PropertyConverter;
import org.structr.core.converter.TemporalDateConverter;
import org.structr.schema.parser.DatePropertyParser;

import java.time.Instant;
import java.util.Date;
import org.structr.common.error.PropertyInputParsingException;

Expand All @@ -45,7 +47,7 @@ public PropertyConverter<Date, Long> databaseConverter(SecurityContext securityC
}

@Override
public PropertyConverter<String, Date> inputConverter(SecurityContext securityContext) {
public PropertyConverter<Object, Date> inputConverter(SecurityContext securityContext) {
return new InputConverter(securityContext);
}

Expand Down Expand Up @@ -79,29 +81,41 @@ public Date revert(Long source) throws FrameworkException {
}
}

private class InputConverter extends PropertyConverter<String, Date> {
private class InputConverter extends PropertyConverter<Object, Date> {

public InputConverter(SecurityContext securityContext) {
super(securityContext, null);
}

@Override
public Date convert(String source) throws FrameworkException {
public Date convert(Object source) throws FrameworkException {

if (StringUtils.isNotBlank(source)) {
if (source != null) {

final Date convertedDate = TemporalDateConverter.convert(source);

if (convertedDate != null) {

return convertedDate;
} else if (source instanceof Long l) {

Date result = DatePropertyParser.parseISO8601DateString(source);
if (result != null) {
return DatePropertyParser.parseISO8601DateString(Date.from(Instant.ofEpochMilli(l)).toString());
} else if (source instanceof String sourceString) {
if (StringUtils.isNotBlank(sourceString)) {

return result;
Date result = DatePropertyParser.parseISO8601DateString(sourceString);
if (result != null) {

} else {
return result;

throw new PropertyInputParsingException(
jsonName(),
new DateFormatToken(declaringClass.getSimpleName(), jsonName())
);
} else {

throw new PropertyInputParsingException(
jsonName(),
new DateFormatToken(declaringClass.getSimpleName(), jsonName())
);
}
}
}
}

Expand Down
Loading

0 comments on commit b376f65

Please sign in to comment.