Subversion Repositories SmartDukaan

Rev

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