-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MODORSERS-1026] - Implement API to execute mod-template-engine request
- Loading branch information
Showing
12 changed files
with
355 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/org/folio/models/template/TemplateProcessingRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.folio.models.template; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class TemplateProcessingRequest { | ||
private UUID templateId; | ||
private String lang; | ||
private String outputFormat; | ||
private Context context; | ||
|
||
public TemplateProcessingRequest withTemplateId(UUID templateId) { | ||
this.templateId = templateId; | ||
return this; | ||
} | ||
|
||
public TemplateProcessingRequest withLang(String lang) { | ||
this.lang = lang; | ||
return this; | ||
} | ||
|
||
public TemplateProcessingRequest withOutputFormat(String outputFormat) { | ||
this.outputFormat = outputFormat; | ||
return this; | ||
} | ||
|
||
public TemplateProcessingRequest withContext(Context context) { | ||
this.context = context; | ||
return this; | ||
} | ||
|
||
@Getter | ||
public static class Context { | ||
private List<User> users; | ||
private List<Item> items; | ||
|
||
public Context withUsers(List<User> users) { | ||
this.users = users; | ||
return this; | ||
} | ||
|
||
} | ||
|
||
@Getter | ||
public static class User { | ||
private String name; | ||
public User withName(String name) { | ||
this.name = name; | ||
return this; | ||
} | ||
} | ||
|
||
@Getter | ||
public static class Item { | ||
private String name; | ||
|
||
public Item setName(String name) { | ||
this.name = name; | ||
return this; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.folio.rest.impl; | ||
|
||
import javax.ws.rs.core.Response; | ||
import java.util.Map; | ||
|
||
import io.vertx.core.AsyncResult; | ||
import io.vertx.core.Context; | ||
import io.vertx.core.Handler; | ||
import org.apache.commons.lang.NotImplementedException; | ||
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.routinglist.RoutingListService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
public class RoutingListAPI extends BaseApi implements OrdersRoutingLists { | ||
|
||
@Autowired | ||
private RoutingListService routingListService; | ||
|
||
@Override | ||
public void getOrdersRoutingLists(String query, String totalRecords, int offset, int limit, Map<String, String> okapiHeaders, | ||
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) { | ||
throw new NotImplementedException(); | ||
} | ||
|
||
@Override | ||
public void postOrdersRoutingLists(RoutingList entity, Map<String, String> okapiHeaders, | ||
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) { | ||
throw new NotImplementedException(); | ||
} | ||
|
||
@Override | ||
public void postOrdersRoutingListsProcessTemplateById(String id, Map<String, String> okapiHeaders, | ||
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) { | ||
routingListService.processTemplateEngine(id, new RequestContext(vertxContext, okapiHeaders)) | ||
.onFailure(t -> handleErrorResponse(asyncResultHandler, t)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,37 @@ | ||
package org.folio.service; | ||
|
||
import static org.folio.orders.utils.HelperUtils.convertIdsToCqlQuery; | ||
import static org.folio.orders.utils.ResourcePathResolver.USERS; | ||
import static org.folio.orders.utils.ResourcePathResolver.resourcesPath; | ||
import static org.folio.rest.RestVerticle.OKAPI_USERID_HEADER; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.core.json.JsonObject; | ||
import org.folio.rest.core.RestClient; | ||
import org.folio.rest.core.models.RequestContext; | ||
import org.folio.rest.core.models.RequestEntry; | ||
|
||
public class UserService { | ||
private static final String ENDPOINT = resourcesPath(USERS); | ||
private final RestClient restClient; | ||
|
||
public UserService(RestClient restClient) { | ||
this.restClient = restClient; | ||
} | ||
|
||
public static String getCurrentUserId(Map<String, String> okapiHeaders) { | ||
return okapiHeaders.get(OKAPI_USERID_HEADER); | ||
} | ||
|
||
public Future<JsonObject> getUsersByIds(List<String> userIds, RequestContext requestContext) { | ||
var requestEntry = new RequestEntry(USERS) | ||
.withOffset(0) | ||
.withLimit(Integer.MAX_VALUE) | ||
.withQuery(convertIdsToCqlQuery(userIds, "sourceInvoiceId")); | ||
|
||
return restClient.get(requestEntry, JsonObject.class, requestContext); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/org/folio/service/routinglist/RoutingListService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.folio.service.routinglist; | ||
|
||
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.List; | ||
import java.util.UUID; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.core.json.JsonObject; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.folio.models.template.TemplateProcessingRequest; | ||
import org.folio.rest.core.RestClient; | ||
import org.folio.rest.core.models.RequestContext; | ||
import org.folio.rest.core.models.RequestEntry; | ||
import org.folio.rest.jaxrs.model.RoutingList; | ||
import org.folio.service.UserService; | ||
|
||
@Log4j2 | ||
public class RoutingListService { | ||
|
||
private static final String ENDPOINT = resourcesPath(ROUTING_LISTS); | ||
private static final String BY_ID_ENDPOINT = ENDPOINT + "/{id}"; | ||
private final RestClient restClient; | ||
private final UserService userService; | ||
|
||
public RoutingListService(RestClient restClient, UserService userService) { | ||
this.restClient = restClient; | ||
this.userService = userService; | ||
} | ||
|
||
public Future<JsonObject> processTemplateEngine(String id, RequestContext requestContext) { | ||
return getRoutingListById(id, requestContext) | ||
.compose(routingList -> fetchUsersAndCreateTemplate(routingList, requestContext)) | ||
.compose(templateProcessingRequest -> postTemplateRequest(templateProcessingRequest, requestContext)); | ||
} | ||
|
||
public Future<RoutingList> getRoutingListById(String routingListId, RequestContext requestContext) { | ||
var requestEntry = new RequestEntry(BY_ID_ENDPOINT).withId(routingListId); | ||
return restClient.get(requestEntry, RoutingList.class, requestContext); | ||
} | ||
|
||
private Future<TemplateProcessingRequest> fetchUsersAndCreateTemplate(RoutingList routingList, RequestContext requestContext) { | ||
return userService.getUsersByIds(routingList.getUserIds(), requestContext) | ||
.map(users -> createTemplateRequest(routingList, users)); | ||
} | ||
|
||
private TemplateProcessingRequest createTemplateRequest(RoutingList routingList, JsonObject users) { | ||
var templateRequest =createBaseTemplateRequest(); | ||
var userListForContext = createUserListForContext(users); | ||
var context = new TemplateProcessingRequest.Context().withUsers(userListForContext); | ||
templateRequest.withContext(context); | ||
return templateRequest; | ||
} | ||
|
||
private TemplateProcessingRequest createBaseTemplateRequest() { | ||
return new TemplateProcessingRequest() | ||
.withTemplateId(UUID.randomUUID()) | ||
.withLang("en") | ||
.withOutputFormat("text/plain"); | ||
} | ||
|
||
private List<TemplateProcessingRequest.User> createUserListForContext(JsonObject users) { | ||
return users.getJsonArray("users").stream() | ||
.map(JsonObject.class::cast) | ||
.map(user -> new TemplateProcessingRequest.User() | ||
.withName(user.getJsonObject("personal").getString("firstName")) | ||
) | ||
.toList(); | ||
} | ||
|
||
private Future<JsonObject> postTemplateRequest(TemplateProcessingRequest templateProcessingRequest, RequestContext requestContext) { | ||
return restClient.post(TEMPLATE_REQUEST, JsonObject.mapFrom(templateProcessingRequest), JsonObject.class, requestContext); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.