Skip to content

Commit

Permalink
[MODORDSTOR-381] Merge with master and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
Saba-Zedginidze-EPAM committed Apr 5, 2024
1 parent db9269b commit a04380c
Show file tree
Hide file tree
Showing 14 changed files with 200 additions and 259 deletions.
14 changes: 7 additions & 7 deletions ramls/routing-lists.raml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ resourceTypes:
body:
application/json:
type: error
/{id}/template:
uriParameters:
id:
description: The UUID of a Title
type: UUID
get:
description: Execute mod-template-engine to process templates with replaced token placeholders [update]
/{id}:
uriParameters:
id:
Expand All @@ -79,10 +86,3 @@ resourceTypes:
collection-item:
exampleItem: !include acq-models/mod-orders-storage/examples/routing_list_get.sample
schema: routing-list
/{id}/template:
uriParameters:
id:
description: The UUID of a Title
type: UUID
get:
description: Execute mod-template-engine to process templates with replaced token placeholders [update]
6 changes: 3 additions & 3 deletions src/main/java/org/folio/config/ApplicationConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
import org.folio.service.pieces.flows.update.PieceUpdateFlowInventoryManager;
import org.folio.service.pieces.flows.update.PieceUpdateFlowManager;
import org.folio.service.pieces.flows.update.PieceUpdateFlowPoLineService;
import org.folio.service.RoutingListService;
import org.folio.service.routinglists.RoutingListService;
import org.folio.service.titles.TitleValidationService;
import org.folio.service.titles.TitlesService;
import org.springframework.beans.factory.annotation.Qualifier;
Expand Down Expand Up @@ -437,8 +437,8 @@ CompositeOrderDynamicDataPopulateService combinedPopulateService(CompositeOrderR
}

@Bean
RoutingListService routingListService(RestClient restClient, UserService userService) {
return new RoutingListService(restClient, userService);
RoutingListService routingListService(RestClient restClient, PurchaseOrderLineService purchaseOrderLineService, UserService userService) {
return new RoutingListService(restClient, purchaseOrderLineService, userService);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ private ResourcePathResolver() {
public static final String ACQUISITIONS_MEMBERSHIPS = "acquisitionsMemberships";
public static final String REPORTING_CODES = "reportingCodes";
public static final String PURCHASE_ORDER_STORAGE = "purchaseOrder";
public static final String ROUTING_LIST_STORAGE = "routingLists";
public static final String PIECES_STORAGE = "pieces";
public static final String RECEIVING_HISTORY = "receiving-history";
public static final String RECEIPT_STATUS = "receiptStatus";
Expand Down Expand Up @@ -67,7 +66,6 @@ private ResourcePathResolver() {
apis.put(PO_LINES_BUSINESS, "/orders/order-lines");
apis.put(PO_NUMBER, "/orders-storage/po-number");
apis.put(PURCHASE_ORDER_STORAGE, "/orders-storage/purchase-orders");
apis.put(ROUTING_LIST_STORAGE, "/orders-storage/routing-lists");
apis.put(PIECES_STORAGE, "/orders-storage/pieces");
apis.put(RECEIVING_HISTORY, "/orders-storage/receiving-history");
apis.put(PO_LINE_NUMBER, "/orders-storage/po-line-number");
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/folio/rest/impl/RoutingListsAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.folio.rest.core.models.RequestContext;
import org.folio.rest.jaxrs.model.RoutingList;
import org.folio.rest.jaxrs.resource.OrdersRoutingLists;
import org.folio.service.routinglists.RoutingListsService;
import org.folio.service.routinglists.RoutingListService;
import org.folio.spring.SpringContextUtil;
import org.springframework.beans.factory.annotation.Autowired;

Expand All @@ -20,7 +20,7 @@
public class RoutingListsAPI extends BaseApi implements OrdersRoutingLists {

@Autowired
private RoutingListService routingListsService;
private RoutingListService routingListService;

public RoutingListsAPI() {
SpringContextUtil.autowireDependencies(this, Vertx.currentContext());
Expand All @@ -29,31 +29,31 @@ public RoutingListsAPI() {
@Override
@Validate
public void getOrdersRoutingLists(String query, String totalRecords, int offset, int limit, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
routingListsStorageService.getRoutingLists(limit, offset, query, new RequestContext(vertxContext, okapiHeaders))
routingListService.getRoutingLists(limit, offset, query, new RequestContext(vertxContext, okapiHeaders))
.onSuccess(lists -> asyncResultHandler.handle(succeededFuture(buildOkResponse(lists))))
.onFailure(fail -> handleErrorResponse(asyncResultHandler, fail));
}

@Override
@Validate
public void postOrdersRoutingLists(RoutingList entity, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
routingListsStorageService.createRoutingList(entity, new RequestContext(vertxContext, okapiHeaders))
routingListService.createRoutingList(entity, new RequestContext(vertxContext, okapiHeaders))
.onSuccess(list -> asyncResultHandler.handle(succeededFuture(buildOkResponse(list))))
.onFailure(fail -> handleErrorResponse(asyncResultHandler, fail));
}

@Override
@Validate
public void getOrdersRoutingListsById(String id, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
routingListsStorageService.getRoutingList(id, new RequestContext(vertxContext, okapiHeaders))
routingListService.getRoutingList(id, new RequestContext(vertxContext, okapiHeaders))
.onSuccess(list -> asyncResultHandler.handle(succeededFuture(buildOkResponse(list))))
.onFailure(fail -> handleErrorResponse(asyncResultHandler, fail));
}

@Override
@Validate
public void deleteOrdersRoutingListsById(String id, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
routingListsStorageService.deleteRoutingList(id, new RequestContext(vertxContext, okapiHeaders))
routingListService.deleteRoutingList(id, new RequestContext(vertxContext, okapiHeaders))
.onSuccess(list -> asyncResultHandler.handle(succeededFuture(buildNoContentResponse())))
.onFailure(fail -> handleErrorResponse(asyncResultHandler, fail));

Expand All @@ -62,7 +62,7 @@ public void deleteOrdersRoutingListsById(String id, Map<String, String> okapiHea
@Override
@Validate
public void putOrdersRoutingListsById(String id, RoutingList entity, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
routingListsStorageService.updateRoutingList(entity, new RequestContext(vertxContext, okapiHeaders))
routingListService.updateRoutingList(entity, new RequestContext(vertxContext, okapiHeaders))
.onSuccess(list -> asyncResultHandler.handle(succeededFuture(buildOkResponse(list))))
.onFailure(fail -> handleErrorResponse(asyncResultHandler, fail));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,111 @@
package org.folio.service;

import static org.folio.orders.utils.ResourcePathResolver.ORDER_SETTINGS;
import static org.folio.orders.utils.ResourcePathResolver.ROUTING_LISTS;
import static org.folio.orders.utils.ResourcePathResolver.TEMPLATE_REQUEST;
import static org.folio.orders.utils.ResourcePathResolver.resourcesPath;

import java.util.Collections;
import java.util.List;
import java.util.UUID;
package org.folio.service.routinglists;

import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.common.errors.ResourceNotFoundException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.models.TemplateProcessingRequest;
import org.folio.models.UserCollection;
import org.folio.rest.RestConstants;
import org.folio.rest.acq.model.SettingCollection;
import org.folio.rest.core.RestClient;
import org.folio.rest.core.exceptions.HttpException;
import org.folio.rest.core.models.RequestContext;
import org.folio.rest.core.models.RequestEntry;
import org.folio.rest.jaxrs.model.Error;
import org.folio.rest.jaxrs.model.Errors;
import org.folio.rest.jaxrs.model.PoLine;
import org.folio.rest.jaxrs.model.RoutingList;
import org.folio.rest.jaxrs.model.RoutingListCollection;
import org.folio.service.UserService;
import org.folio.service.orders.PurchaseOrderLineService;
import org.folio.service.routinglists.validators.RoutingListValidatorUtil;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

import static org.folio.orders.utils.ResourcePathResolver.ORDER_SETTINGS;
import static org.folio.orders.utils.ResourcePathResolver.ROUTING_LISTS;
import static org.folio.orders.utils.ResourcePathResolver.TEMPLATE_REQUEST;
import static org.folio.orders.utils.ResourcePathResolver.resourcesPath;

public class RoutingListService {
private static final Logger logger = LogManager.getLogger(RoutingListService.class);

private static final Logger log = LogManager.getLogger();
private static final UUID TEMPLATE_REQUEST_ID = UUID.fromString("9465105a-e8a1-470c-9817-142d33bc4fcd");
private static final String TEMPLATE_REQUEST_LANG = "en";
private static final String TEMPLATE_REQUEST_OUTPUT = "text/html";
private static final String ROUTING_LIST_ENDPOINT = resourcesPath(ROUTING_LISTS);
private static final String ORDER_SETTINGS_ENDPOINT = resourcesPath(ORDER_SETTINGS);
private static final String ROUTING_USER_ADDRESS_TYPE_ID = "ROUTING_USER_ADDRESS_TYPE_ID";
private static final String ROUTING_LIST_BY_ID_ENDPOINT = ROUTING_LIST_ENDPOINT + "/{id}";
private static final String ROUTING_LIST_BY_POL_ID = "poLineId==%s";

private static final String TEMPLATE_REQUEST_ENDPOINT = resourcesPath(TEMPLATE_REQUEST);
private static final UUID TEMPLATE_REQUEST_ID = UUID.fromString("9465105a-e8a1-470c-9817-142d33bc4fcd");
private static final String TEMPLATE_REQUEST_LANG = "en";
private static final String TEMPLATE_REQUEST_OUTPUT = "text/html";

private final RestClient restClient;
private final PurchaseOrderLineService poLineService;
private final UserService userService;
private final RestClient restClient;

public RoutingListService(RestClient restClient, UserService userService) {
public RoutingListService(RestClient restClient, PurchaseOrderLineService poLineService, UserService userService) {
this.restClient = restClient;
this.poLineService = poLineService;
this.userService = userService;
}

public Future<RoutingList> getRoutingList(String rListId, RequestContext requestContext) {
RequestEntry requestEntry = new RequestEntry(ROUTING_LIST_BY_ID_ENDPOINT).withId(rListId);
return restClient.get(requestEntry, RoutingList.class, requestContext);
}

public Future<Void> updateRoutingList(RoutingList routingList, RequestContext requestContext) {
validateRoutingList(routingList, requestContext);
RequestEntry requestEntry = new RequestEntry(ROUTING_LIST_BY_ID_ENDPOINT).withId(routingList.getId());
return restClient.put(requestEntry, requestContext, requestContext);
}

public Future<Void> deleteRoutingList(String rListId, RequestContext requestContext) {
RequestEntry requestEntry = new RequestEntry(ROUTING_LIST_BY_ID_ENDPOINT).withId(rListId);
return restClient.delete(requestEntry, requestContext);
}

public Future<RoutingList> createRoutingList(RoutingList routingList, RequestContext requestContext) {
validateRoutingList(routingList, requestContext);
RequestEntry requestEntry = new RequestEntry(ROUTING_LIST_ENDPOINT);
return restClient.post(requestEntry, routingList, RoutingList.class, requestContext);
}

public Future<RoutingListCollection> getRoutingLists(int limit, int offset, String query, RequestContext requestContext) {
RequestEntry requestEntry = new RequestEntry(ROUTING_LIST_ENDPOINT)
.withQuery(query)
.withOffset(offset)
.withLimit(limit);
return restClient.get(requestEntry, RoutingListCollection.class, requestContext);
}

private Future<RoutingListCollection> getRoutingListsByPoLineId(String poLineId, RequestContext requestContext) {
String query = String.format(ROUTING_LIST_BY_POL_ID, poLineId);
return getRoutingLists(Integer.MAX_VALUE, 0, query, requestContext);
}

private void validateRoutingList(RoutingList routingList, RequestContext requestContext) {
RoutingListCollection routingLists = getRoutingListsByPoLineId(routingList.getPoLineId(), requestContext).result();
PoLine poLine = poLineService.getOrderLineById(routingList.getPoLineId(), requestContext).result();
List<Error> combinedErrors = RoutingListValidatorUtil.validateRoutingList(routingLists, poLine);
if (CollectionUtils.isNotEmpty(combinedErrors)) {
Errors errors = new Errors().withErrors(combinedErrors).withTotalRecords(combinedErrors.size());
logger.error("Validation error: {}", JsonObject.mapFrom(errors).encodePrettily());
throw new HttpException(RestConstants.VALIDATION_ERROR, errors);
}
}

public Future<JsonObject> processTemplateRequest(String routingListId, RequestContext requestContext) {
log.debug("processTemplateRequest: Tying to process template request for routingListId={}", routingListId);
logger.debug("processTemplateRequest: Tying to process template request for routingListId={}", routingListId);
return getRoutingListById(routingListId, requestContext)
.compose(routingList -> getUsersAndCreateTemplate(routingList, requestContext))
.compose(templateProcessingRequest -> postTemplateRequest(templateProcessingRequest, requestContext));
Expand All @@ -69,7 +129,7 @@ private Future<String> getAddressTypeId(RequestContext requestContext) {
.map(settingCollection -> {
var settings = settingCollection.getSettings();
if (ObjectUtils.isEmpty(settings) || StringUtils.isBlank(settings.get(0).getValue())) {
log.error("getAddressTypeId:: Setting is not found with key={}", ROUTING_USER_ADDRESS_TYPE_ID);
logger.error("getAddressTypeId:: Setting is not found with key={}", ROUTING_USER_ADDRESS_TYPE_ID);
throw new ResourceNotFoundException(String.format("Setting is not found with key=%s", ROUTING_USER_ADDRESS_TYPE_ID));
}
return settings.get(0).getValue();
Expand All @@ -82,7 +142,7 @@ private TemplateProcessingRequest createTemplateRequest(RoutingList routingList,
.withRoutingList(fillRoutingListForContext(routingList))
.withUsers(fillUsersForContext(users, addressTypeId)));

log.info("createTemplateRequest:: TemplateProcessingRequest object created for routing list name: {}",
logger.info("createTemplateRequest:: TemplateProcessingRequest object created for routing list name: {}",
templateRequest.getContext().getRoutingList().getName());
return templateRequest;
}
Expand Down Expand Up @@ -117,11 +177,11 @@ private List<TemplateProcessingRequest.User> fillUsersForContext(UserCollection
private String getUserAddress(List<UserCollection.User.Personal.Address> addressList, String addressTypeId) {
for (UserCollection.User.Personal.Address address : addressList) {
if (address.getAddressTypeId().equals(addressTypeId)) {
log.info("getUserAddress:: Required address with addressTypeId={} is found", addressTypeId);
logger.info("getUserAddress:: Required address with addressTypeId={} is found", addressTypeId);
return address.getAddressLine1();
}
}
log.warn("getUserAddress:: Required address is not found with addressTypId={}", addressTypeId);
logger.warn("getUserAddress:: Required address is not found with addressTypId={}", addressTypeId);
return "";
}

Expand All @@ -133,7 +193,8 @@ private RoutingList fillRoutingListForContext(RoutingList routingList) {

private Future<JsonObject> postTemplateRequest(TemplateProcessingRequest templateRequest, RequestContext requestContext) {
var requestEntry = new RequestEntry(TEMPLATE_REQUEST_ENDPOINT);
log.info("postTemplateRequest:: Sending template request with routing list name={}", templateRequest.getContext().getRoutingList().getName());
logger.info("postTemplateRequest:: Sending template request with routing list name={}", templateRequest.getContext().getRoutingList().getName());
return restClient.postJsonObject(requestEntry, JsonObject.mapFrom(templateRequest), requestContext);
}

}
Loading

0 comments on commit a04380c

Please sign in to comment.