Subversion Repositories SmartDukaan

Rev

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