Skip to content

Instantly share code, notes, and snippets.

@jwmatthews
Created April 1, 2024 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwmatthews/067c5fb8af58d738dff432d26cfca81b to your computer and use it in GitHub Desktop.
Save jwmatthews/067c5fb8af58d738dff432d26cfca81b to your computer and use it in GitHub Desktop.

Model ID: ibm-mistralai/mixtral-8x7b-instruct-v01-q

Reasoning

  1. The first step is to address the issues found in the static source code analysis. We need to replace the javax.json import statements with jakarta.json as Quarkus uses the Jakarta EE API, which uses the jakarta package namespace instead of the javax package namespace. This change is required in lines 13, 14, 15, 16, 17, and 18.

  2. Next, we need to consider the external dependencies in the pom.xml file. Since we are migrating to Quarkus, we need to replace the Java EE dependencies with Quarkus-specific dependencies. For example, we need to replace the javaee-api dependency with the jakarta.platform dependency. Additionally, we need to add the quarkus-resteasy-json-provider-jakarta dependency to enable the Jakarta JSON-P API.

  3. We also need to update the imports to reflect the changes made in step 2. We need to replace the javax.json imports with jakarta.json imports.

  4. We need to update the Transformers class to use the Quarkus-specific APIs instead of the Java EE APIs. For example, we can use the jakarta.json.bind.Jsonb class to convert Java objects to JSON strings and vice versa.

  5. We need to update the Transformers class to use the Quarkus-specific logging APIs instead of the Java EE logging APIs. We can use the org.jboss.logging.Logger class to log messages.

Updated File

package com.redhat.coolstore.utils;

import com.redhat.coolstore.model.CatalogItemEntity;
import com.redhat.coolstore.model.Order;
import com.redhat.coolstore.model.OrderItem;
import com.redhat.coolstore.model.Product;
import com.redhat.coolstore.model.ProductImpl;
import com.redhat.coolstore.model.ShoppingCart;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonObject;
import jakarta.json.JsonReader;
import jakarta.json.JsonWriter;
import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;
import org.jboss.logging.Logger;

import java.util.concurrent.ThreadLocalRandom;

/**
 * Created by tqvarnst on 2017-03-30.
 */
public class Transformers {

    private static final String[] RANDOM_NAMES = {"Sven Karlsson","Johan Andersson","Karl Svensson","Anders Johansson","Stefan Olson","Martin Ericsson"};
    private static final String[] RANDOM_EMAILS = {"sven@gmail.com","johan@gmail.com","karl@gmail.com","anders@gmail.com","stefan@gmail.com","martin@gmail.com"};

    private static Logger log = Logger.getLogger(Transformers.class.getName());

    public static Product toProduct(CatalogItemEntity entity) {
        ProductImpl prod = new ProductImpl();
        prod.setItemId(entity.getItemId());
        prod.setName(entity.getName());
        prod.setDesc(entity.getDesc());
        prod.setPrice(entity.getPrice());
        if (entity.getInventory() != null) {
            prod.setLocation(entity.getInventory().getLocation());
            prod.setLink(entity.getInventory().getLink());
            prod.setQuantity(entity.getInventory().getQuantity());
        } else {
            log.warning("Inventory for " + entity.getName() + "[" + entity.getItemId()+ "] unknown and missing");
        }
        return prod;
    }

    public static String shoppingCartToJson(ShoppingCart cart) {
        JsonArrayBuilder cartItems = Json.createArrayBuilder();
        cart.getShoppingCartItemList().forEach(item -> {
            cartItems.add(Json.createObjectBuilder()
                .add("productSku",item.getProduct().getItemId())
                .add("quantity",item.getQuantity())
            );
        });

        int randomNameAndEmailIndex = ThreadLocalRandom.current().nextInt(RANDOM_NAMES.length);

        JsonObject jsonObject = Json.createObjectBuilder()
            .add("orderValue", Double.valueOf(cart.getCartTotal()))
            .add("customerName",RANDOM_NAMES[randomNameAndEmailIndex])
            .add("customerEmail",RANDOM_EMAILS[randomNameAndEmailIndex])
            .add("retailPrice", cart.getShoppingCartItemList().stream().mapToDouble(i -> i.getQuantity()*i.getPrice()).sum())
            .add("discount", Double.valueOf(cart.getCartItemPromoSavings()))
            .add("shippingFee", Double.valueOf(cart.getShippingTotal()))
            .add("shippingDiscount", Double.valueOf(cart.getShippingPromoSavings()))
            .add("items",cartItems) 
            .build();
        StringWriter w = new StringWriter();
        try (JsonWriter writer = Json.createWriter(w)) {
            writer.write(jsonObject);
        }
        return w.toString();
    }

    public static Order jsonToOrder(String json) {
        JsonReader jsonReader = Json.createReader(new StringReader(json));
        JsonObject rootObject = jsonReader.readObject();
        Order order = new Order();
        order.setCustomerName(rootObject.getString("customerName"));
        order.setCustomerEmail(rootObject.getString("customerEmail"));
        order.setOrderValue(rootObject.getJsonNumber("orderValue").doubleValue());
        order.setRetailPrice(rootObject.getJsonNumber("retailPrice").doubleValue());
        order.setDiscount(rootObject.getJsonNumber("dis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment