Subversion Repositories SmartDukaan

Rev

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