Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.support.controllers;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import in.shop2020.model.v1.catalog.Amazonlisted;
import in.shop2020.model.v1.catalog.CatalogServiceException;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.model.v1.inventory.AmazonInventorySnapshot;
import in.shop2020.model.v1.catalog.CatalogService.Client;
import in.shop2020.model.v1.inventory.InventoryServiceException;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.thrift.clients.InventoryClient;
import in.shop2020.thrift.clients.LogisticsClient;
import in.shop2020.logistics.DeliveryType;
import in.shop2020.logistics.LogisticsInfo;
import in.shop2020.logistics.LogisticsServiceException;
import in.shop2020.logistics.PickUpType;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ValidationAwareSupport;

@SuppressWarnings("serial")
@Results({
        @Result(name = "redirect", location = "${url}", type = "redirect"),
        @Result(name="authsuccess", type="redirectAction", params = {"actionName" , "reports"})
})
public class AmazonListController extends ValidationAwareSupport implements ServletRequestAware {

        private static Logger logger = LoggerFactory.getLogger(AmazonListController.class);

        private ServletRequest request;
        private String url;
        private String id;
        private String itemId;
        private String isFba;
        private String isNonFba;
        private String isInventoryOverride;
        private String fbaPrice;
        private String sellingPrice;
        private String saholicPrice;
        private String isTime;
        private String handlingTime;
        private String customHandlingTime;
        private String holdInventory;
        private String defaultInventory;
        private String fileUploadFileName;
        private String fileUploadContentType;
        private File file;

        public String index() {
                return "index";
        }


        public List<Amazonlisted> fetchItems() throws TException {
                Client CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
                return CatalogClient.getAllAmazonListedItems();
        }

        public Amazonlisted fetchItemDetail() throws NumberFormatException, TException {
                Client CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
                return CatalogClient.getAmazonItemDetails(Long.valueOf(id));
        }

        public Item getSaholicItem(String id) throws NumberFormatException, CatalogServiceException, TException {
                Client CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
                return CatalogClient.getItem(Long.valueOf(id));
        }



        public String update() throws NumberFormatException, TException{
                Client CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
                if ( Double.valueOf(fbaPrice) > Double.valueOf(saholicPrice) || Double.valueOf(sellingPrice) > Double.valueOf(saholicPrice) ) {
                        addActionError("Failed To Do Changes");
                }
                else {
                        boolean flag=false;
                        long delay = Long.valueOf(handlingTime);
                        if ( Boolean.valueOf(isTime)) {
                                flag = true;
                                delay = Long.valueOf(customHandlingTime);
                        }
                        changeItemInventory();
                        CatalogClient.updateAmazonItemDetails(Long.valueOf(itemId),Double.valueOf(fbaPrice),Double.valueOf(sellingPrice),Boolean.valueOf(isFba),Boolean.valueOf(isNonFba),Boolean.valueOf(isInventoryOverride),delay,flag);
                }
                setUrl("/amazon-list/");
                return "redirect";

        }

        public String upload() throws IOException {
                File fileToCreate = new File("/tmp/", "Amazon-shipping.xls");
                FileUtils.copyFile(this.file, fileToCreate);
                return "authsuccess";
        }

        private void changeItemInventory() throws NumberFormatException, TException {
                Client CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();

                if (StringUtils.isEmpty(holdInventory)) {
                        holdInventory = "0";
                }
                if (StringUtils.isEmpty(defaultInventory)) {
                        defaultInventory = "0";
                }

                CatalogClient.updateItemInventory(Long.valueOf(itemId),Long.valueOf(holdInventory),Long.valueOf(defaultInventory));
        }


        public Map<Long, Long> getAvailableItemInventory(String itemId) throws NumberFormatException, InventoryServiceException, TException{
                InventoryClient inventoryServiceClient = new InventoryClient();
                in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
                in.shop2020.model.v1.inventory.ItemInventory thriftItemInventory = inventoryClient.getItemInventoryByItemId(Long.valueOf(itemId));
                return thriftItemInventory.getAvailability();

        }

        public Map<Long, Long> getReservedItemInventory(String itemId) throws NumberFormatException, InventoryServiceException, TException{
                InventoryClient inventoryServiceClient = new InventoryClient();
                in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
                in.shop2020.model.v1.inventory.ItemInventory thriftItemInventory = inventoryClient.getItemInventoryByItemId(Long.valueOf(itemId));
                return thriftItemInventory.getReserved();

        }

        public String getWarehouseName(String warehouseId) throws NumberFormatException, TException { 
                InventoryClient inventoryServiceClient = new InventoryClient();
                return inventoryServiceClient.getClient().getWarehouseName(Long.valueOf(warehouseId));
        }

        public AmazonInventorySnapshot getInventoryForAmazonItem(String itemId) throws NumberFormatException, TException {
                InventoryClient inventoryServiceClient = new InventoryClient();
                try { 
                        return inventoryServiceClient.getClient().getAmazonInventoryForItem(Long.valueOf(itemId));
                }
                catch (Exception e){
                        return null;
                }
        }

        public Long getTimetoShip(String itemId) throws LogisticsServiceException, TException{
                LogisticsClient logisticsClient = new LogisticsClient();
                return logisticsClient.getClient().getLogisticsInfo("110001", Long.valueOf(itemId), DeliveryType.COD, PickUpType.COURIER).getShippingTime();

        }

        public  Map<Long, String> getAliveItemMap() throws TException{
                Client CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
                List<Item> itemList = CatalogClient.getAllAliveItems();
                Map<Long, String> itemMap = new HashMap<Long, String>();
                for (Item o : itemList) {
                        itemMap.put((Long) o.getId(), (String) o.getBrand()+" "+o.getModelName()+" "+o.getModelNumber()+" "+o.getColor());
                }
                return itemMap;
        }

        public String edit() {
                return "edit";
        }

        public String show() {
                return "show";
        }

        public String editNew() {
                return "editNew";
        }

        public void setId(String id) {
                this.id = id;
        }

        public void setUrl(String url) {
                this.url = url;
        }

        public String getUrl() {
                return url;
        }

        public String getId() {
                return id;
        }

        public void setItemId(String itemId) {
                this.itemId = itemId;
        }

        public String getItemId() {
                return itemId;
        }

        public void setIsFba(String isFba) {
                this.isFba = isFba;
        }

        public String getIsFba() {
                return isFba;
        }

        public void setIsNonFba(String isNonFba) {
                this.isNonFba = isNonFba;
        }

        public String getIsNonFba() {
                return isNonFba;
        }

        public void setIsInventoryOverride(String isInventoryOverride) {
                this.isInventoryOverride = isInventoryOverride;
        }

        public String getIsInventoryOverride() {
                return isInventoryOverride;
        }

        public void setSellingPrice(String sellingPrice) {
                this.sellingPrice = sellingPrice;
        }

        public String getSellingPrice() {
                return sellingPrice;
        }

        public void setFbaPrice(String fbaPrice) {
                this.fbaPrice = fbaPrice;
        }

        public String getFbaPrice() {
                return fbaPrice;
        }

        public void setSaholicPrice(String saholicPrice) {
                this.saholicPrice = saholicPrice;
        }

        public String getSaholicPrice() {
                return saholicPrice;
        }

        public void setIsTime(String isTime){
                this.isTime = isTime;
        }

        public String getIsTime(){
                return isTime;
        }

        public void setHandlingTime(String handlingTime){
                this.handlingTime = handlingTime;
        }

        public String getHandlingTime(){
                return handlingTime;
        }

        public void setCustomHandlingTime(String customHandlingTime){
                this.customHandlingTime = customHandlingTime;
        }

        public String getCustomHandlingTime(){
                return customHandlingTime;
        }

        public void setHoldInventory(String holdInventory){
                this.holdInventory = holdInventory;
        }

        public String getHoldInventory(){
                return holdInventory;
        }

        public void setDefaultInventory(String defaultInventory){
                this.defaultInventory = defaultInventory;
        }

        public String getDefaultInventory(){
                return defaultInventory;
        }

        public File getFile() {
                return file;
        }

        public void setFile(File file) {
                this.file = file;
        }

        public String getFileUploadContentType() {
                return fileUploadContentType;
        }

        public void setFileUploadContentType(String fileUploadContentType) {
                this.fileUploadContentType = fileUploadContentType;
        }

        public String getFileUploadFileName() {
                return fileUploadFileName;
        }

        public void setFileUploadFileName(String fileUploadFileName) {
                this.fileUploadFileName = fileUploadFileName;
        }

        @Override
        public void setServletRequest(HttpServletRequest request) {
                this.request = request;

        }

        public static void main(String[] args) throws NumberFormatException, InventoryServiceException, TException {
                AmazonListController call = new AmazonListController();
                System.out.println(call.getInventoryForAmazonItem("5"));
        }
}