Subversion Repositories SmartDukaan

Rev

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