Subversion Repositories SmartDukaan

Rev

Rev 4377 | Rev 5158 | 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
 
4377 mandeep.dh 123
            TicketCategory ticketCategory = TicketCategory.findByValue((int) communicationType);
4277 anupam.sin 124
 
4377 mandeep.dh 125
            // Validating ticket category to avoid cases where someone tries to programmatically
126
            // send requests to us with invalid communication types
127
            if (ticketCategory != null) {
4378 mandeep.dh 128
                if (request.getParameter("order_id") != null) {
129
                    orderId = Integer.parseInt(request.getParameter("order_id"));
130
                }
131
 
132
                String awb = request.getParameter("awb");
133
                String product = request.getParameter("product");
134
                String subject = request.getParameter("subject");
135
                String message = request.getParameter("message");
136
 
137
                UserContextService.Client userClient = (new UserClient()).getClient();
138
 
139
                if (userClient.saveUserCommunication(userId, email,
140
                        communicationType, orderId, awb, product, subject, message)) {
141
                    log.info("User Communication saved successfully!");
142
                }
143
 
144
                /*
145
                 * We need to change status for the given order to
146
                 * CANCELLATION_REQUEST_RECIEVED
147
                 */
148
                if(communicationType == 2) {
149
                    in.shop2020.model.v1.order.TransactionService.Client transactionClient = 
150
                        (new TransactionClient()).getClient();
151
                    transactionClient.markOrderCancellationRequestReceived(orderId);
152
                }
153
 
4377 mandeep.dh 154
                // We need to have tickets for all data communication!
155
                log.info("Creating ticket!");
156
                Client crmServiceClient = new CRMClient().getClient();
157
                Ticket ticket = new Ticket();
158
                ticket.setAirwayBillNo(awb);
159
                ticket.setProductName(product);
160
                ticket.setCategory(ticketCategory);
161
                ticket.setDescription("From: " + email + "\n\nSubject: " + subject + "\n\nBody: " + message);
3272 mandeep.dh 162
 
4377 mandeep.dh 163
                if (orderId != -1) {
164
                    ticket.setOrderId(orderId);
165
                }
3272 mandeep.dh 166
 
4377 mandeep.dh 167
                ticket.setCreatorId(ADMIN_AGENT_ID);
168
                ticket.setPriority(TicketPriority.MEDIUM);
169
                ticket.setStatus(TicketStatus.OPEN);
3272 mandeep.dh 170
 
4377 mandeep.dh 171
                log.info("Creating activity!");
172
                Activity activity = new Activity();
173
                activity.setCreatorId(ADMIN_AGENT_ID);
174
                activity.setDescription("Ticket created");
175
                activity.setTicketCategory(ticket.getCategory());
176
                activity.setTicketDescription(ticket.getDescription());
177
                activity.setTicketPriority(ticket.getPriority());
178
                activity.setTicketStatus(ticket.getStatus());
179
                activity.setType(ActivityType.RECEIVED_EMAIL_FROM_CUSTOMER);
3272 mandeep.dh 180
 
4377 mandeep.dh 181
                if (isUserLoggedIn) {
182
                    activity.setCustomerId(userId);
183
                    ticket.setCustomerId(userId);
3272 mandeep.dh 184
                } else {
4377 mandeep.dh 185
                    User user = userClient.getUserByEmail(email);
186
                    if (user != null && user.getUserId() != -1) {
187
                        log.info("Found registered user for: " + email);
188
                        ticket.setCustomerId(user.getUserId());
189
                        activity.setCustomerId(user.getUserId());
190
                    } else {
191
                        log.info("Setting customer email id to: " + email);
192
                        ticket.setCustomerEmailId(email);
193
                    }
3272 mandeep.dh 194
                }
4377 mandeep.dh 195
 
196
                crmServiceClient.insertTicket(ticket, activity);                
3272 mandeep.dh 197
            }
1876 varun.gupt 198
        } catch (NumberFormatException e) {
2944 chandransh 199
            log.error("Unable to get communication type or order id: ", e);
3126 rajveer 200
        } catch (TTransportException e) {
3272 mandeep.dh 201
            log.error("Unable to initialize client: ", e);
202
        } catch (UserCommunicationException e) {
3126 rajveer 203
            log.error("Unable to get communication type or order id: ", e);
3272 mandeep.dh 204
        } catch (TException e) {
205
            log.error("Thrift exception: ", e);
206
        } catch (UserContextException e) {
207
            log.error("Thrift exception: ", e);
4277 anupam.sin 208
        } catch (TransactionServiceException e) {
209
            log.error("Unable to mark cancellation request recieved", e);
3272 mandeep.dh 210
        }
1876 varun.gupt 211
        return "success";
212
    }
213
 
214
    public String getId() {
215
        return this.id;
216
    }
217
 
218
    /**
1297 varun.gupt 219
     * @param id
220
     */
221
    public void setId(String id) {
1876 varun.gupt 222
        this.id = id;
1297 varun.gupt 223
    }
569 rajveer 224
 
1876 varun.gupt 225
    public UserCommunicationType getFormType() {
226
        return this.formType;
227
    }
1297 varun.gupt 228
 
1876 varun.gupt 229
    public String getSuccessMsg() {
230
        return UserMessage.USER_COMMUNICATION_SUCCESS;
231
    }
232
 
233
    private String getOrderIdSelector(List<Long> order_ids) {
2944 chandransh 234
        StringBuilder html = new StringBuilder();
1876 varun.gupt 235
 
236
        if (order_ids == null || order_ids.size() == 0) {
2944 chandransh 237
            html.append("<option value='-1'>No Orders Found</option>");
1876 varun.gupt 238
        } else {
2944 chandransh 239
            html.append("<option value='-1'>Select Order ID</option>");
1876 varun.gupt 240
 
241
            for (long order_id : order_ids) {
3272 mandeep.dh 242
                html.append("<option value='" + order_id + "'>" + order_id
243
                        + "</option>");
1876 varun.gupt 244
            }
245
        }
2944 chandransh 246
        return html.toString();
1876 varun.gupt 247
    }
248
 
249
    public String getProductsForOrder() {
2944 chandransh 250
        StringBuilder html = new StringBuilder();
1876 varun.gupt 251
 
252
        if (products == null || products.size() == 0) {
2944 chandransh 253
            html.append("<option value='-1'>No Products Found</option>");
1876 varun.gupt 254
        } else {
255
            for (LineItem product : products) {
2944 chandransh 256
                html.append("<option value='" + product.getId() + "'>");
1876 varun.gupt 257
 
258
                if (product.getBrand() != null)
2944 chandransh 259
                    html.append(product.getBrand() + " ");
1876 varun.gupt 260
                if (product.getModel_name() != null)
2944 chandransh 261
                    html.append(product.getModel_name() + " ");
1876 varun.gupt 262
                if (product.getModel_number() != null)
2944 chandransh 263
                    html.append(product.getModel_number());
1876 varun.gupt 264
 
2944 chandransh 265
                html.append("</option>");
1876 varun.gupt 266
            }
267
        }
2944 chandransh 268
        return html.toString();
1876 varun.gupt 269
    }
270
 
271
    public String getIdsOfAllOrders() {
272
        List<Long> order_ids = new ArrayList<Long>();
273
 
274
        for (Order order : this.orders) {
275
            order_ids.add(order.getId());
276
        }
277
        return getOrderIdSelector(order_ids);
278
    }
279
 
280
    public String getIdsOfReturnableOrders() {
281
        return getOrderIdSelector(this.order_ids);
282
    }
283
 
284
    public String getIdsOfCancellableOrders() {
285
        return getOrderIdSelector(this.order_ids);
286
    }
569 rajveer 287
}