FINERACT-2238: New command processing - Business Date Management (org.apache.fineract.infrastructure.businessdate)

This commit is contained in:
viktorpavlenko
2025-06-26 00:49:05 +02:00
committed by Aleksandar Vidakovic
parent d5958f36c7
commit bb46397daa
81 changed files with 1385 additions and 1099 deletions
@@ -0,0 +1,91 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.validation.config;
import jakarta.validation.MessageInterpolator;
import java.util.Locale;
import java.util.regex.Pattern;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.messageinterpolation.AbstractMessageInterpolator;
import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.validation.beanvalidation.MessageSourceResourceBundleLocator;
import yakworks.i18n.icu.ICUBundleMessageSource;
@Slf4j
@RequiredArgsConstructor
@Configuration
public class ValidationConfig {
@Bean
public MessageSource messageSource() {
var messageSource = new ICUBundleMessageSource();
messageSource.setBasenames("classpath:fineract/validation/messages");
messageSource.setCacheSeconds(3600);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
@Bean
public MessageInterpolator messageInterpolator() {
var resourceBundleLocator = new MessageSourceResourceBundleLocator(messageSource());
var messageInterpolator = new ResourceBundleMessageInterpolator(resourceBundleLocator);
return new RecursiveLocaleContextMessageInterpolator(messageInterpolator);
}
@Bean
@Primary
public Validator validator() {
var localValidatorFactoryBean = new LocalValidatorFactoryBean();
localValidatorFactoryBean.setMessageInterpolator(messageInterpolator());
return localValidatorFactoryBean;
}
private static final class RecursiveLocaleContextMessageInterpolator extends AbstractMessageInterpolator {
private static final Pattern PATTERN_PLACEHOLDER = Pattern.compile("\\{([^}]+)\\}");
private final MessageInterpolator interpolator;
private RecursiveLocaleContextMessageInterpolator(MessageInterpolator interpolator) {
this.interpolator = interpolator;
}
@Override
public String interpolate(MessageInterpolator.Context context, Locale locale, String message) {
int level = 0;
while (containsPlaceholder(message) && (level++ < 2)) {
message = this.interpolator.interpolate(message, context, locale);
}
return message;
}
private boolean containsPlaceholder(String code) {
return PATTERN_PLACEHOLDER.matcher(code).find();
}
}
}
@@ -0,0 +1,46 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.validation.constraints;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Constraint(validatedBy = LocalDateValidator.class)
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface LocalDate {
String message() default "{org.apache.fineract.validation.local-date}";
String dateField();
String formatField();
String localeField();
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
@@ -0,0 +1,83 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.validation.constraints;
import static java.time.LocalDateTime.parse;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.ResolverStyle;
import java.time.temporal.ChronoField;
import java.util.Locale;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@Slf4j
class LocalDateValidator implements ConstraintValidator<LocalDate, Object> {
private String dateField;
private String formatField;
private String localeField;
@Override
public void initialize(LocalDate annotation) {
this.dateField = annotation.dateField();
this.formatField = annotation.formatField();
this.localeField = annotation.localeField();
}
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
try {
var dateAttr = value.getClass().getDeclaredField(dateField);
var formatAttr = value.getClass().getDeclaredField(formatField);
var localeAttr = value.getClass().getDeclaredField(localeField);
dateAttr.setAccessible(true);
formatAttr.setAccessible(true);
localeAttr.setAccessible(true);
var date = (String) dateAttr.get(value);
var format = (String) formatAttr.get(value);
var locale = (String) localeAttr.get(value);
if (StringUtils.isBlank(date) || StringUtils.isBlank(format) || StringUtils.isBlank(locale)) {
return false;
}
toLocalDate(date, format, locale);
return true;
} catch (IllegalAccessException | NoSuchFieldException e) {
throw new RuntimeException("Invalid configuration for @LocalDate", e);
} catch (Exception e) {
return false;
}
}
private void toLocalDate(String date, String format, String locale) {
var formatter = new DateTimeFormatterBuilder().parseCaseInsensitive().parseLenient().appendPattern(format.replace("y", "u"))
.optionalStart().appendPattern(" HH:mm:ss").optionalEnd().parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0).parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.toFormatter(Locale.forLanguageTag(locale)).withResolverStyle(ResolverStyle.STRICT);
parse(date, formatter);
}
}
@@ -0,0 +1,40 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.validation.constraints;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Constraint(validatedBy = LocaleValidator.class)
@Target({ ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
public @interface Locale {
String message() default "{org.apache.fineract.validation.locale}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
@@ -0,0 +1,32 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.validation.constraints;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import java.util.Arrays;
import org.apache.commons.lang3.StringUtils;
class LocaleValidator implements ConstraintValidator<Locale, String> {
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
return StringUtils.isNotBlank(value) && Arrays.asList(java.util.Locale.getISOLanguages()).contains(value);
}
}
@@ -0,0 +1,30 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
# Validation
org.apache.fineract.validation.local-date=Wrong local date fields.
org.apache.fineract.validation.locale=The parameter `locale` has an invalid language value: `${validatedValue}`.
## Business Date
org.apache.fineract.businessdate.date-format.not-blank=The parameter 'dateFormat' is mandatory: '${validatedValue}'.
org.apache.fineract.businessdate.type.not-null=The parameter 'type' is mandatory: '${validatedValue}'.
org.apache.fineract.businessdate.date.not-blank=The parameter 'date' is mandatory: '${validatedValue}'.
org.apache.fineract.businessdate.locale.not-blank=The parameter 'locale' is mandatory: '${validatedValue}'.
@@ -0,0 +1,30 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
# Validation
org.apache.fineract.validation.local-date=Wrong local date fields.
org.apache.fineract.validation.locale=The parameter `locale` has an invalid language value: `${validatedValue}`.
## Business Date
org.apache.fineract.businessdate.date-format.not-blank=The parameter 'dateFormat' is mandatory: '${validatedValue}'.
org.apache.fineract.businessdate.type.not-null=The parameter 'type' is mandatory: '${validatedValue}'.
org.apache.fineract.businessdate.date.not-blank=The parameter 'date' is mandatory: '${validatedValue}'.
org.apache.fineract.businessdate.locale.not-blank=The parameter 'locale' is mandatory: '${validatedValue}'.
@@ -0,0 +1,96 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.validation.constraints;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.validation.config.ValidationConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.validation.Validator;
@Slf4j
@SpringBootTest
@ContextConfiguration(classes = { ValidationConfig.class })
class LocalDateValidationTest {
@Autowired
private Validator validator;
@Test
void invalidAllBlank() {
var request = LocalDateModel.builder().format("").date(" ").locale(null).build();
var errors = validator.validateObject(request);
assertThat(errors.getAllErrors()).hasSize(1);
assertThat(errors.getAllErrors()).allMatch(e -> "Wrong local date fields.".equals(e.getDefaultMessage()));
}
@Test
void invalidLocaleFormat() {
var request = LocalDateModel.builder().format("dd-MM-yyyy").date("12-05-2025").locale("").build();
var errors = validator.validateObject(request);
assertThat(errors.getAllErrors()).hasSize(1);
assertThat(errors.getAllErrors()).allMatch(e -> "Wrong local date fields.".equals(e.getDefaultMessage()));
}
@Test
void invalidDateFormat() {
var request = LocalDateModel.builder().format("dd/MM/yyyy").date("12-05-2025").locale("en").build();
var errors = validator.validateObject(request);
assertThat(errors.getAllErrors()).hasSize(1);
assertThat(errors.getAllErrors()).allMatch(e -> "Wrong local date fields.".equals(e.getDefaultMessage()));
}
@Test
void valid() {
var request = LocalDateModel.builder().format("dd-MM-yyyy").date("12-05-2025").locale("en").build();
var errors = validator.validateObject(request);
assertThat(errors.getAllErrors()).isEmpty();
}
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@LocalDate(dateField = "date", formatField = "format", localeField = "locale")
static class LocalDateModel {
private String date;
private String format;
private String locale;
}
}
@@ -0,0 +1,83 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.validation.constraints;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.validation.config.ValidationConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.validation.Validator;
@Slf4j
@SpringBootTest
@ContextConfiguration(classes = { ValidationConfig.class })
class LocaleValidationTest {
@Autowired
private Validator validator;
@Test
void invalidBlank() {
var request = LocaleModel.builder().locale(null).build();
var errors = validator.validateObject(request);
assertThat(errors.getFieldErrorCount()).isEqualTo(1);
assertThat(errors.getFieldErrors()).anyMatch(e -> e.getField().equals("locale"));
}
@Test
void invalidFormat() {
var request = LocaleModel.builder().locale("EN").build();
var errors = validator.validateObject(request);
assertThat(errors.getFieldErrorCount()).isEqualTo(1);
assertThat(errors.getFieldErrors()).anyMatch(e -> e.getField().equals("locale"));
}
@Test
void valid() {
var request = LocaleModel.builder().locale("en").build();
var errors = validator.validateObject(request);
assertThat(errors.getFieldErrorCount()).isEqualTo(0);
}
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
static class LocaleModel {
@Locale
private String locale;
}
}