Subversion Repositories SmartDukaan

Rev

Rev 2724 | Rev 3090 | 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
 
2724 vikas 3
import in.shop2020.model.v1.order.Order;
4
import in.shop2020.model.v1.order.OrderStatus;
5
import in.shop2020.model.v1.user.Cart;
6
import in.shop2020.model.v1.user.Line;
7
import in.shop2020.model.v1.user.User;
8
import in.shop2020.thrift.clients.TransactionServiceClient;
9
import in.shop2020.thrift.clients.UserContextServiceClient;
2674 vikas 10
 
2724 vikas 11
import java.text.SimpleDateFormat;
12
import java.util.Arrays;
13
import java.util.Date;
14
import java.util.List;
15
 
2674 vikas 16
import org.apache.log4j.Logger;
17
import org.apache.struts2.convention.annotation.Action;
18
 
19
/**
20
 * @author vikas
21
 *
22
 */
23
@SuppressWarnings("serial")
24
public class HomeController extends BaseController {
25
    private static Logger log = Logger.getLogger(Class.class);
2724 vikas 26
    private String email;
27
    private long orderId;
28
    private User user;
29
    private long orderCount;
30
    private long completedOrderCount;
31
    private long openOrderCount;
32
    private long failedOrderCount;
2830 vikas 33
    private long userCommunicationCount;
2724 vikas 34
    private String lastLogin;
35
    private double cartItems;
36
    private String couponCode = "";
2674 vikas 37
 
38
    public HomeController(){
39
        super();
40
    }
41
 
42
    @Action("/")
2724 vikas 43
    public String index() throws Exception {
44
        if (email == null) {
45
            return "input";
46
        }
47
        UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
48
        in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
49
        TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
50
        if (email == null || email.isEmpty()) {
51
            if (orderId != 0) {
52
                try {
53
                    Order order = transactionServiceClient.getClient().getOrder(orderId);
54
                    user = userClient.getUserById(order.getCustomer_id());
55
                }
56
                catch (Exception e) {
57
                    addActionError("Invalid Input");
58
                    return "input";
59
                }
60
            }
61
        }
62
        else {
63
            try {
64
                user = userClient.getUserByEmail(email);
65
            } catch (Exception e) {
66
                log.warn("Unable to get user service client", e);
67
                return "input";
68
            }
69
        }
70
        if (user == null || user.getUserId() <= 0) {
71
            addActionError("Invalid Input");
72
            return "input";
73
        }
74
 
2830 vikas 75
        userCommunicationCount = userClient.getUserCommunicationByUser(user.getUserId()).size();
76
 
2724 vikas 77
        List<Order> allOrders = transactionServiceClient.getClient().getOrdersForCustomer(user.getUserId(), 0, (new Date()).getTime(), null);
78
        orderCount = allOrders.size();
79
 
80
        List<OrderStatus> failedStatusList = Arrays.asList(new OrderStatus[] {
81
                OrderStatus.SALES_RET_RESHIPPED,
82
                OrderStatus.SALES_RET_REFUNDED,
83
                OrderStatus.DOA_INVALID_REFUNDED,
84
                OrderStatus.DOA_INVALID_RESHIPPED,
85
                OrderStatus.DOA_VALID_REFUNDED,
86
                OrderStatus.DOA_RESHIPPED,
87
                OrderStatus.REFUNDED,
88
                OrderStatus.REJECTED,
89
                OrderStatus.FAILED});
90
 
91
        for (Order o : allOrders) {
92
            if (o.getStatus() == OrderStatus.DELIVERY_SUCCESS) {
93
                completedOrderCount++;
94
            }
95
            else if (failedStatusList.contains(o.getStatus())) {
96
                failedOrderCount++;
97
            }
98
            else {
99
                openOrderCount++;
100
            }
101
        }
102
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
103
        lastLogin = sdf.format(new Date(userClient.getUserState(user.getUserId()).getLastLogin()));
104
        try {
105
            Cart cart = userClient.getCurrentCart(user.getUserId());
106
            if (cart.getCouponCode() != null) {
107
                couponCode = cart.getCouponCode();
108
            }
109
            for (Line line : cart.getLines()) {
110
                cartItems += line.getQuantity();
111
            }
112
        } catch (Exception e) {
113
            log.warn("No cart assigned for this user", e);
114
        }
2674 vikas 115
        return "index";
116
    }
2724 vikas 117
 
118
    public void setEmail(String email) {
119
        this.email = email;
120
    }
121
 
122
    public String getEmail() {
123
        return email;
124
    }
125
 
126
    public void setOrderId(String orderId) {
127
        try {
128
            this.orderId = Long.parseLong(orderId);
129
        }
130
        catch (NumberFormatException e) {
131
            log.error(e);
132
        }
133
    }
134
 
135
    public Long getOrderId() {
136
        return orderId;
137
    }
138
 
139
    public User getUser() {
140
        return user;
141
    }
142
 
143
    public long getOrderCount() {
144
        return orderCount;
145
    }
146
 
147
    public long getCompletedOrderCount() {
148
        return completedOrderCount;
149
    }
150
 
151
    public long getOpenOrderCount() {
152
        return openOrderCount;
153
    }
154
 
155
    public long getFailedOrderCount() {
156
        return failedOrderCount;
157
    }
158
 
159
    public double getCartItems() {
160
        return cartItems;
161
    }
162
 
163
    public String getLastLogin() {
164
        return lastLogin;
165
    }
166
 
167
    public String getCouponCode() {
168
        return couponCode;
169
    }
2830 vikas 170
 
171
    public long getUserCommunicationCount() {
172
        return userCommunicationCount;
173
    }
2724 vikas 174
}