Subversion Repositories SmartDukaan

Rev

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