Subversion Repositories SmartDukaan

Rev

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