Search in sources :

Example 1 with PlacedOrder

use of it.unibo.dronesecurity.userapplication.shipping.courier.entities.PlacedOrder in project DroneSecurity by mirko-felice.

the class CourierShippingService method performDelivery.

private void performDelivery(@NotNull final RoutingContext routingContext) {
    final RequestParameters params = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
    final JsonObject body = params.body().getJsonObject();
    final PlacedOrder order = body.getJsonObject(OrderConstants.ORDER_KEY).mapTo(PlacedOrder.class);
    if (order == null)
        routingContext.response().setStatusCode(CLIENT_ERROR_CODE).end();
    else {
        final DeliveringOrder deliveringOrder = order.deliver();
        OrderRepository.getInstance().delivering(deliveringOrder);
        final JsonNode messageJson = new ObjectMapper().createObjectNode().put(MqttMessageParameterConstants.SYNC_PARAMETER, MqttMessageValueConstants.PERFORM_DELIVERY_MESSAGE).put(MqttMessageParameterConstants.ORDER_ID_PARAMETER, order.getId()).put(MqttMessageParameterConstants.COURIER_PARAMETER, body.getString(OrderConstants.COURIER_KEY));
        final Connection connection = Connection.getInstance();
        connection.publish(MqttTopicConstants.ORDER_TOPIC, messageJson);
        connection.subscribe(MqttTopicConstants.LIFECYCLE_TOPIC, this::callback);
        routingContext.response().end(CORRECT_RESPONSE_TO_PERFORM_DELIVERY);
    }
}
Also used : DeliveringOrder(it.unibo.dronesecurity.userapplication.shipping.courier.entities.DeliveringOrder) Connection(it.unibo.dronesecurity.lib.Connection) JsonObject(io.vertx.core.json.JsonObject) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RequestParameters(io.vertx.ext.web.validation.RequestParameters) PlacedOrder(it.unibo.dronesecurity.userapplication.shipping.courier.entities.PlacedOrder)

Example 2 with PlacedOrder

use of it.unibo.dronesecurity.userapplication.shipping.courier.entities.PlacedOrder in project DroneSecurity by mirko-felice.

the class OrdersController method performDelivery.

@FXML
private void performDelivery() {
    final Optional<Order> selectedOrder = this.getSelectedOrder();
    selectedOrder.ifPresentOrElse(order -> {
        // TODO how to check ???
        if (order instanceof PlacedOrder) {
            final JsonObject body = new JsonObject().put(OrderConstants.ORDER_KEY, order).put(OrderConstants.COURIER_KEY, UserHelper.getLoggedUser().getUsername());
            VertxHelper.WEB_CLIENT.post(PORT, HOST, PERFORM_DELIVERY_URI).putHeader("Content-Type", "application/json").sendBuffer(body.toBuffer()).onSuccess(h -> Platform.runLater(() -> {
                ((Stage) this.performDeliveryButton.getScene().getWindow()).close();
                final URL fileUrl = getClass().getResource(MONITORING_FILENAME);
                final FXMLLoader fxmlLoader = new FXMLLoader(fileUrl);
                FXHelper.initializeWindow(Modality.NONE, "Monitoring...", fxmlLoader).ifPresent(Stage::show);
            }));
        } else
            AlertUtils.showErrorAlert("You can NOT deliver an order that isn't placed.");
    }, NOT_SELECTED_RUNNABLE);
}
Also used : Order(it.unibo.dronesecurity.userapplication.shipping.courier.entities.Order) PlacedOrder(it.unibo.dronesecurity.userapplication.shipping.courier.entities.PlacedOrder) JsonObject(io.vertx.core.json.JsonObject) FXMLLoader(javafx.fxml.FXMLLoader) URL(java.net.URL) PlacedOrder(it.unibo.dronesecurity.userapplication.shipping.courier.entities.PlacedOrder) FXML(javafx.fxml.FXML)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)2 PlacedOrder (it.unibo.dronesecurity.userapplication.shipping.courier.entities.PlacedOrder)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 RequestParameters (io.vertx.ext.web.validation.RequestParameters)1 Connection (it.unibo.dronesecurity.lib.Connection)1 DeliveringOrder (it.unibo.dronesecurity.userapplication.shipping.courier.entities.DeliveringOrder)1 Order (it.unibo.dronesecurity.userapplication.shipping.courier.entities.Order)1 URL (java.net.URL)1 FXML (javafx.fxml.FXML)1 FXMLLoader (javafx.fxml.FXMLLoader)1