Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
569 rajveer 1
package in.shop2020.serving.controllers;
2
 
1297 varun.gupt 3
import java.io.IOException;
1380 varun.gupt 4
import java.lang.NumberFormatException;
569 rajveer 5
import java.util.ArrayList;
1297 varun.gupt 6
import java.util.Date;
569 rajveer 7
import java.util.List;
8
 
1297 varun.gupt 9
import in.shop2020.model.v1.order.LineItem;
10
import in.shop2020.model.v1.order.Order;
1170 varun.gupt 11
import in.shop2020.model.v1.user.UserContextException;
12
import in.shop2020.model.v1.user.UserContextService;
1876 varun.gupt 13
import in.shop2020.model.v1.user.UserCommunicationType;
569 rajveer 14
import in.shop2020.serving.controllers.BaseController;
1297 varun.gupt 15
import in.shop2020.thrift.clients.TransactionServiceClient;
1170 varun.gupt 16
import in.shop2020.thrift.clients.UserContextServiceClient;
17
import in.shop2020.serving.utils.UserMessage;
569 rajveer 18
 
832 rajveer 19
import org.apache.log4j.Logger;
569 rajveer 20
 
1309 varun.gupt 21
/**
22
 * @author Varun Gupta
23
 */
24
 
2145 chandransh 25
@SuppressWarnings("serial")
1876 varun.gupt 26
public class ContactUsController extends BaseController {
1297 varun.gupt 27
 
1876 varun.gupt 28
    private String id;
2145 chandransh 29
 
1876 varun.gupt 30
    private static Logger log = Logger.getLogger(Class.class);
1170 varun.gupt 31
 
1876 varun.gupt 32
    private UserCommunicationType formType = null;
1380 varun.gupt 33
 
1876 varun.gupt 34
    List<Order> orders = null;
35
    List<LineItem> products = null;
36
    List<Long> order_ids = null;
1170 varun.gupt 37
 
1876 varun.gupt 38
    public ContactUsController() {
39
        super();
40
    }
1170 varun.gupt 41
 
1876 varun.gupt 42
    // GET /Show Form
43
    public String index() {
44
        try {
45
            TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
46
            in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();
47
            orders = orderClient.getOrdersForCustomer(userinfo.getUserId(), 0, (new Date()).getTime(), null);
48
        } catch (Exception e) {
49
            e.printStackTrace();
50
        }
1170 varun.gupt 51
 
1876 varun.gupt 52
        if (request.getParameter("type") != null) {
53
            String requestedFormType = request.getParameter("type").toString().trim();
54
 
55
            if (requestedFormType.equals("return")) {
56
                this.formType = UserCommunicationType.RETURN_FORM;
57
            } else if (requestedFormType.equals("cancel")) {
58
                this.formType = UserCommunicationType.ORDER_CANCELLATION;
59
            } else if (requestedFormType.equals("delivery")) {
60
                this.formType = UserCommunicationType.DELIVERY_PROBLEM;
61
            }
62
        }
63
        return "index";
64
    }
65
 
66
    public String show() throws SecurityException, IOException {
67
        try {
68
            TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
69
            in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();
70
 
71
            if (id.equals("to_return")) {
72
                order_ids = orderClient.getReturnableOrdersForCustomer(userinfo.getUserId(), 0);
73
            } else if (id.equals("to_cancel")) {
74
                order_ids = orderClient.getCancellableOrdersForCustomer(userinfo.getUserId(), 0);
75
            } else if (id.equals("all_orders")) {
76
                orders = orderClient.getOrdersForCustomer(userinfo.getUserId(), 0, (new Date()).getTime(), null);
77
            } else {
78
                long orderId = Integer.parseInt(id);
79
                products = orderClient.getLineItemsForOrder(orderId);
80
            }
81
        } catch (NumberFormatException e) {
82
            e.printStackTrace();
83
        } catch (Exception e) {
84
            e.printStackTrace();
85
        }
86
        return "ajax";
87
    }
88
 
89
    // POST /Save Communication
90
    public String create() {
91
        long communicationType = -1;
92
        long orderId = -1;
93
 
94
        try {
95
            boolean u = userinfo.isLoggedIn();
96
            long userId = u ? userinfo.getUserId() : 0;
97
            String email = request.getParameter("email");
98
            communicationType = Integer.parseInt(request.getParameter("communication_type"));
99
 
100
            if (request.getParameter("order_id") != null) {
101
                orderId = Integer.parseInt(request.getParameter("order_id"));
102
            }
103
            String awb = request.getParameter("awb");
104
            String product = request.getParameter("product");
105
            String subject = request.getParameter("subject");
106
            String message = request.getParameter("message");
107
 
108
            UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
109
 
110
            if (userClient.saveUserCommunication(userId, email, communicationType, orderId, awb, product, subject,
111
                    message)) {
112
                System.out.println("User Communication saved successfully!");
113
            }
114
 
115
        } catch (NumberFormatException e) {
116
            e.printStackTrace();
117
        } catch (UserContextException e) {
118
            e.printStackTrace();
119
        } catch (Exception e) {
120
            e.printStackTrace();
121
        } finally {
122
 
123
        }
124
        return "success";
125
    }
126
 
127
    public String getId() {
128
        return this.id;
129
    }
130
 
131
    /**
1297 varun.gupt 132
     * @param id
133
     */
134
    public void setId(String id) {
1876 varun.gupt 135
        this.id = id;
1297 varun.gupt 136
    }
569 rajveer 137
 
1876 varun.gupt 138
    public UserCommunicationType getFormType() {
139
        return this.formType;
140
    }
1297 varun.gupt 141
 
1876 varun.gupt 142
    public String getSuccessMsg() {
143
        return UserMessage.USER_COMMUNICATION_SUCCESS;
144
    }
145
 
146
    private String getOrderIdSelector(List<Long> order_ids) {
147
        String html = "";
148
 
149
        if (order_ids == null || order_ids.size() == 0) {
150
            html += "<option value='-1'>No Orders Found</option>";
151
        } else {
152
            html += "<option value='-1'>Select Order ID</option>";
153
 
154
            for (long order_id : order_ids) {
155
                html += "<option value='" + order_id + "'>" + order_id + "</option>";
156
            }
157
        }
158
        return html;
159
    }
160
 
161
    public String getProductsForOrder() {
162
        String html = "";
163
 
164
        if (products == null || products.size() == 0) {
165
            html += "<option value='-1'>No Products Found</option>";
166
        } else {
167
            for (LineItem product : products) {
168
                html += "<option value='" + product.getId() + "'>";
169
 
170
                if (product.getBrand() != null)
171
                    html += product.getBrand() + " ";
172
                if (product.getModel_name() != null)
173
                    html += product.getModel_name() + " ";
174
                if (product.getModel_number() != null)
175
                    html += product.getModel_number();
176
 
177
                html += "</option>";
178
            }
179
        }
180
        return html;
181
    }
182
 
183
    public String getIdsOfAllOrders() {
184
        List<Long> order_ids = new ArrayList<Long>();
185
 
186
        for (Order order : this.orders) {
187
            order_ids.add(order.getId());
188
        }
189
        return getOrderIdSelector(order_ids);
190
    }
191
 
192
    public String getIdsOfReturnableOrders() {
193
        return getOrderIdSelector(this.order_ids);
194
    }
195
 
196
    public String getIdsOfCancellableOrders() {
197
        return getOrderIdSelector(this.order_ids);
198
    }
569 rajveer 199
}