Subversion Repositories SmartDukaan

Rev

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