package cs4224;

import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

import java.util.List;

/**
 * Created by Jiang Sheng on 4/10/15.
 */
public class OrderStatus {
    private PreparedStatement customerQuery;
    private PreparedStatement orderLineQuery;
    private PreparedStatement orderQuery;
    private Session session;

    public OrderStatus(SimpleClient client) {
        session = client.getSession();

        this.customerQuery = session.prepare("select c_first, c_middle, c_last, c_balance from customers where c_w_id = ? and c_d_id = ? and c_id = ?;");
        this.orderLineQuery = session.prepare("select ol_i_id, ol_supply_w_id, ol_quantity, ol_amount, ol_delivery_d from orderlines where ol_w_id = ? and ol_d_id = ? and ol_o_id = ?;");
        this.orderQuery = session.prepare("select o_id, o_entry_d, o_carrier_id from orders where o_w_id = ? and o_d_id = ? and o_c_id = ?;");
    }

    /**
     * Get Order Status data
     * @param c_w_id
     * @param c_d_id
     * @param c_id
     */
    public void getOrderStatus(int c_w_id, int c_d_id, int c_id) {
        // retrieve customer's information.
        ResultSet results = session.execute(customerQuery.bind(c_w_id, c_d_id, c_id));

        // should be a single value
        Row customer = results.all().get(0);
        System.out.println("Customer Info:");
        System.out.println(String.format("Name: %s %s %s ,Balance: %.4f", customer.getString("c_first"), customer.getString("c_middle"),
                customer.getString("c_last"), customer.getFloat("c_balance")));

        // retrieve order information for this customer
        results = session.execute(orderQuery.bind(c_w_id, c_d_id, c_id));
        List<Row> allOrders = results.all();
        if (allOrders.size() == 0) {
            return;
        }
        // take the largest value as last order
        int targetIndex = 0;
        int lastOrderId = allOrders.get(0).getInt("o_id");
        // keep index for target order in the list
        for (int i=0; i<allOrders.size();i++) {
            Row row = allOrders.get(i);
            if (row.getInt("o_id") > lastOrderId) {
                targetIndex = i;
            }
        }

        Row lastOrder = allOrders.get(targetIndex);
        int orderId = lastOrder.getInt("o_id");
        System.out.println("Last Order:");
        System.out.println(String.format("id: %d, time: %s, carrier_id: %d", orderId,
                lastOrder.getTimestamp("o_entry_d"), lastOrder.getInt("o_carrier_id")));

        // retrieve order-line for this order.
        results = session.execute(orderLineQuery.bind(c_w_id, c_d_id, orderId));
        System.out.println("Items in this order:");
        for (Row row : results) {
            System.out.println(String.format("%d, %d, %d, %.4f, %s ",row.getInt("ol_i_id"), row.getInt("ol_supply_w_id"),
                    row.getInt("ol_quantity"), row.getFloat("ol_amount"), row.getTimestamp("ol_delivery_d")));
        }

        System.out.println();
    }

    public static void main(String[] args) {
        SimpleClient client = new SimpleClient();
        client.connect("127.0.0.1", "cs4224");

        OrderStatus transaction = new OrderStatus(client);
        transaction.getOrderStatus(1, 1, 5);
        transaction.getOrderStatus(3, 2, 20);
        transaction.getOrderStatus(2, 4, 10);

        client.close();
    }
}

@@ -46,148 +46,38 @@
 ore.
-PreparedStatement;%0Aimport com.datastax.driver.core.ResultSet;%0Aimport com.datastax.driver.core.Row;%0Aimport com.datastax.driver.core.Session;%0A
+*;%0A%0Aimport java.sql.Timestamp;
 %0Aimp
@@ -95,16 +95,38 @@
 il.List;
+%0Aimport java.util.Map;
 %0A%0A/**%0A *
@@ -599,195 +599,8 @@
 %22);%0A
-        this.orderLineQuery = session.prepare(%22select ol_i_id, ol_supply_w_id, ol_quantity, ol_amount, ol_delivery_d from orderlines where ol_w_id = ? and ol_d_id = ? and ol_o_id = ?;%22);%0A
     
@@ -674,16 +674,23 @@
 rrier_id
+, o_ols
  from or
@@ -2121,32 +2121,33 @@
 getInt(%22o_id%22);%0A
+%0A
         System.o
@@ -2408,166 +2408,176 @@
     
-results = session.execute(orderLineQuery.bind(c_w_id, c_d_id, orderId));%0A        System.out.println(%22Items in this order:%22);%0A        for (Row row : results) %7B
+Map%3CInteger, UDTValue%3E ols = lastOrder.getMap(%22o_ols%22, Integer.class, UDTValue.class);%0A        for (Integer key: ols.keySet()) %7B%0A            UDTValue ol = ols.get(key);
 %0A   
@@ -2642,19 +2642,18 @@
 f, %25s %22,
-row
+ol
 .getInt(
@@ -2660,27 +2660,26 @@
 %22ol_i_id%22), 
-row
+ol
 .getInt(%22ol_
@@ -2713,19 +2713,18 @@
         
-row
+ol
 .getInt(
@@ -2735,27 +2735,26 @@
 quantity%22), 
-row
+ol
 .getFloat(%22o
@@ -2765,19 +2765,18 @@
 ount%22), 
-row
+ol
 .getTime
@@ -3092,17 +3092,17 @@
 s(1, 1, 
-5
+1
 );%0A     
@@ -3184,13 +3184,12 @@
 (2, 
-4
+1
 , 1
-0
 );%0A%0A

Change Type 3 for new schema.