Subversion Repositories SmartDukaan

Rev

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