Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package in.shop2020.hotspot.dashbaord.server.handler;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import in.shop2020.hotspot.dashbaord.shared.actions.AllOrderListRequest;
import in.shop2020.hotspot.dashbaord.shared.actions.AllOrderListResponse;
import in.shop2020.hotspot.dashbaord.shared.actions.Order;
import in.shop2020.hotspot.dashbaord.shared.actions.OrderType;
import in.shop2020.model.v1.order.Alert;
import in.shop2020.model.v1.order.ExtraOrderInfo;
import in.shop2020.model.v1.order.Transaction;
import in.shop2020.model.v1.order.TransactionStatus;
import in.shop2020.model.v1.order.TransactionService.Client;
import in.shop2020.thrift.clients.TransactionServiceClient;
import net.customware.gwt.dispatch.server.ActionHandler;
import net.customware.gwt.dispatch.server.ExecutionContext;
import net.customware.gwt.dispatch.shared.ActionException;

public class AllOrderListHandler implements ActionHandler<AllOrderListRequest, AllOrderListResponse>{

        private OrderType type;
        
        
        @Override
        public AllOrderListResponse execute(AllOrderListRequest request,
                        ExecutionContext context) throws ActionException {
                // TODO Auto-generated method stub
                
                type = request.getType();
                AllOrderListResponse or = new AllOrderListResponse(type, getInitTransactions());
                return or;
        }

        @Override
        public Class<AllOrderListRequest> getActionType() {
                // TODO Auto-generated method stub
                return AllOrderListRequest.class;
        }

        @Override
        public void rollback(AllOrderListRequest arg0, AllOrderListResponse arg1,
                        ExecutionContext arg2) throws ActionException {
                // TODO Auto-generated method stub
                
        }
        
        private List<Order> getInitTransactions(){
                List<Order> orders = new ArrayList<Order>();
                try{
                        TransactionServiceClient client = new TransactionServiceClient();
                        Client c = client.getClient();
                        List<Transaction> txns = c.getAllTransactions(null, 0L, new Date().getTime());
                        for (Transaction t: txns){
                                long sku_id;
                                String model;
                                String vendor;
                                String colour;
                                ExtraOrderInfo eoi = c.getExtraInfo(t.getId());
                                if(eoi == null){
                                        sku_id = 34;
                                        model = "N 72";
                                        colour = "Black";
                                        vendor = "Nokia";
                                }else{
                                        sku_id = eoi.getSku_id();
                                        model = eoi.getModel_name();
                                        vendor = eoi.getVendor_info();
                                        colour = eoi.getColour();
                                }
                                
                                Order o = new Order(t.getCustomer_id(), t.getId(), t.getCreatedOn(), t.getExpectedDeliveryTime(), t.getTransactionStatus().toString(), t.getStatusDescription(), model, colour, vendor, sku_id);
                                //Order o = new Order(t.getCustomer_id(), t.getId(), t.getCreatedOn(), t.getExpectedDeliveryTime(), t.getTransactionStatus().toString(), t.getStatusDescription());
                                orders.add(o);
                                //check if it has an associated alert
                                List<Alert> alerts = c.getAlerts(t.getId(), true);
                                if(alerts != null){
                                        if (alerts.size() != 0){
                                                o.setAlert(true);
                                                o.setStatusMessage(alerts.iterator().next().getComment());
                                        }
                                }else{
                                        o.setAlert(false);
                                }
                        }
                        
                }catch(Exception e){
                        System.out.println(e);
                }
                return orders;
        }

}