Skip to content

Commit 7c26ba6

Browse files
committed
FINERACT-2326: Reworked Business date handling (Hibernate validator + separation of concerns between API and service)
1 parent d52bc9d commit 7c26ba6

File tree

25 files changed

+301
-173
lines changed

25 files changed

+301
-173
lines changed

fineract-core/src/main/java/org/apache/fineract/infrastructure/businessdate/api/BusinessDateApiResource.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636
import lombok.RequiredArgsConstructor;
3737
import org.apache.fineract.command.core.CommandPipeline;
3838
import org.apache.fineract.infrastructure.businessdate.command.BusinessDateUpdateCommand;
39-
import org.apache.fineract.infrastructure.businessdate.data.BusinessDateResponse;
40-
import org.apache.fineract.infrastructure.businessdate.data.BusinessDateUpdateRequest;
39+
import org.apache.fineract.infrastructure.businessdate.data.api.BusinessDateResponse;
40+
import org.apache.fineract.infrastructure.businessdate.data.api.BusinessDateUpdateRequest;
41+
import org.apache.fineract.infrastructure.businessdate.data.api.BusinessDateUpdateResponse;
42+
import org.apache.fineract.infrastructure.businessdate.mapper.BusinessDateMapper;
4143
import org.apache.fineract.infrastructure.businessdate.service.BusinessDateReadPlatformService;
4244
import org.apache.fineract.infrastructure.core.service.DateUtils;
4345
import org.springframework.stereotype.Component;
@@ -50,13 +52,14 @@ public class BusinessDateApiResource {
5052

5153
private final BusinessDateReadPlatformService readPlatformService;
5254
private final CommandPipeline commandPipeline;
55+
private final BusinessDateMapper businessDateMapper;
5356

5457
@GET
5558
@Consumes({ MediaType.TEXT_HTML, MediaType.APPLICATION_JSON })
5659
@Produces(MediaType.APPLICATION_JSON)
5760
@Operation(summary = "List all business dates", description = "")
5861
public List<BusinessDateResponse> getBusinessDates() {
59-
return this.readPlatformService.findAll();
62+
return businessDateMapper.mapFetchResponse(this.readPlatformService.findAll());
6063
}
6164

6265
@GET
@@ -65,24 +68,24 @@ public List<BusinessDateResponse> getBusinessDates() {
6568
@Produces(MediaType.APPLICATION_JSON)
6669
@Operation(summary = "Retrieve a specific Business date", description = "")
6770
public BusinessDateResponse getBusinessDate(@PathParam("type") @Parameter(description = "type") final String type) {
68-
return this.readPlatformService.findByType(type);
71+
return businessDateMapper.mapFetchResponse(this.readPlatformService.findByType(type));
6972
}
7073

7174
@POST
7275
@Consumes({ MediaType.APPLICATION_JSON })
7376
@Produces({ MediaType.APPLICATION_JSON })
7477
@Operation(summary = "Update Business Date", description = "")
75-
public BusinessDateResponse updateBusinessDate(@HeaderParam("Idempotency-Key") String idempotencyKey,
78+
public BusinessDateUpdateResponse updateBusinessDate(@HeaderParam("Idempotency-Key") String idempotencyKey,
7679
@Valid BusinessDateUpdateRequest request) {
7780

78-
final var command = new BusinessDateUpdateCommand();
81+
final BusinessDateUpdateCommand command = new BusinessDateUpdateCommand();
7982

8083
command.setId(UUID.randomUUID());
8184
command.setIdempotencyKey(idempotencyKey);
8285
command.setCreatedAt(DateUtils.getAuditOffsetDateTime());
8386
command.setPayload(request);
8487

85-
final Supplier<BusinessDateResponse> response = commandPipeline.send(command);
88+
final Supplier<BusinessDateUpdateResponse> response = commandPipeline.send(command);
8689

8790
return response.get();
8891
}

fineract-core/src/main/java/org/apache/fineract/infrastructure/businessdate/command/BusinessDateUpdateCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import lombok.Data;
2222
import lombok.EqualsAndHashCode;
2323
import org.apache.fineract.command.core.Command;
24-
import org.apache.fineract.infrastructure.businessdate.data.BusinessDateUpdateRequest;
24+
import org.apache.fineract.infrastructure.businessdate.data.api.BusinessDateUpdateRequest;
2525

2626
@Data
2727
@EqualsAndHashCode(callSuper = true)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.fineract.infrastructure.businessdate.data.api;
20+
21+
import java.io.Serial;
22+
import java.io.Serializable;
23+
import java.time.LocalDate;
24+
import lombok.AllArgsConstructor;
25+
import lombok.Builder;
26+
import lombok.Data;
27+
import lombok.NoArgsConstructor;
28+
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType;
29+
import org.apache.fineract.infrastructure.core.jersey.serializer.legacy.JsonLocalDateArrayFormat;
30+
31+
@Builder
32+
@Data
33+
@NoArgsConstructor
34+
@AllArgsConstructor
35+
@JsonLocalDateArrayFormat
36+
public class BusinessDateResponse implements Serializable {
37+
38+
@Serial
39+
private static final long serialVersionUID = 1L;
40+
41+
private String description;
42+
private BusinessDateType type;
43+
private LocalDate date;
44+
}

fineract-core/src/main/java/org/apache/fineract/infrastructure/businessdate/data/BusinessDateUpdateRequest.java renamed to fineract-core/src/main/java/org/apache/fineract/infrastructure/businessdate/data/api/BusinessDateUpdateRequest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.apache.fineract.infrastructure.businessdate.data;
19+
package org.apache.fineract.infrastructure.businessdate.data.api;
2020

21+
import io.swagger.v3.oas.annotations.media.Schema;
2122
import jakarta.validation.constraints.NotBlank;
2223
import jakarta.validation.constraints.NotNull;
2324
import java.io.Serial;
@@ -27,6 +28,7 @@
2728
import lombok.Data;
2829
import lombok.NoArgsConstructor;
2930
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType;
31+
import org.apache.fineract.validation.constraints.EnumValue;
3032
import org.apache.fineract.validation.constraints.LocalDate;
3133
import org.apache.fineract.validation.constraints.Locale;
3234

@@ -42,8 +44,10 @@ public class BusinessDateUpdateRequest implements Serializable {
4244

4345
@NotBlank(message = "{org.apache.fineract.businessdate.date-format.not-blank}")
4446
private String dateFormat;
45-
@NotNull(message = "{org.apache.fineract.businessdate.type.not-null}")
46-
private BusinessDateType type;
47+
@Schema(description = "Type of business date", example = "BUSINESS_DATE", allowableValues = { "BUSINESS_DATE", "COB_DATE" })
48+
@EnumValue(enumClass = BusinessDateType.class, message = "{org.apache.fineract.businessdate.type.invalid}")
49+
@NotNull(message = "{org.apache.fineract.businessdate.type.not-blank}")
50+
private String type;
4751
@NotBlank(message = "{org.apache.fineract.businessdate.date.not-blank}")
4852
private String date;
4953
@NotBlank(message = "{org.apache.fineract.businessdate.locale.not-blank}")
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.fineract.infrastructure.businessdate.data.api;
20+
21+
import java.io.Serial;
22+
import java.io.Serializable;
23+
import java.time.LocalDate;
24+
import java.util.Map;
25+
import lombok.AllArgsConstructor;
26+
import lombok.Builder;
27+
import lombok.Data;
28+
import lombok.NoArgsConstructor;
29+
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType;
30+
import org.apache.fineract.infrastructure.core.jersey.serializer.legacy.JsonLocalDateArrayFormat;
31+
32+
@Builder
33+
@Data
34+
@NoArgsConstructor
35+
@AllArgsConstructor
36+
@JsonLocalDateArrayFormat
37+
public class BusinessDateUpdateResponse implements Serializable {
38+
39+
@Serial
40+
private static final long serialVersionUID = 1L;
41+
42+
private String description;
43+
private BusinessDateType type;
44+
private LocalDate date;
45+
private Map<BusinessDateType, LocalDate> changes;
46+
}

fineract-core/src/main/java/org/apache/fineract/infrastructure/businessdate/data/BusinessDateResponse.java renamed to fineract-core/src/main/java/org/apache/fineract/infrastructure/businessdate/data/service/BusinessDateDTO.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.apache.fineract.infrastructure.businessdate.data;
19+
package org.apache.fineract.infrastructure.businessdate.data.service;
2020

2121
import java.io.Serial;
2222
import java.io.Serializable;
@@ -33,7 +33,7 @@
3333
@Data
3434
@NoArgsConstructor
3535
@AllArgsConstructor
36-
public class BusinessDateResponse implements Serializable {
36+
public class BusinessDateDTO implements Serializable {
3737

3838
@Serial
3939
private static final long serialVersionUID = 1L;

fineract-core/src/main/java/org/apache/fineract/infrastructure/businessdate/handler/BusinessDateUpdateHandler.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,27 @@
2222
import lombok.extern.slf4j.Slf4j;
2323
import org.apache.fineract.command.core.Command;
2424
import org.apache.fineract.command.core.CommandHandler;
25-
import org.apache.fineract.infrastructure.businessdate.data.BusinessDateResponse;
26-
import org.apache.fineract.infrastructure.businessdate.data.BusinessDateUpdateRequest;
25+
import org.apache.fineract.infrastructure.businessdate.data.api.BusinessDateUpdateRequest;
26+
import org.apache.fineract.infrastructure.businessdate.data.api.BusinessDateUpdateResponse;
27+
import org.apache.fineract.infrastructure.businessdate.data.service.BusinessDateDTO;
28+
import org.apache.fineract.infrastructure.businessdate.mapper.BusinessDateMapper;
2729
import org.apache.fineract.infrastructure.businessdate.service.BusinessDateWritePlatformService;
2830
import org.springframework.stereotype.Component;
2931
import org.springframework.transaction.annotation.Transactional;
3032

3133
@Slf4j
3234
@Component
3335
@RequiredArgsConstructor
34-
public class BusinessDateUpdateHandler implements CommandHandler<BusinessDateUpdateRequest, BusinessDateResponse> {
36+
public class BusinessDateUpdateHandler implements CommandHandler<BusinessDateUpdateRequest, BusinessDateUpdateResponse> {
3537

3638
private final BusinessDateWritePlatformService businessDateWritePlatformService;
39+
private final BusinessDateMapper businessDateMapper;
3740

3841
@Transactional
3942
@Override
40-
public BusinessDateResponse handle(Command<BusinessDateUpdateRequest> command) {
41-
return businessDateWritePlatformService.updateBusinessDate(command.getPayload());
43+
public BusinessDateUpdateResponse handle(Command<BusinessDateUpdateRequest> command) {
44+
BusinessDateDTO businessDateDto = businessDateMapper.mapUpdateRequest(command.getPayload());
45+
businessDateDto = businessDateWritePlatformService.updateBusinessDate(businessDateDto);
46+
return businessDateMapper.mapUpdateResponse(businessDateDto);
4247
}
4348
}

fineract-core/src/main/java/org/apache/fineract/infrastructure/businessdate/mapper/BusinessDateMapper.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
package org.apache.fineract.infrastructure.businessdate.mapper;
2020

2121
import java.util.List;
22-
import org.apache.fineract.infrastructure.businessdate.data.BusinessDateResponse;
22+
import org.apache.fineract.infrastructure.businessdate.data.api.BusinessDateResponse;
23+
import org.apache.fineract.infrastructure.businessdate.data.api.BusinessDateUpdateRequest;
24+
import org.apache.fineract.infrastructure.businessdate.data.api.BusinessDateUpdateResponse;
25+
import org.apache.fineract.infrastructure.businessdate.data.service.BusinessDateDTO;
2326
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDate;
2427
import org.apache.fineract.infrastructure.core.config.MapstructMapperConfig;
2528
import org.mapstruct.Mapper;
@@ -29,8 +32,20 @@
2932
@Mapper(config = MapstructMapperConfig.class)
3033
public interface BusinessDateMapper {
3134

32-
@Mappings({ @Mapping(target = "description", source = "source.type.description"), @Mapping(target = "changes", ignore = true) })
33-
BusinessDateResponse map(BusinessDate source);
35+
@Mappings({ @Mapping(target = "description", source = "type.description"), @Mapping(target = "changes", ignore = true) })
36+
BusinessDateDTO mapEntity(BusinessDate source);
3437

35-
List<BusinessDateResponse> map(List<BusinessDate> sources);
38+
List<BusinessDateDTO> mapEntity(List<BusinessDate> sources);
39+
40+
@Mapping(target = "description", expression = "java(org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType.valueOf(source.getType()).getDescription())")
41+
@Mapping(target = "type", expression = "java(org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType.valueOf(source.getType()))")
42+
@Mapping(target = "date", expression = "java(org.apache.fineract.infrastructure.core.service.DateUtils.toLocalDate(source.getLocale(), source.getDate(), source.getDateFormat()))")
43+
@Mapping(target = "changes", ignore = true)
44+
BusinessDateDTO mapUpdateRequest(BusinessDateUpdateRequest source);
45+
46+
List<BusinessDateResponse> mapFetchResponse(List<BusinessDateDTO> sources);
47+
48+
BusinessDateResponse mapFetchResponse(BusinessDateDTO source);
49+
50+
BusinessDateUpdateResponse mapUpdateResponse(BusinessDateDTO source);
3651
}

fineract-core/src/main/java/org/apache/fineract/infrastructure/businessdate/mapper/BusinessDateUpdateRequestMapper.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

fineract-core/src/main/java/org/apache/fineract/infrastructure/businessdate/service/BusinessDateReadPlatformService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
import java.time.LocalDate;
2222
import java.util.HashMap;
2323
import java.util.List;
24-
import org.apache.fineract.infrastructure.businessdate.data.BusinessDateResponse;
24+
import org.apache.fineract.infrastructure.businessdate.data.service.BusinessDateDTO;
2525
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType;
2626

2727
public interface BusinessDateReadPlatformService {
2828

29-
List<BusinessDateResponse> findAll();
29+
List<BusinessDateDTO> findAll();
3030

31-
BusinessDateResponse findByType(String type);
31+
BusinessDateDTO findByType(String type);
3232

3333
HashMap<BusinessDateType, LocalDate> getBusinessDates();
3434
}

0 commit comments

Comments
 (0)