Subversion Repositories SmartDukaan

Rev

Rev 3397 | Rev 3499 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2674 vikas 1
package in.shop2020.serving.controllers;
2
 
3090 mandeep.dh 3
import in.shop2020.crm.Activity;
3397 mandeep.dh 4
import in.shop2020.crm.Agent;
3390 mandeep.dh 5
import in.shop2020.crm.SearchFilter;
3090 mandeep.dh 6
import in.shop2020.crm.Ticket;
7
import in.shop2020.crm.TicketStatus;
2724 vikas 8
import in.shop2020.model.v1.order.Order;
9
import in.shop2020.model.v1.order.OrderStatus;
3090 mandeep.dh 10
import in.shop2020.model.v1.order.TransactionServiceException;
2724 vikas 11
import in.shop2020.model.v1.user.Cart;
12
import in.shop2020.model.v1.user.Line;
13
import in.shop2020.model.v1.user.User;
3090 mandeep.dh 14
import in.shop2020.model.v1.user.UserCommunication;
15
import in.shop2020.model.v1.user.UserCommunicationException;
16
import in.shop2020.model.v1.user.UserContextException;
17
import in.shop2020.model.v1.user.UserState;
3390 mandeep.dh 18
import in.shop2020.serving.auth.CRMAuthorizingRealm;
19
import in.shop2020.thrift.clients.CRMClient;
20
import in.shop2020.thrift.clients.TransactionClient;
21
import in.shop2020.thrift.clients.UserClient;
3269 mandeep.dh 22
import in.shop2020.util.CRMConstants;
2674 vikas 23
 
3397 mandeep.dh 24
import java.util.Calendar;
3390 mandeep.dh 25
import java.util.Collections;
2724 vikas 26
import java.util.Date;
27
import java.util.List;
28
 
2674 vikas 29
import org.apache.log4j.Logger;
30
import org.apache.struts2.convention.annotation.Action;
3090 mandeep.dh 31
import org.apache.thrift.TException;
2674 vikas 32
 
33
/**
34
 * @author vikas
3422 mandeep.dh 35
 * 
2674 vikas 36
 */
37
@SuppressWarnings("serial")
38
public class HomeController extends BaseController {
3422 mandeep.dh 39
    private static final Logger log        = Logger.getLogger(HomeController.class);
3090 mandeep.dh 40
 
3422 mandeep.dh 41
    private String              email;
42
    private String              orderId;
43
    private String              mobileNumber;
44
    private User                user;
45
    private long                orderCount;
46
    private long                completedOrderCount;
47
    private long                openOrderCount;
48
    private long                failedOrderCount;
49
    private long                userCommunicationCount;
50
    private long                ticketCount;
51
    private long                openTicketCount;
52
    private long                closedTicketCount;
53
    private String              ticketId;
54
    private String              lastLogin;
55
    private double              cartItems;
56
    private String              couponCode = "";
57
    private long                agentOpenTicketCount;
58
    private long                unassignedTicketCount;
59
    private long                activityCount;
60
    private long                customerActivityCount;
2674 vikas 61
 
62
    @Action("/")
2724 vikas 63
    public String index() throws Exception {
3137 mandeep.dh 64
        String returnValue = INPUT;
65
 
3090 mandeep.dh 66
        try {
3106 mandeep.dh 67
            if (email == null) {
3390 mandeep.dh 68
                loadTicketCounts();
69
                loadCustomerActivities();
3137 mandeep.dh 70
                return returnValue;
3106 mandeep.dh 71
            }
72
 
3390 mandeep.dh 73
            userContextServiceClient = new UserClient().getClient();
3096 mandeep.dh 74
            if (email != null && !email.isEmpty()) {
75
                user = userContextServiceClient.getUserByEmail(email);
3422 mandeep.dh 76
            } else if (mobileNumber != null && !mobileNumber.isEmpty()) {
77
                user = userContextServiceClient.getUserByMobileNumber(Long
78
                        .parseLong(mobileNumber));
79
            } else if (orderId != null && !orderId.isEmpty()) {
3390 mandeep.dh 80
                transactionServiceClient = new TransactionClient().getClient();
3422 mandeep.dh 81
                Order order = transactionServiceClient.getOrder(Long
82
                        .parseLong(orderId));
83
                user = userContextServiceClient.getUserById(order
84
                        .getCustomer_id());
85
            } else if (ticketId != null && !ticketId.isEmpty()) {
3390 mandeep.dh 86
                SearchFilter searchFilter = new SearchFilter();
87
                searchFilter.setTicketId(Long.parseLong(ticketId));
3422 mandeep.dh 88
                crmServiceClient = new CRMClient().getClient();
89
                List<Ticket> tickets = crmServiceClient
90
                        .getTickets(searchFilter);
3390 mandeep.dh 91
                if (!tickets.isEmpty()) {
92
                    Ticket ticket = tickets.get(0);
93
                    if (ticket.isSetCustomerId()) {
94
                        user = userContextServiceClient.getUserById(ticket.getCustomerId());
95
                    }
96
                }
3422 mandeep.dh 97
                else {
98
                    // resetting in case on invalid ticket Id
99
                    ticketId = null;
100
                }
3096 mandeep.dh 101
            }
3090 mandeep.dh 102
 
103
            if (user == null || user.getUserId() <= 0) {
3422 mandeep.dh 104
                if (ticketId == null || ticketId.isEmpty()) {
105
                    addActionError("Invalid input");
106
                }
107
            } else {
3390 mandeep.dh 108
                loadUserCommunicationDetails();
109
                loadOrderDetails();
110
                loadCartDetails();
111
                loadTicketDetails();
112
                loadActivityDetails();
3137 mandeep.dh 113
                returnValue = INDEX;
114
            }
3422 mandeep.dh 115
        } catch (Exception e) {
3137 mandeep.dh 116
            addActionError("Invalid input");
3090 mandeep.dh 117
            log.error("Error occurred", e);
2724 vikas 118
        }
3137 mandeep.dh 119
 
120
        if (INPUT.equals(returnValue)) {
3390 mandeep.dh 121
            loadTicketCounts();
3422 mandeep.dh 122
            loadCustomerActivities();
3137 mandeep.dh 123
        }
124
 
125
        return returnValue;
3090 mandeep.dh 126
    }
127
 
3390 mandeep.dh 128
    private void loadCustomerActivities() throws TException {
129
        SearchFilter searchFilter = new SearchFilter();
3422 mandeep.dh 130
        searchFilter.setActivityCreatorIds(Collections
131
                .singletonList(CRMConstants.ADMIN_AGENT_ID));
3390 mandeep.dh 132
        searchFilter.setIsActivityRead(false);
133
 
3422 mandeep.dh 134
        crmServiceClient = new CRMClient().getClient();
135
        List<Activity> activities = crmServiceClient
136
                .getActivities(searchFilter);
3339 mandeep.dh 137
        if (activities != null) {
138
            customerActivityCount = activities.size();
139
        }
140
    }
141
 
3390 mandeep.dh 142
    private void loadActivityDetails() throws TException {
143
        SearchFilter searchFilter = new SearchFilter();
144
        searchFilter.setCustomerId(user.getUserId());
145
 
3422 mandeep.dh 146
        crmServiceClient = new CRMClient().getClient();
147
        List<Activity> activities = crmServiceClient
148
                .getActivities(searchFilter);
3106 mandeep.dh 149
        if (activities != null) {
150
            activityCount = activities.size();
151
        }
152
    }
153
 
3390 mandeep.dh 154
    private void loadTicketCounts() throws TException {
155
        SearchFilter searchFilter = new SearchFilter();
3422 mandeep.dh 156
        searchFilter.setTicketAssigneeIds(Collections
157
                .singletonList(CRMAuthorizingRealm
158
                        .getAgent(currentAgentEmailId).getId()));
3390 mandeep.dh 159
 
3422 mandeep.dh 160
        crmServiceClient = new CRMClient().getClient();
3390 mandeep.dh 161
        List<Ticket> tickets = crmServiceClient.getTickets(searchFilter);
3137 mandeep.dh 162
        for (Ticket ticket : tickets) {
163
            if (TicketStatus.OPEN.equals(ticket.getStatus())) {
164
                agentOpenTicketCount++;
165
            }
166
        }
3390 mandeep.dh 167
 
3422 mandeep.dh 168
        List<Ticket> unassignedTickets = crmServiceClient
169
                .getUnassignedTickets();
3137 mandeep.dh 170
        unassignedTicketCount = unassignedTickets.size();
3106 mandeep.dh 171
    }
172
 
3390 mandeep.dh 173
    private void loadUserCommunicationDetails()
3090 mandeep.dh 174
            throws UserCommunicationException, TException {
3390 mandeep.dh 175
        userContextServiceClient = new UserClient().getClient();
3422 mandeep.dh 176
        List<UserCommunication> userCommunication = userContextServiceClient
177
                .getUserCommunicationByUser(user.getUserId());
3090 mandeep.dh 178
        userCommunicationCount = userCommunication.size();
179
    }
180
 
3422 mandeep.dh 181
    private void loadOrderDetails() throws TransactionServiceException,
182
            TException {
3390 mandeep.dh 183
        transactionServiceClient = new TransactionClient().getClient();
3422 mandeep.dh 184
        List<Order> allOrders = transactionServiceClient.getOrdersForCustomer(
185
                user.getUserId(), 0, (new Date()).getTime(), null);
2724 vikas 186
        orderCount = allOrders.size();
3090 mandeep.dh 187
 
2724 vikas 188
        for (Order o : allOrders) {
189
            if (o.getStatus() == OrderStatus.DELIVERY_SUCCESS) {
190
                completedOrderCount++;
3422 mandeep.dh 191
            } else if (CRMConstants.failedStatusList.contains(o.getStatus())) {
2724 vikas 192
                failedOrderCount++;
3422 mandeep.dh 193
            } else {
2724 vikas 194
                openOrderCount++;
195
            }
196
        }
3090 mandeep.dh 197
    }
198
 
3422 mandeep.dh 199
    private void loadCartDetails() throws UserContextException, TException {
3390 mandeep.dh 200
        userContextServiceClient = new UserClient().getClient();
3422 mandeep.dh 201
        UserState userState = userContextServiceClient.getUserState(user
202
                .getUserId());
3390 mandeep.dh 203
        lastLogin = SDF.format(new Date(userState.getLastLogin()));
3090 mandeep.dh 204
        Cart cart = null;
205
 
2724 vikas 206
        try {
3090 mandeep.dh 207
            cart = userContextServiceClient.getCurrentCart(user.getUserId());
2724 vikas 208
            if (cart.getCouponCode() != null) {
209
                couponCode = cart.getCouponCode();
210
            }
211
            for (Line line : cart.getLines()) {
212
                cartItems += line.getQuantity();
213
            }
214
        } catch (Exception e) {
215
            log.warn("No cart assigned for this user", e);
216
        }
2674 vikas 217
    }
2724 vikas 218
 
3390 mandeep.dh 219
    private void loadTicketDetails() throws TException {
3090 mandeep.dh 220
        // Fetching tickets' info
221
        ticketCount = 0;
222
        openTicketCount = 0;
223
        closedTicketCount = 0;
224
 
3422 mandeep.dh 225
        crmServiceClient = new CRMClient().getClient();
3390 mandeep.dh 226
        SearchFilter searchFilter = new SearchFilter();
227
        searchFilter.setCustomerId(user.getUserId());
228
 
229
        List<Ticket> tickets = crmServiceClient.getTickets(searchFilter);
3422 mandeep.dh 230
        if (tickets != null) {
3090 mandeep.dh 231
            for (Ticket ticket : tickets) {
232
                ticketCount++;
3106 mandeep.dh 233
                if (!ticket.getStatus().equals(TicketStatus.CLOSED)) {
3090 mandeep.dh 234
                    openTicketCount++;
3422 mandeep.dh 235
                } else {
3090 mandeep.dh 236
                    closedTicketCount++;
237
                }
238
            }
239
        }
240
    }
241
 
3397 mandeep.dh 242
    public String getToday() {
243
        return SDF.format(new Date());
244
    }
245
 
246
    public String getYesterday() {
247
        Calendar calendar = Calendar.getInstance();
248
        calendar.setTime(new Date());
249
        calendar.add(Calendar.DAY_OF_MONTH, -1);
250
        return SDF.format(calendar.getTime());
251
    }
252
 
253
    public List<Agent> getAllAgents() {
254
        return CRMAuthorizingRealm.getAgents();
255
    }
256
 
2724 vikas 257
    public void setEmail(String email) {
258
        this.email = email;
259
    }
260
 
261
    public String getEmail() {
262
        return email;
263
    }
264
 
265
    public void setOrderId(String orderId) {
3390 mandeep.dh 266
        this.orderId = orderId;
2724 vikas 267
    }
268
 
3390 mandeep.dh 269
    public String getOrderId() {
2724 vikas 270
        return orderId;
271
    }
3422 mandeep.dh 272
 
2724 vikas 273
    public User getUser() {
274
        return user;
275
    }
3422 mandeep.dh 276
 
2724 vikas 277
    public long getOrderCount() {
278
        return orderCount;
279
    }
3422 mandeep.dh 280
 
2724 vikas 281
    public long getCompletedOrderCount() {
282
        return completedOrderCount;
283
    }
3422 mandeep.dh 284
 
2724 vikas 285
    public long getOpenOrderCount() {
286
        return openOrderCount;
287
    }
3422 mandeep.dh 288
 
2724 vikas 289
    public long getFailedOrderCount() {
290
        return failedOrderCount;
291
    }
3422 mandeep.dh 292
 
2724 vikas 293
    public double getCartItems() {
294
        return cartItems;
295
    }
3422 mandeep.dh 296
 
2724 vikas 297
    public String getLastLogin() {
298
        return lastLogin;
299
    }
300
 
301
    public String getCouponCode() {
302
        return couponCode;
303
    }
2830 vikas 304
 
305
    public long getUserCommunicationCount() {
306
        return userCommunicationCount;
307
    }
3090 mandeep.dh 308
 
309
    public long getTicketCount() {
310
        return ticketCount;
311
    }
312
 
313
    public long getOpenTicketCount() {
314
        return openTicketCount;
315
    }
316
 
317
    public long getClosedTicketCount() {
318
        return closedTicketCount;
319
    }
320
 
321
    public void setMobileNumber(String mobileNumber) {
322
        this.mobileNumber = mobileNumber;
323
    }
3096 mandeep.dh 324
 
325
    public String getTicketId() {
326
        return ticketId;
327
    }
328
 
329
    public void setTicketId(String ticketId) {
330
        this.ticketId = ticketId;
331
    }
3106 mandeep.dh 332
 
333
    public long getAgentOpenTicketCount() {
334
        return agentOpenTicketCount;
335
    }
336
 
337
    public long getUnassignedTicketCount() {
338
        return unassignedTicketCount;
339
    }
340
 
341
    public long getActivityCount() {
342
        return activityCount;
343
    }
344
 
345
    public void setActivityCount(long activityCount) {
346
        this.activityCount = activityCount;
347
    }
3339 mandeep.dh 348
 
349
    public long getCustomerActivityCount() {
350
        return customerActivityCount;
351
    }
352
 
353
    public void setCustomerActivityCount(long customerActivityCount) {
354
        this.customerActivityCount = customerActivityCount;
355
    }
2724 vikas 356
}