use of org.folio.orders.utils.HelperUtils.COMPOSITE_PO_LINES in project mod-orders by folio-org.
the class MockServer method handleGetPoLines.
private void handleGetPoLines(RoutingContext ctx, String type) {
logger.info("handleGetPoLines got: {}?{}", ctx.request().path(), ctx.request().query());
String queryParam = StringUtils.trimToEmpty(ctx.request().getParam("query"));
addServerRqQuery(type, queryParam);
if (queryParam.contains(BAD_QUERY)) {
serverResponse(ctx, 400, APPLICATION_JSON, Response.Status.BAD_REQUEST.getReasonPhrase());
} else if (queryParam.contains(ID_FOR_INTERNAL_SERVER_ERROR) || queryParam.contains(PO_ID_GET_LINES_INTERNAL_SERVER_ERROR)) {
serverResponse(ctx, 500, APPLICATION_JSON, Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase());
} else {
String poId = EMPTY;
String tenant = ctx.request().getHeader(OKAPI_HEADER_TENANT);
List<String> polIds = Collections.emptyList();
if (queryParam.contains(PURCHASE_ORDER_ID)) {
Matcher matcher = Pattern.compile(".*" + PURCHASE_ORDER_ID + "==(\\S[^)]+).*").matcher(queryParam);
poId = matcher.find() ? matcher.group(1) : EMPTY;
} else if (queryParam.startsWith("id==")) {
polIds = extractIdsFromQuery(queryParam);
}
List<JsonObject> postedPoLines = getRqRsEntries(HttpMethod.SEARCH, type);
try {
PoLineCollection poLineCollection = new PoLineCollection();
if (postedPoLines.isEmpty()) {
if (poId.equals(ORDER_ID_WITH_PO_LINES) || !polIds.isEmpty()) {
poLineCollection = new JsonObject(getMockData(POLINES_COLLECTION)).mapTo(PoLineCollection.class);
// Filter PO Lines either by PO id or by expected line ids
Iterator<PoLine> iterator = poLineCollection.getPoLines().iterator();
while (iterator.hasNext()) {
PoLine poLine = iterator.next();
if (polIds.isEmpty() ? !poId.equals(poLine.getPurchaseOrderId()) : !polIds.contains(poLine.getId())) {
iterator.remove();
}
}
poLineCollection.setTotalRecords(poLineCollection.getPoLines().size());
} else {
String filePath;
if (ID_FOR_PRINT_MONOGRAPH_ORDER.equals(poId)) {
filePath = LISTED_PRINT_MONOGRAPH_PATH;
} else {
filePath = String.format("%s%s.json", COMP_ORDER_MOCK_DATA_PATH, poId);
}
JsonObject compPO = new JsonObject(getMockData(filePath));
// Build PoLineCollection to make sure content is valid
poLineCollection = buildPoLineCollection(tenant, compPO.getJsonArray(COMPOSITE_PO_LINES), poId);
}
} else {
// Attempt to find POLine in mock server memory
poLineCollection.getPoLines().addAll(postedPoLines.stream().map(jsonObj -> jsonObj.mapTo(PoLine.class)).collect(Collectors.toList()));
}
poLineCollection.setTotalRecords(poLineCollection.getPoLines().size());
// Update calculated data
updatePoLineCalculatedData(poLineCollection);
JsonObject po_lines = JsonObject.mapFrom(poLineCollection);
logger.info(po_lines.encodePrettily());
addServerRqRsData(HttpMethod.GET, type, po_lines);
serverResponse(ctx, 200, APPLICATION_JSON, po_lines.encode());
} catch (NoSuchFileException e) {
PoLineCollection poLineCollection = new PoLineCollection();
// Attempt to find POLine in mock server memory
if (postedPoLines != null) {
String finalPoId = poId;
poLineCollection.getPoLines().addAll(postedPoLines.stream().map(json -> json.mapTo(PoLine.class)).filter(line -> finalPoId.equals(line.getPurchaseOrderId())).collect(Collectors.toList()));
}
poLineCollection.setTotalRecords(poLineCollection.getPoLines().size());
JsonObject entries = JsonObject.mapFrom(poLineCollection);
addServerRqRsData(HttpMethod.GET, type, entries);
serverResponse(ctx, 200, APPLICATION_JSON, entries.encodePrettily());
} catch (IOException e) {
PoLineCollection poLineCollection = new PoLineCollection();
poLineCollection.setTotalRecords(0);
JsonObject entries = JsonObject.mapFrom(poLineCollection);
addServerRqRsData(HttpMethod.GET, type, entries);
serverResponse(ctx, 200, APPLICATION_JSON, JsonObject.mapFrom(poLineCollection).encodePrettily());
}
}
}
use of org.folio.orders.utils.HelperUtils.COMPOSITE_PO_LINES in project mod-orders by folio-org.
the class MockServer method handleGetPurchaseOrderByQuery.
private void handleGetPurchaseOrderByQuery(RoutingContext ctx, String orderType) {
String query = StringUtils.trimToEmpty(ctx.request().getParam("query"));
addServerRqQuery(orderType, query);
JsonObject po = new JsonObject();
PurchaseOrderCollection orderCollection = new PurchaseOrderCollection();
// Attempt to find POLine in mock server memory
List<JsonObject> postedOrders = serverRqRs.column(HttpMethod.SEARCH).get(orderType);
if (postedOrders != null) {
orderCollection.withPurchaseOrders(postedOrders.stream().peek(order -> order.remove(COMPOSITE_PO_LINES)).map(order -> order.mapTo(PurchaseOrder.class)).collect(Collectors.toList())).withTotalRecords(orderCollection.getPurchaseOrders().size());
po = JsonObject.mapFrom(orderCollection);
po.remove(COMPOSITE_PO_LINES);
po.remove("totalEstimatedPrice");
po.remove("totalItems");
addServerRqRsData(HttpMethod.GET, orderType, po);
} else {
if (query.contains(BAD_QUERY)) {
serverResponse(ctx, 400, APPLICATION_JSON, Response.Status.BAD_REQUEST.getReasonPhrase());
} else if (query.contains(ID_FOR_INTERNAL_SERVER_ERROR)) {
serverResponse(ctx, 500, APPLICATION_JSON, Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase());
} else {
addServerRqRsData(HttpMethod.GET, orderType, po);
Matcher matcher = Pattern.compile(".*poNumber==(\\S[^)]+).*").matcher(query);
final String poNumber = matcher.find() ? matcher.group(1) : EMPTY;
switch(poNumber) {
case EXISTING_PO_NUMBER:
po.put(TOTAL_RECORDS, 1);
break;
case NONEXISTING_PO_NUMBER:
po.put(TOTAL_RECORDS, 0);
break;
case EMPTY:
po.put(TOTAL_RECORDS, 0);
break;
default:
// modify later as needed
po.put(TOTAL_RECORDS, 0);
}
}
}
serverResponse(ctx, 200, APPLICATION_JSON, po.encodePrettily());
}
Aggregations