Subversion Repositories SmartDukaan

Rev

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