Subversion Repositories SmartDukaan

Rev

Rev 1023 | Rev 3125 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.support.controllers;

import java.util.List;
import java.util.HashMap;
import java.util.Map;

import in.shop2020.model.v1.catalog.InventoryService.Client;
import in.shop2020.model.v1.catalog.Warehouse;
import in.shop2020.support.models.InventoryUpdate;
import in.shop2020.support.models.Update;
import in.shop2020.thrift.clients.CatalogServiceClient;

import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;
import com.opensymphony.xwork2.ModelDriven;

public class UpdatesController implements ModelDriven<InventoryUpdate>{

        private InventoryUpdate inventoryUpdate = new InventoryUpdate();
        private String timestamp;
        
        private int errorCode = 0;
        private String errorMessage;
        
        private String id;
        
        
        public UpdatesController(){
                
        }
        
        @Override
        public InventoryUpdate getModel() {
                return this.inventoryUpdate;
        }
        
        public HttpHeaders show(){
                System.out.println("Warehouse Id is:  " + getId());
                CatalogServiceClient catalogServiceClient;
                Client client;
                
                try {
                        catalogServiceClient = new CatalogServiceClient();
                        client = catalogServiceClient.getClient();
                        long warehouseId = Long.parseLong(getId());
                        Warehouse warehouse = client.getWarehouse(warehouseId);
                        setTimestamp(warehouse.getVendorString());
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return new DefaultHttpHeaders("lsuccess");
        }
        
        
        public HttpHeaders create(){
                System.out.println(inventoryUpdate);
                // need to store all data from object to service
                CatalogServiceClient catalogServiceClient;
                Client client;
                try {
                        catalogServiceClient = new CatalogServiceClient();
                        client = catalogServiceClient.getClient();
                        Long warehouseId = Long.parseLong(inventoryUpdate.getwarehouseId());
                        String timestamp = inventoryUpdate.getCurrentTimestamp();
                        Map<String, Long> availability = new HashMap<String, Long>();
                        List<Update> updates = inventoryUpdate.getUpdates();
                        for(Update update: updates){
                                String key = update.getKey();
                                long quantity = (long) Double.parseDouble(update.getQuantity());
                                if(availability.containsKey(key)){
                                        quantity = quantity + availability.get(key);
                                }
                                availability.put(key, quantity);
                        }
                        client.updateInventory(warehouseId, timestamp, availability);
                        
                } catch (Exception e) {
                        e.printStackTrace();
                }
                
                return new DefaultHttpHeaders("psuccess");

        }

        public int getErrorCode() {
                return errorCode;
        }

        public String getErrorMessage() {
                return errorMessage;
        }
        
        public String getId(){
                return id;
        }
        
        public void setId(String id){
                this.id = id;
        }

        public InventoryUpdate getInventoryUpdate() {
                return inventoryUpdate;
        }

        public void setInventoryUpdate(InventoryUpdate inventoryUpdate) {
                this.inventoryUpdate = inventoryUpdate;
        }

        public void setTimestamp(String timestamp) {
                this.timestamp = timestamp;
        }

        public String getTimestamp() {
                return timestamp;
        }       
}