Subversion Repositories SmartDukaan

Rev

Rev 4687 | Rev 4846 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4687 mandeep.dh 1
package in.shop2020.inventory.controllers;
2
 
3
import in.shop2020.model.v1.catalog.InventoryServiceException;
4
import in.shop2020.purchase.PurchaseServiceException;
5
import in.shop2020.thrift.clients.CatalogClient;
6
import in.shop2020.thrift.clients.PurchaseClient;
7
import in.shop2020.thrift.clients.WarehouseClient;
8
import in.shop2020.warehouse.InventoryItem;
9
import in.shop2020.warehouse.ScanType;
10
import in.shop2020.warehouse.WarehouseService.Client;
11
import in.shop2020.warehouse.WarehouseServiceException;
12
 
13
import javax.servlet.ServletContext;
14
 
15
import org.apache.struts2.convention.annotation.Result;
16
import org.apache.struts2.convention.annotation.Results;
17
import org.apache.thrift.TException;
18
import org.apache.thrift.transport.TTransportException;
19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
21
 
22
@SuppressWarnings("serial")
23
@Results({ @Result(name = "redirect", type = "redirectAction", params = {
24
        "actionName", "warehouse" }) })
25
public class PurchaseController extends BaseController {
26
 
27
    private static Logger logger = LoggerFactory
28
            .getLogger(PurchaseController.class);
29
 
30
    private ServletContext context;
31
    private String id;
32
    private String itemId;
33
    private String itemNo;
34
    private String errorMsg = "";
35
 
36
    private String purchaseOrderId;
37
 
38
    public String editNew() {
39
        purchaseOrderId = request.getParameter("poId");
40
        return "new";
41
    }
42
 
43
    public String create() {
44
        this.purchaseOrderId = request.getParameter("poId");
45
        long poId = Long.parseLong(purchaseOrderId);
46
        String invoiceNumber = request.getParameter("invoiceNo");
47
        String fc = request.getParameter("freightCharges").trim();
48
        double freightCharges = 0;
49
        if (fc != null && !fc.isEmpty())
50
            freightCharges = Double.parseDouble(fc);
51
        try {
52
            PurchaseClient purchaseClient = new PurchaseClient();
53
            in.shop2020.purchase.PurchaseService.Client client = purchaseClient
54
                    .getClient();
55
            id = "" + client.startPurchase(poId, invoiceNumber, freightCharges);
56
        } catch (TTransportException e) {
57
            errorMsg = "Error while establishing connection to the warehouse server";
58
            logger.error(errorMsg, e);
59
        } catch (TException e) {
60
            errorMsg = "Error while scanning in the item";
61
            logger.error(errorMsg, e);
62
        } catch (PurchaseServiceException e) {
63
            errorMsg = e.getMessage();
64
            logger.error(errorMsg, e);
65
        }
66
 
67
        if (errorMsg.isEmpty())
68
            return "create";
69
        else {
70
            addActionError(errorMsg);
71
            return "new";
72
        }
73
 
74
    }
75
 
76
    public String show() {
77
        return "show";
78
    }
79
 
80
    public String update() {
81
        long id = Long.parseLong(this.id);
82
        String itemNumber = request.getParameter("itemNo");
83
        String imeiNumber = request.getParameter("imeiNo");
84
 
85
        try {
86
            WarehouseClient warehouseClient = new WarehouseClient();
87
            Client client = warehouseClient.getClient();
88
            InventoryItem inventoryItem = client
89
                    .createSerializedInventoryItemFromItemNumber(itemNumber,
90
                            imeiNumber, id);
91
            client.scanSerializedItem(inventoryItem.getId(), ScanType.PURCHASE,
4754 mandeep.dh 92
                    PurchaseOrderController.WAREHOUSE_ID);
4687 mandeep.dh 93
            in.shop2020.model.v1.catalog.InventoryService.Client catalaogClient = new CatalogClient()
94
                    .getClient();
4754 mandeep.dh 95
            catalaogClient.addInventory(inventoryItem.getItemId(), PurchaseOrderController.WAREHOUSE_ID,
4687 mandeep.dh 96
                    1);
97
        } catch (TTransportException e) {
98
            errorMsg = "Error while establishing connection to the warehouse server";
99
            logger.error(errorMsg, e);
100
        } catch (WarehouseServiceException e) {
101
            errorMsg = e.getMessage();
102
            logger.error(errorMsg, e);
103
        } catch (TException e) {
104
            errorMsg = "Error while scanning in the item";
105
            logger.error(errorMsg, e);
106
        } catch (InventoryServiceException e) {
107
            errorMsg = "Error while incresing item's inventory availability";
108
            logger.error(errorMsg, e);
109
        }
110
        if (!errorMsg.isEmpty()) {
111
            addActionError(errorMsg);
112
        }
113
        return "show";
114
    }
115
 
116
    public String destroy() {
117
        long id = Long.parseLong(this.id);
118
 
119
        try {
120
            PurchaseClient warehouseClient = new PurchaseClient();
121
            in.shop2020.purchase.PurchaseService.Client client = warehouseClient
122
                    .getClient();
123
            client.closePurchase(id);
124
        } catch (TTransportException e) {
125
            errorMsg = "Error while establishing connection to the warehouse server";
126
            logger.error(errorMsg, e);
127
        } catch (TException e) {
128
            errorMsg = "Error while scanning in the item";
129
            logger.error(errorMsg, e);
130
        } catch (PurchaseServiceException e) {
131
            errorMsg = e.getMessage();
132
            logger.error(errorMsg, e);
133
        }
134
        return "redirect";
135
    }
136
 
137
    public String createItemNumberMapping() {
138
        long itemIdLong = Long.parseLong(itemId);
139
 
140
        try {
141
            Client warehouseClient = new WarehouseClient().getClient();
142
            warehouseClient.createItemNumberMapping(itemNo, itemIdLong);
143
        } catch (TTransportException e) {
144
            logger.error("Could not create thrift client", e);
145
        } catch (TException e) {
146
            logger.error("Could not create item number mapping", e);
147
        }
148
 
149
        return show();
150
    }
151
 
152
    public void setId(String id) {
153
        this.id = id;
154
    }
155
 
156
    public String getId() {
157
        return id;
158
    }
159
 
160
    public String getErrorMessage() {
161
        return errorMsg;
162
    }
163
 
164
    public String getPurchaseOrderId() {
165
        return purchaseOrderId;
166
    }
167
 
168
    public String getServletContextPath() {
169
        return context.getContextPath();
170
    }
171
 
172
    public String getItemId() {
173
        return itemId;
174
    }
175
 
176
    public void setItemId(String itemId) {
177
        this.itemId = itemId;
178
    }
179
 
180
    public String getItemNo() {
181
        return itemNo;
182
    }
183
 
184
    public void setItemNo(String itemNo) {
185
        this.itemNo = itemNo;
186
    }
187
 
188
}