Subversion Repositories SmartDukaan

Rev

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