Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3381 chandransh 1
package in.shop2020.support.controllers;
2
 
3
import in.shop2020.thrift.clients.WarehouseClient;
4
import in.shop2020.warehouse.ScanType;
5
import in.shop2020.warehouse.WarehouseServiceException;
6
import in.shop2020.warehouse.WarehouseService.Client;
7
 
8
import javax.servlet.ServletContext;
9
import javax.servlet.http.HttpServletRequest;
10
 
11
import org.apache.struts2.interceptor.ServletRequestAware;
12
import org.apache.struts2.util.ServletContextAware;
13
import org.apache.thrift.TException;
14
import org.apache.thrift.transport.TTransportException;
15
import org.slf4j.Logger;
16
import org.slf4j.LoggerFactory;
17
 
3423 chandransh 18
import com.opensymphony.xwork2.ValidationAwareSupport;
3381 chandransh 19
 
3423 chandransh 20
@SuppressWarnings("serial")
21
public class PurchaseController extends ValidationAwareSupport implements  ServletRequestAware, ServletContextAware {
22
 
3381 chandransh 23
    private static Logger logger = LoggerFactory.getLogger(PurchaseController.class);
24
 
25
    protected HttpServletRequest request;
26
    private ServletContext context;
27
    private String id;
28
    private String errorMsg="";
29
 
30
    private String purchaseOrderId;
31
 
32
    public String editNew(){
33
        purchaseOrderId = request.getParameter("poId");
34
        return "new";
35
    }
36
 
37
    public String create(){
38
        this.purchaseOrderId = request.getParameter("poId");
39
        long poId = Long.parseLong(purchaseOrderId);
40
        String invoiceNumber = request.getParameter("invoiceNo");
41
        String fc = request.getParameter("freightCharges").trim();
42
        double freightCharges = 0;
43
        if(fc != null && !fc.isEmpty())
44
            freightCharges = Double.parseDouble(fc);
45
        try {
46
            WarehouseClient warehouseClient = new WarehouseClient();
47
            Client client = warehouseClient.getClient();
48
            id = "" + client.startPurchase(poId, invoiceNumber, freightCharges);
49
        } catch (TTransportException e) {
50
            errorMsg = "Error while establishing connection to the warehouse server";
51
            logger.error(errorMsg, e);
52
        } catch (WarehouseServiceException e) {
3423 chandransh 53
            errorMsg = e.getMessage();
3381 chandransh 54
            logger.error(errorMsg, e);
55
        } catch (TException e) {
56
            errorMsg = "Error while scanning in the item";
57
            logger.error(errorMsg, e);
58
        }
59
 
60
        if(errorMsg.isEmpty())
61
            return "create";
3423 chandransh 62
        else{
63
            addActionError(errorMsg);
3381 chandransh 64
            return "new";
3423 chandransh 65
        }
66
 
3381 chandransh 67
    }
68
 
69
    public String show(){
70
        return "show";
71
    }
72
 
73
    public String update(){
74
        long id = Long.parseLong(this.id);
75
        String itemNumber = request.getParameter("itemNo");
76
        String imeiNumber = request.getParameter("imeiNo");
77
 
78
        try {
79
            WarehouseClient warehouseClient = new WarehouseClient();
80
            Client client = warehouseClient.getClient();
81
            client.scanIn(id, itemNumber, imeiNumber, ScanType.PURCHASE);
82
        } catch (TTransportException e) {
83
            errorMsg = "Error while establishing connection to the warehouse server";
84
            logger.error(errorMsg, e);
85
        } catch (WarehouseServiceException e) {
3423 chandransh 86
            errorMsg = e.getMessage();
3381 chandransh 87
            logger.error(errorMsg, e);
88
        } catch (TException e) {
89
            errorMsg = "Error while scanning in the item";
90
            logger.error(errorMsg, e);
91
        }
3423 chandransh 92
        if(!errorMsg.isEmpty()){
93
            addActionError(errorMsg);
94
        }
3381 chandransh 95
        return "show";
96
    }
97
 
98
    public void setId(String id){
99
        this.id = id;
100
    }
101
 
102
    public String getId(){
103
        return id;
104
    }
105
 
106
    public String getErrorMessage(){
107
        return errorMsg;
108
    }
109
 
110
    public String getPurchaseOrderId() {
111
        return purchaseOrderId;
112
    }
113
 
114
    @Override
115
    public void setServletRequest(HttpServletRequest request) {
116
        this.request = request;
117
    }
118
 
119
    @Override
120
    public void setServletContext(ServletContext context) {
121
        this.context = context;
122
    }
123
 
124
    public String getServletContextPath() {
125
        return context.getContextPath();
126
    }
127
 
128
}