Subversion Repositories SmartDukaan

Rev

Rev 4754 | Go to most recent revision | Details | 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
            long warehouseId = 7;
89
            InventoryItem inventoryItem = client
90
                    .createSerializedInventoryItemFromItemNumber(itemNumber,
91
                            imeiNumber, id);
92
            client.scanSerializedItem(inventoryItem.getId(), ScanType.PURCHASE,
93
                    warehouseId);
94
            in.shop2020.model.v1.catalog.InventoryService.Client catalaogClient = new CatalogClient()
95
                    .getClient();
96
            catalaogClient.addInventory(inventoryItem.getItemId(), warehouseId,
97
                    1);
98
        } catch (TTransportException e) {
99
            errorMsg = "Error while establishing connection to the warehouse server";
100
            logger.error(errorMsg, e);
101
        } catch (WarehouseServiceException e) {
102
            errorMsg = e.getMessage();
103
            logger.error(errorMsg, e);
104
        } catch (TException e) {
105
            errorMsg = "Error while scanning in the item";
106
            logger.error(errorMsg, e);
107
        } catch (InventoryServiceException e) {
108
            errorMsg = "Error while incresing item's inventory availability";
109
            logger.error(errorMsg, e);
110
        }
111
        if (!errorMsg.isEmpty()) {
112
            addActionError(errorMsg);
113
        }
114
        return "show";
115
    }
116
 
117
    public String destroy() {
118
        long id = Long.parseLong(this.id);
119
 
120
        try {
121
            PurchaseClient warehouseClient = new PurchaseClient();
122
            in.shop2020.purchase.PurchaseService.Client client = warehouseClient
123
                    .getClient();
124
            client.closePurchase(id);
125
        } catch (TTransportException e) {
126
            errorMsg = "Error while establishing connection to the warehouse server";
127
            logger.error(errorMsg, e);
128
        } catch (TException e) {
129
            errorMsg = "Error while scanning in the item";
130
            logger.error(errorMsg, e);
131
        } catch (PurchaseServiceException e) {
132
            errorMsg = e.getMessage();
133
            logger.error(errorMsg, e);
134
        }
135
        return "redirect";
136
    }
137
 
138
    public String createItemNumberMapping() {
139
        long itemIdLong = Long.parseLong(itemId);
140
 
141
        try {
142
            Client warehouseClient = new WarehouseClient().getClient();
143
            warehouseClient.createItemNumberMapping(itemNo, itemIdLong);
144
        } catch (TTransportException e) {
145
            logger.error("Could not create thrift client", e);
146
        } catch (TException e) {
147
            logger.error("Could not create item number mapping", e);
148
        }
149
 
150
        return show();
151
    }
152
 
153
    public void setId(String id) {
154
        this.id = id;
155
    }
156
 
157
    public String getId() {
158
        return id;
159
    }
160
 
161
    public String getErrorMessage() {
162
        return errorMsg;
163
    }
164
 
165
    public String getPurchaseOrderId() {
166
        return purchaseOrderId;
167
    }
168
 
169
    public String getServletContextPath() {
170
        return context.getContextPath();
171
    }
172
 
173
    public String getItemId() {
174
        return itemId;
175
    }
176
 
177
    public void setItemId(String itemId) {
178
        this.itemId = itemId;
179
    }
180
 
181
    public String getItemNo() {
182
        return itemNo;
183
    }
184
 
185
    public void setItemNo(String itemNo) {
186
        this.itemNo = itemNo;
187
    }
188
 
189
}