Subversion Repositories SmartDukaan

Rev

Rev 11943 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
569 rajveer 1
package in.shop2020.serving.controllers;
2
 
3272 mandeep.dh 3
import in.shop2020.crm.TicketCategory;
1297 varun.gupt 4
import in.shop2020.model.v1.order.LineItem;
5
import in.shop2020.model.v1.order.Order;
4277 anupam.sin 6
import in.shop2020.model.v1.order.TransactionServiceException;
3126 rajveer 7
import in.shop2020.model.v1.user.UserCommunicationException;
3272 mandeep.dh 8
import in.shop2020.model.v1.user.UserCommunicationType;
1170 varun.gupt 9
import in.shop2020.model.v1.user.UserContextService;
3272 mandeep.dh 10
import in.shop2020.serving.utils.UserMessage;
3126 rajveer 11
import in.shop2020.thrift.clients.TransactionClient;
12
import in.shop2020.thrift.clients.UserClient;
569 rajveer 13
 
3272 mandeep.dh 14
import java.io.IOException;
15
import java.util.ArrayList;
16
import java.util.Date;
17
import java.util.List;
18
 
12859 kshitij.so 19
import nl.captcha.Captcha;
20
 
6176 amit.gupta 21
import org.apache.commons.lang.StringUtils;
832 rajveer 22
import org.apache.log4j.Logger;
3126 rajveer 23
import org.apache.thrift.TException;
24
import org.apache.thrift.transport.TTransportException;
569 rajveer 25
 
1309 varun.gupt 26
/**
27
 * @author Varun Gupta
28
 */
29
 
2145 chandransh 30
@SuppressWarnings("serial")
1876 varun.gupt 31
public class ContactUsController extends BaseController {
1297 varun.gupt 32
 
3272 mandeep.dh 33
    private String                id;
1170 varun.gupt 34
 
3272 mandeep.dh 35
    private static Logger         log        = Logger.getLogger(Class.class);
1380 varun.gupt 36
 
3272 mandeep.dh 37
    private UserCommunicationType formType   = null;
1170 varun.gupt 38
 
3272 mandeep.dh 39
    List<Order>                   orders     = null;
40
    List<LineItem>                products   = null;
41
    List<Long>                    order_ids  = null;
12859 kshitij.so 42
 
43
    private String failureMsg = "";
3272 mandeep.dh 44
 
1876 varun.gupt 45
    public ContactUsController() {
46
        super();
47
    }
1170 varun.gupt 48
 
1876 varun.gupt 49
    // GET /Show Form
50
    public String index() {
51
        try {
3126 rajveer 52
            TransactionClient transactionServiceClient = new TransactionClient();
3272 mandeep.dh 53
            in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient
54
                    .getClient();
55
            orders = orderClient.getOrdersForCustomer(userinfo.getUserId(), 0,
56
                    (new Date()).getTime(), null);
1876 varun.gupt 57
        } catch (Exception e) {
2944 chandransh 58
            log.error("Unable to get the orders for the user becauase of: ", e);
1876 varun.gupt 59
        }
1170 varun.gupt 60
 
1876 varun.gupt 61
        if (request.getParameter("type") != null) {
3272 mandeep.dh 62
            String requestedFormType = request.getParameter("type").toString()
63
                    .trim();
2148 chandransh 64
            log.debug("Contact us requested for: " + requestedFormType);
3272 mandeep.dh 65
 
1876 varun.gupt 66
            if (requestedFormType.equals("return")) {
67
                this.formType = UserCommunicationType.RETURN_FORM;
68
            } else if (requestedFormType.equals("cancel")) {
69
                this.formType = UserCommunicationType.ORDER_CANCELLATION;
70
            } else if (requestedFormType.equals("delivery")) {
71
                this.formType = UserCommunicationType.DELIVERY_PROBLEM;
72
            }
73
        }
74
        return "index";
75
    }
76
 
77
    public String show() throws SecurityException, IOException {
78
        try {
3126 rajveer 79
            TransactionClient transactionServiceClient = new TransactionClient();
3272 mandeep.dh 80
            in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient
81
                    .getClient();
1876 varun.gupt 82
 
83
            if (id.equals("to_return")) {
3272 mandeep.dh 84
                order_ids = orderClient.getReturnableOrdersForCustomer(
85
                        userinfo.getUserId(), 0);
1876 varun.gupt 86
            } else if (id.equals("to_cancel")) {
3272 mandeep.dh 87
                order_ids = orderClient.getCancellableOrdersForCustomer(
88
                        userinfo.getUserId(), 0);
1876 varun.gupt 89
            } else if (id.equals("all_orders")) {
3272 mandeep.dh 90
                orders = orderClient.getOrdersForCustomer(userinfo.getUserId(),
91
                        0, (new Date()).getTime(), null);
1876 varun.gupt 92
            } else {
93
                long orderId = Integer.parseInt(id);
94
                products = orderClient.getLineItemsForOrder(orderId);
95
            }
96
        } catch (NumberFormatException e) {
2944 chandransh 97
            log.error("Unable to get order id", e);
1876 varun.gupt 98
        } catch (Exception e) {
2944 chandransh 99
            log.error("Unable to get user order details: ", e);
1876 varun.gupt 100
        }
101
        return "ajax";
102
    }
103
 
104
    // POST /Save Communication
105
    public String create() {
12859 kshitij.so 106
        if(!verifyCaptcha()){
107
            setFailureMsg(UserMessage.INVALID_CAPTCHA);
108
            return "success";
109
        }
3952 mandeep.dh 110
        // Default communication type OTHER
111
        long communicationType = 8;
1876 varun.gupt 112
        long orderId = -1;
113
 
114
        try {
3272 mandeep.dh 115
            boolean isUserLoggedIn = userinfo.isLoggedIn();
116
            long userId = isUserLoggedIn ? userinfo.getUserId() : 0;
1876 varun.gupt 117
            String email = request.getParameter("email");
3272 mandeep.dh 118
            communicationType = Integer.parseInt(request
119
                    .getParameter("communication_type"));
1876 varun.gupt 120
 
4377 mandeep.dh 121
            TicketCategory ticketCategory = TicketCategory.findByValue((int) communicationType);
4277 anupam.sin 122
 
4377 mandeep.dh 123
            // Validating ticket category to avoid cases where someone tries to programmatically
124
            // send requests to us with invalid communication types
125
            if (ticketCategory != null) {
4378 mandeep.dh 126
                if (request.getParameter("order_id") != null) {
127
                    orderId = Integer.parseInt(request.getParameter("order_id"));
128
                }
6176 amit.gupta 129
                if (StringUtils.isNotEmpty(request.getParameter("rechargeOrderNumber"))) {
130
                	//Valid recharge formats are:
131
                	//MOBR9999
132
                	//99999
133
                	String []strOrder = request.getParameter("rechargeOrderNumber").split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
134
                	if(strOrder.length==2) {
135
                		orderId = Integer.parseInt(strOrder[1]);
136
                	} else {
137
                		orderId = Integer.parseInt(strOrder[0]);
138
                	}
139
                }
140
 
4378 mandeep.dh 141
 
142
                String awb = request.getParameter("awb");
6176 amit.gupta 143
                if(StringUtils.isEmpty(awb)){
144
                	awb = request.getParameter("deviceNumber");
145
                }
4378 mandeep.dh 146
                String product = request.getParameter("product");
147
                String subject = request.getParameter("subject");
148
                String message = request.getParameter("message");
149
 
150
                UserContextService.Client userClient = (new UserClient()).getClient();
11943 kshitij.so 151
                if (communicationType == 21){
152
                    product = subject;
153
                    subject = "Bulk Order enquiry requested for " + subject;
154
                    message = message +"Product Name : "+product+"\n";
155
                    message = message+"Contact Number : "+request.getParameter("contactNumber")+"\n";
156
                    message = message+"Quantity : "+request.getParameter("quantity")+"\n";
157
                    message = message+"User Email: "+email;
158
                    awb = request.getParameter("contactNumber");
159
                }
4378 mandeep.dh 160
 
161
                if (userClient.saveUserCommunication(userId, email,
162
                        communicationType, orderId, awb, product, subject, message)) {
163
                    log.info("User Communication saved successfully!");
164
                }
165
 
166
                /*
167
                 * We need to change status for the given order to
168
                 * CANCELLATION_REQUEST_RECIEVED
169
                 */
170
                if(communicationType == 2) {
171
                    in.shop2020.model.v1.order.TransactionService.Client transactionClient = 
172
                        (new TransactionClient()).getClient();
173
                    transactionClient.markOrderCancellationRequestReceived(orderId);
174
                }
3272 mandeep.dh 175
            }
1876 varun.gupt 176
        } catch (NumberFormatException e) {
2944 chandransh 177
            log.error("Unable to get communication type or order id: ", e);
3126 rajveer 178
        } catch (TTransportException e) {
3272 mandeep.dh 179
            log.error("Unable to initialize client: ", e);
180
        } catch (UserCommunicationException e) {
3126 rajveer 181
            log.error("Unable to get communication type or order id: ", e);
3272 mandeep.dh 182
        } catch (TException e) {
183
            log.error("Thrift exception: ", e);
4277 anupam.sin 184
        } catch (TransactionServiceException e) {
185
            log.error("Unable to mark cancellation request recieved", e);
3272 mandeep.dh 186
        }
1876 varun.gupt 187
        return "success";
188
    }
189
 
190
    public String getId() {
191
        return this.id;
192
    }
193
 
194
    /**
1297 varun.gupt 195
     * @param id
196
     */
197
    public void setId(String id) {
1876 varun.gupt 198
        this.id = id;
1297 varun.gupt 199
    }
569 rajveer 200
 
1876 varun.gupt 201
    public UserCommunicationType getFormType() {
202
        return this.formType;
203
    }
1297 varun.gupt 204
 
1876 varun.gupt 205
    public String getSuccessMsg() {
206
        return UserMessage.USER_COMMUNICATION_SUCCESS;
207
    }
12859 kshitij.so 208
 
209
    public void setFailureMsg(String failureMsg){
210
        this.failureMsg = failureMsg;
211
    }
212
 
213
    public String getFailureMsg(){
214
        return failureMsg;
215
    }
1876 varun.gupt 216
 
217
    private String getOrderIdSelector(List<Long> order_ids) {
2944 chandransh 218
        StringBuilder html = new StringBuilder();
1876 varun.gupt 219
 
220
        if (order_ids == null || order_ids.size() == 0) {
2944 chandransh 221
            html.append("<option value='-1'>No Orders Found</option>");
1876 varun.gupt 222
        } else {
2944 chandransh 223
            html.append("<option value='-1'>Select Order ID</option>");
1876 varun.gupt 224
 
225
            for (long order_id : order_ids) {
3272 mandeep.dh 226
                html.append("<option value='" + order_id + "'>" + order_id
227
                        + "</option>");
1876 varun.gupt 228
            }
229
        }
2944 chandransh 230
        return html.toString();
1876 varun.gupt 231
    }
232
 
233
    public String getProductsForOrder() {
2944 chandransh 234
        StringBuilder html = new StringBuilder();
1876 varun.gupt 235
 
236
        if (products == null || products.size() == 0) {
2944 chandransh 237
            html.append("<option value='-1'>No Products Found</option>");
1876 varun.gupt 238
        } else {
239
            for (LineItem product : products) {
2944 chandransh 240
                html.append("<option value='" + product.getId() + "'>");
1876 varun.gupt 241
 
242
                if (product.getBrand() != null)
2944 chandransh 243
                    html.append(product.getBrand() + " ");
1876 varun.gupt 244
                if (product.getModel_name() != null)
2944 chandransh 245
                    html.append(product.getModel_name() + " ");
1876 varun.gupt 246
                if (product.getModel_number() != null)
2944 chandransh 247
                    html.append(product.getModel_number());
1876 varun.gupt 248
 
2944 chandransh 249
                html.append("</option>");
1876 varun.gupt 250
            }
251
        }
2944 chandransh 252
        return html.toString();
1876 varun.gupt 253
    }
254
 
255
    public String getIdsOfAllOrders() {
256
        List<Long> order_ids = new ArrayList<Long>();
257
 
258
        for (Order order : this.orders) {
259
            order_ids.add(order.getId());
260
        }
261
        return getOrderIdSelector(order_ids);
262
    }
263
 
264
    public String getIdsOfReturnableOrders() {
265
        return getOrderIdSelector(this.order_ids);
266
    }
267
 
268
    public String getIdsOfCancellableOrders() {
269
        return getOrderIdSelector(this.order_ids);
270
    }
12859 kshitij.so 271
 
272
    private boolean verifyCaptcha() {
273
        String cookieCaptchaAnswer = getCookie(Captcha.NAME, true, "saholic");
274
        String captchaReceived = (String) request.getParameter("captcha_response_field");
275
 
276
        if (captchaReceived.equalsIgnoreCase(cookieCaptchaAnswer)) {
277
            return true;
278
        } 
279
        else {
280
            return false;
281
        }
282
    }
569 rajveer 283
}