Subversion Repositories SmartDukaan

Rev

Rev 7762 | Rev 8070 | 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.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

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.model.v1.order.AmazonOrder;
import in.shop2020.support.utils.ReportsUtils;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.thrift.clients.InventoryClient;
import in.shop2020.thrift.clients.LogisticsClient;
import in.shop2020.thrift.clients.TransactionClient;
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.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
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.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ValidationAwareSupport;

@SuppressWarnings("serial")
@InterceptorRefs({
        @InterceptorRef("defaultStack"),
        @InterceptorRef("login")
})
@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 HttpServletRequest request;
        private HttpSession session;
        private ServletContext context;
        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() {
                if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {
                        return "authfail";
                }
                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(customHandlingTime);
                        if ( Boolean.valueOf(isTime)) {
                                flag = true;
                        }
                        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 "index";

        }

        public String upload() throws IOException, TException {
                File fileToCreate = new File("/tmp/", "Amazon-shipping.xls");
                FileUtils.copyFile(this.file, fileToCreate);
                FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
                HSSFWorkbook workbook = new HSSFWorkbook(iFile);
                HSSFSheet sheet = workbook.getSheetAt(0);
                Map<Long,Map<String,String>> amazonOrderMap = new HashMap<Long,Map<String,String>>();
                for (int iterator=sheet.getFirstRowNum();iterator<=sheet.getLastRowNum();iterator++){
                        TransactionClient tcl = new TransactionClient();
                        List<AmazonOrder> orders = tcl.getClient().getAmazonOrderByAmazonOrderId(sheet.getRow(iterator).getCell(0).getStringCellValue());
                        for (AmazonOrder order : orders){
                                if (StringUtils.equals(order.getStatus(), "Order-Payment-Success")){
                                        Map<String,String> dateMap = new HashMap<String,String>();
                                        dateMap.put(sheet.getRow(iterator).getCell(1).getStringCellValue(), sheet.getRow(iterator).getCell(2).getStringCellValue());
                                        amazonOrderMap.put(order.getOrderId(), dateMap);
                                }
                        }
                }
                TransactionClient tcl = new TransactionClient();
                tcl.getClient().updateTimestampForAmazonOrder(amazonOrderMap);
                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, Item> getAliveItemMap() throws TTransportException, InterruptedException{
                List<Item> itemList = null;
                int retry=0;
                while(true){
                        try {
                                if (retry==3)
                                        break;
                                Client CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
                                itemList = CatalogClient.getAllAliveItems();
                                break;
                        } catch ( NullPointerException e) {
                                retry++;
                                System.out.println("Unable to fetch alive items.Retrying");
                                Thread.sleep(3000);
                                e.printStackTrace();
                        } catch (TException e) {
                                retry++;
                                System.out.println("Unable to fetch alive items.Retrying");
                                Thread.sleep(3000);
                                e.printStackTrace();
                        }
                }
                Map<Long, Item> itemMap = new HashMap<Long, Item>();
                for (Item o : itemList) {
                        itemMap.put((Long) o.getId(), o);
                }
                return itemMap;
        }

        public String edit() {
                if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
                        return "authfail";
                }
                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;
        }

        public void setServletRequest(HttpServletRequest req) {
                this.request = req;
                this.session = req.getSession();        
        }

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