Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.inventory.controllers;

import in.shop2020.generic.ExceptionType;
import in.shop2020.model.v1.catalog.InventoryServiceException;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.purchase.PurchaseServiceException;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.thrift.clients.PurchaseClient;
import in.shop2020.thrift.clients.WarehouseClient;
import in.shop2020.utils.ModelUtils;
import in.shop2020.warehouse.InventoryItem;
import in.shop2020.warehouse.ScanType;
import in.shop2020.warehouse.WarehouseService.Client;
import in.shop2020.warehouse.WarehouseServiceException;

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

import javax.servlet.ServletContext;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings("serial")
@Results({ @Result(name = "redirect", type = "redirectAction", params = {
        "actionName", "warehouse" }) })
public class PurchaseController extends BaseController {

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

    private ServletContext context;
    private String id;
    private String itemId;
    private String itemNo;
    private String errorMsg = "";
    private List<Item> items;

    private String purchaseOrderId;

    public String editNew() {
        purchaseOrderId = request.getParameter("poId");
        return "new";
    }

    public String create() {
        this.purchaseOrderId = request.getParameter("poId");
        long poId = Long.parseLong(purchaseOrderId);
        String invoiceNumber = request.getParameter("invoiceNo");
        String fc = request.getParameter("freightCharges").trim();
        double freightCharges = 0;
        if (fc != null && !fc.isEmpty())
            freightCharges = Double.parseDouble(fc);
        try {
            PurchaseClient purchaseClient = new PurchaseClient();
            in.shop2020.purchase.PurchaseService.Client client = purchaseClient
                    .getClient();
            id = "" + client.startPurchase(poId, invoiceNumber, freightCharges);
        } catch (TTransportException e) {
            errorMsg = "Error while establishing connection to the warehouse server";
            logger.error(errorMsg, e);
        } catch (TException e) {
            errorMsg = "Error while scanning in the item";
            logger.error(errorMsg, e);
        } catch (PurchaseServiceException e) {
            errorMsg = e.getMessage();
            logger.error(errorMsg, e);
        }

        if (errorMsg.isEmpty())
            return "create";
        else {
            addActionError(errorMsg);
            return "new";
        }

    }

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

    public String update() {
        long id = Long.parseLong(this.id);
        String itemNumber = request.getParameter("itemNo");
        String imeiNumber = request.getParameter("imeiNo");

        try {
            WarehouseClient warehouseClient = new WarehouseClient();
            Client client = warehouseClient.getClient();
            InventoryItem inventoryItem = null;
            if (itemId != null) {
                inventoryItem = client.createSerializedInventoryItem(Long.parseLong(itemId), itemNumber, imeiNumber, id);
            }
            else {
                inventoryItem = client.createSerializedInventoryItemFromItemNumber(itemNumber, imeiNumber, id);
            }

            client.scanSerializedItem(inventoryItem.getId(), ScanType.PURCHASE,
                    PurchaseOrderController.WAREHOUSE_ID);
            in.shop2020.model.v1.catalog.InventoryService.Client catalaogClient = new CatalogClient()
                    .getClient();
            catalaogClient.addInventory(inventoryItem.getItemId(), PurchaseOrderController.WAREHOUSE_ID,
                    1);
        } catch (TTransportException e) {
            errorMsg = "Error while establishing connection to the warehouse server";
            logger.error(errorMsg, e);
        } catch (WarehouseServiceException e) {
            errorMsg = e.getMessage();
            logger.error(errorMsg, e);
            if (ExceptionType.MULTIPLE_ITEMS_FOUND.equals(e.getExceptionType())) {
                try {
                    items = new ArrayList<Item>();
                    in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = new CatalogClient().getClient();
                    for (String itemId : e.getMessage().split(",")) {
                        items.add(catalogClient.getItem(Long.parseLong(itemId)));
                    }
                }
                catch(Exception e1) {
                    logger.error("Could not lookup items: " + e.getMessage(), e1);
                }
                errorMsg = "Multiple items found for Item number: " + itemNumber + ". Please select appropriate item.";
            }
        } catch (TException e) {
            errorMsg = "Error while scanning in the item";
            logger.error(errorMsg, e);
        } catch (InventoryServiceException e) {
            errorMsg = "Error while incresing item's inventory availability";
            logger.error(errorMsg, e);
        }
        if (!errorMsg.isEmpty()) {
            addActionError(errorMsg);
        }

        return "show";
    }

    public String destroy() {
        long id = Long.parseLong(this.id);

        try {
            PurchaseClient warehouseClient = new PurchaseClient();
            in.shop2020.purchase.PurchaseService.Client client = warehouseClient
                    .getClient();
            client.closePurchase(id);
        } catch (TTransportException e) {
            errorMsg = "Error while establishing connection to the warehouse server";
            logger.error(errorMsg, e);
        } catch (TException e) {
            errorMsg = "Error while scanning in the item";
            logger.error(errorMsg, e);
        } catch (PurchaseServiceException e) {
            errorMsg = e.getMessage();
            logger.error(errorMsg, e);
        }
        return "redirect";
    }

    public String createItemNumberMapping() {
        long itemIdLong = Long.parseLong(itemId);

        try {
            Client warehouseClient = new WarehouseClient().getClient();
            warehouseClient.createItemNumberMapping(itemNo, itemIdLong);
        } catch (TTransportException e) {
            logger.error("Could not create thrift client", e);
        } catch (TException e) {
            logger.error("Could not create item number mapping", e);
        }

        return show();
    }

    public String getName(Item item){
        return ModelUtils.extractProductNameFromItem(item);
    }

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

    public String getId() {
        return id;
    }

    public String getErrorMessage() {
        return errorMsg;
    }

    public String getPurchaseOrderId() {
        return purchaseOrderId;
    }

    public String getServletContextPath() {
        return context.getContextPath();
    }

    public String getItemId() {
        return itemId;
    }

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

    public String getItemNo() {
        return itemNo;
    }

    public void setItemNo(String itemNo) {
        this.itemNo = itemNo;
    }

    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }

}