Subversion Repositories SmartDukaan

Rev

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