Subversion Repositories SmartDukaan

Rev

Rev 2944 | Rev 3272 | 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
 
1297 varun.gupt 3
import java.io.IOException;
1380 varun.gupt 4
import java.lang.NumberFormatException;
569 rajveer 5
import java.util.ArrayList;
1297 varun.gupt 6
import java.util.Date;
569 rajveer 7
import java.util.List;
8
 
1297 varun.gupt 9
import in.shop2020.model.v1.order.LineItem;
10
import in.shop2020.model.v1.order.Order;
3126 rajveer 11
import in.shop2020.model.v1.user.UserCommunicationException;
1170 varun.gupt 12
import in.shop2020.model.v1.user.UserContextService;
1876 varun.gupt 13
import in.shop2020.model.v1.user.UserCommunicationType;
569 rajveer 14
import in.shop2020.serving.controllers.BaseController;
3126 rajveer 15
import in.shop2020.thrift.clients.TransactionClient;
16
import in.shop2020.thrift.clients.UserClient;
1170 varun.gupt 17
import in.shop2020.serving.utils.UserMessage;
569 rajveer 18
 
832 rajveer 19
import org.apache.log4j.Logger;
3126 rajveer 20
import org.apache.thrift.TException;
21
import org.apache.thrift.transport.TTransportException;
569 rajveer 22
 
1309 varun.gupt 23
/**
24
 * @author Varun Gupta
25
 */
26
 
2145 chandransh 27
@SuppressWarnings("serial")
1876 varun.gupt 28
public class ContactUsController extends BaseController {
1297 varun.gupt 29
 
1876 varun.gupt 30
    private String id;
2145 chandransh 31
 
1876 varun.gupt 32
    private static Logger log = Logger.getLogger(Class.class);
1170 varun.gupt 33
 
1876 varun.gupt 34
    private UserCommunicationType formType = null;
1380 varun.gupt 35
 
1876 varun.gupt 36
    List<Order> orders = null;
37
    List<LineItem> products = null;
38
    List<Long> order_ids = null;
1170 varun.gupt 39
 
1876 varun.gupt 40
    public ContactUsController() {
41
        super();
42
    }
1170 varun.gupt 43
 
1876 varun.gupt 44
    // GET /Show Form
45
    public String index() {
46
        try {
3126 rajveer 47
            TransactionClient transactionServiceClient = new TransactionClient();
1876 varun.gupt 48
            in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();
49
            orders = orderClient.getOrdersForCustomer(userinfo.getUserId(), 0, (new Date()).getTime(), null);
50
        } catch (Exception e) {
2944 chandransh 51
            log.error("Unable to get the orders for the user becauase of: ", e);
1876 varun.gupt 52
        }
1170 varun.gupt 53
 
1876 varun.gupt 54
        if (request.getParameter("type") != null) {
55
            String requestedFormType = request.getParameter("type").toString().trim();
2148 chandransh 56
            log.debug("Contact us requested for: " + requestedFormType);
57
 
1876 varun.gupt 58
            if (requestedFormType.equals("return")) {
59
                this.formType = UserCommunicationType.RETURN_FORM;
60
            } else if (requestedFormType.equals("cancel")) {
61
                this.formType = UserCommunicationType.ORDER_CANCELLATION;
62
            } else if (requestedFormType.equals("delivery")) {
63
                this.formType = UserCommunicationType.DELIVERY_PROBLEM;
64
            }
65
        }
66
        return "index";
67
    }
68
 
69
    public String show() throws SecurityException, IOException {
70
        try {
3126 rajveer 71
            TransactionClient transactionServiceClient = new TransactionClient();
1876 varun.gupt 72
            in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();
73
 
74
            if (id.equals("to_return")) {
75
                order_ids = orderClient.getReturnableOrdersForCustomer(userinfo.getUserId(), 0);
76
            } else if (id.equals("to_cancel")) {
77
                order_ids = orderClient.getCancellableOrdersForCustomer(userinfo.getUserId(), 0);
78
            } else if (id.equals("all_orders")) {
79
                orders = orderClient.getOrdersForCustomer(userinfo.getUserId(), 0, (new Date()).getTime(), null);
80
            } else {
81
                long orderId = Integer.parseInt(id);
82
                products = orderClient.getLineItemsForOrder(orderId);
83
            }
84
        } catch (NumberFormatException e) {
2944 chandransh 85
            log.error("Unable to get order id", e);
1876 varun.gupt 86
        } catch (Exception e) {
2944 chandransh 87
            log.error("Unable to get user order details: ", e);
1876 varun.gupt 88
        }
89
        return "ajax";
90
    }
91
 
92
    // POST /Save Communication
93
    public String create() {
94
        long communicationType = -1;
95
        long orderId = -1;
96
 
97
        try {
98
            boolean u = userinfo.isLoggedIn();
99
            long userId = u ? userinfo.getUserId() : 0;
100
            String email = request.getParameter("email");
101
            communicationType = Integer.parseInt(request.getParameter("communication_type"));
102
 
103
            if (request.getParameter("order_id") != null) {
104
                orderId = Integer.parseInt(request.getParameter("order_id"));
105
            }
106
            String awb = request.getParameter("awb");
107
            String product = request.getParameter("product");
108
            String subject = request.getParameter("subject");
109
            String message = request.getParameter("message");
110
 
3126 rajveer 111
            UserContextService.Client userClient = (new UserClient()).getClient();
1876 varun.gupt 112
 
113
            if (userClient.saveUserCommunication(userId, email, communicationType, orderId, awb, product, subject,
114
                    message)) {
2944 chandransh 115
                log.info("User Communication saved successfully!");
1876 varun.gupt 116
            }
117
 
118
        } catch (NumberFormatException e) {
2944 chandransh 119
            log.error("Unable to get communication type or order id: ", e);
3126 rajveer 120
        } catch (TTransportException e) {
121
        	log.error("Unable to initialize client: ", e);
122
		} catch (UserCommunicationException e) {
123
            log.error("Unable to get communication type or order id: ", e);
124
		} catch (TException e) {
125
			log.error("Thrift exception: ", e);
126
		}
1876 varun.gupt 127
        return "success";
128
    }
129
 
130
    public String getId() {
131
        return this.id;
132
    }
133
 
134
    /**
1297 varun.gupt 135
     * @param id
136
     */
137
    public void setId(String id) {
1876 varun.gupt 138
        this.id = id;
1297 varun.gupt 139
    }
569 rajveer 140
 
1876 varun.gupt 141
    public UserCommunicationType getFormType() {
142
        return this.formType;
143
    }
1297 varun.gupt 144
 
1876 varun.gupt 145
    public String getSuccessMsg() {
146
        return UserMessage.USER_COMMUNICATION_SUCCESS;
147
    }
148
 
149
    private String getOrderIdSelector(List<Long> order_ids) {
2944 chandransh 150
        StringBuilder html = new StringBuilder();
1876 varun.gupt 151
 
152
        if (order_ids == null || order_ids.size() == 0) {
2944 chandransh 153
            html.append("<option value='-1'>No Orders Found</option>");
1876 varun.gupt 154
        } else {
2944 chandransh 155
            html.append("<option value='-1'>Select Order ID</option>");
1876 varun.gupt 156
 
157
            for (long order_id : order_ids) {
2944 chandransh 158
                html.append("<option value='" + order_id + "'>" + order_id + "</option>");
1876 varun.gupt 159
            }
160
        }
2944 chandransh 161
        return html.toString();
1876 varun.gupt 162
    }
163
 
164
    public String getProductsForOrder() {
2944 chandransh 165
        StringBuilder html = new StringBuilder();
1876 varun.gupt 166
 
167
        if (products == null || products.size() == 0) {
2944 chandransh 168
            html.append("<option value='-1'>No Products Found</option>");
1876 varun.gupt 169
        } else {
170
            for (LineItem product : products) {
2944 chandransh 171
                html.append("<option value='" + product.getId() + "'>");
1876 varun.gupt 172
 
173
                if (product.getBrand() != null)
2944 chandransh 174
                    html.append(product.getBrand() + " ");
1876 varun.gupt 175
                if (product.getModel_name() != null)
2944 chandransh 176
                    html.append(product.getModel_name() + " ");
1876 varun.gupt 177
                if (product.getModel_number() != null)
2944 chandransh 178
                    html.append(product.getModel_number());
1876 varun.gupt 179
 
2944 chandransh 180
                html.append("</option>");
1876 varun.gupt 181
            }
182
        }
2944 chandransh 183
        return html.toString();
1876 varun.gupt 184
    }
185
 
186
    public String getIdsOfAllOrders() {
187
        List<Long> order_ids = new ArrayList<Long>();
188
 
189
        for (Order order : this.orders) {
190
            order_ids.add(order.getId());
191
        }
192
        return getOrderIdSelector(order_ids);
193
    }
194
 
195
    public String getIdsOfReturnableOrders() {
196
        return getOrderIdSelector(this.order_ids);
197
    }
198
 
199
    public String getIdsOfCancellableOrders() {
200
        return getOrderIdSelector(this.order_ids);
201
    }
569 rajveer 202
}