Subversion Repositories SmartDukaan

Rev

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