Subversion Repositories SmartDukaan

Rev

Rev 2830 | Rev 3096 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2830 Rev 3090
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
2
 
2
 
-
 
3
import in.shop2020.crm.Activity;
-
 
4
import in.shop2020.crm.CRMService.Client;
-
 
5
import in.shop2020.crm.Ticket;
-
 
6
import in.shop2020.crm.TicketStatus;
3
import in.shop2020.model.v1.order.Order;
7
import in.shop2020.model.v1.order.Order;
4
import in.shop2020.model.v1.order.OrderStatus;
8
import in.shop2020.model.v1.order.OrderStatus;
-
 
9
import in.shop2020.model.v1.order.TransactionServiceException;
5
import in.shop2020.model.v1.user.Cart;
10
import in.shop2020.model.v1.user.Cart;
6
import in.shop2020.model.v1.user.Line;
11
import in.shop2020.model.v1.user.Line;
7
import in.shop2020.model.v1.user.User;
12
import in.shop2020.model.v1.user.User;
8
import in.shop2020.thrift.clients.TransactionServiceClient;
13
import in.shop2020.model.v1.user.UserCommunication;
-
 
14
import in.shop2020.model.v1.user.UserCommunicationException;
9
import in.shop2020.thrift.clients.UserContextServiceClient;
15
import in.shop2020.model.v1.user.UserContextException;
-
 
16
import in.shop2020.model.v1.user.UserState;
10
 
17
 
11
import java.text.SimpleDateFormat;
18
import java.text.SimpleDateFormat;
12
import java.util.Arrays;
19
import java.util.Arrays;
13
import java.util.Date;
20
import java.util.Date;
-
 
21
import java.util.HashSet;
14
import java.util.List;
22
import java.util.List;
-
 
23
import java.util.Set;
15
 
24
 
16
import org.apache.log4j.Logger;
25
import org.apache.log4j.Logger;
-
 
26
import org.apache.shiro.SecurityUtils;
-
 
27
import org.apache.shiro.crypto.hash.Sha256Hash;
17
import org.apache.struts2.convention.annotation.Action;
28
import org.apache.struts2.convention.annotation.Action;
-
 
29
import org.apache.thrift.TException;
18
 
30
 
19
/**
31
/**
20
 * @author vikas
32
 * @author vikas
21
 *
33
 *
22
 */
34
 */
23
@SuppressWarnings("serial")
35
@SuppressWarnings("serial")
24
public class HomeController extends BaseController {
36
public class HomeController extends BaseController {
25
    private static Logger log = Logger.getLogger(Class.class);
37
    private static final Logger log = Logger.getLogger(HomeController.class);
-
 
38
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-
 
39
 
-
 
40
    private static final Set<OrderStatus> failedStatusList = new HashSet<OrderStatus>(Arrays.asList(new OrderStatus[] {
-
 
41
            OrderStatus.SALES_RET_RESHIPPED,
-
 
42
            OrderStatus.SALES_RET_REFUNDED,
-
 
43
            OrderStatus.DOA_INVALID_REFUNDED,
-
 
44
            OrderStatus.DOA_INVALID_RESHIPPED,
-
 
45
            OrderStatus.DOA_VALID_REFUNDED,
-
 
46
            OrderStatus.DOA_RESHIPPED,
-
 
47
            OrderStatus.REFUNDED,
-
 
48
            OrderStatus.REJECTED,
-
 
49
            OrderStatus.FAILED}));
-
 
50
 
26
    private String email;
51
    private String email;
27
    private long orderId;
52
    private long orderId;
-
 
53
    private String mobileNumber;
28
    private User user;
54
    private User user;
29
    private long orderCount;
55
    private long orderCount;
30
    private long completedOrderCount;
56
    private long completedOrderCount;
31
    private long openOrderCount;
57
    private long openOrderCount;
32
    private long failedOrderCount;
58
    private long failedOrderCount;
33
    private long userCommunicationCount;
59
    private long userCommunicationCount;
-
 
60
    private long ticketCount;
-
 
61
    private long openTicketCount;
-
 
62
    private long closedTicketCount;
34
    private String lastLogin;
63
    private String lastLogin;
35
    private double cartItems;
64
    private double cartItems;
36
    private String couponCode = "";
65
    private String couponCode = "";
37
    
-
 
38
    public HomeController(){
-
 
39
        super();
-
 
40
    }
-
 
41
 
66
 
42
    @Action("/")
67
    @Action("/")
43
    public String index() throws Exception {
68
    public String index() throws Exception {
44
        if (email == null) {
69
        try {
45
            return "input";
70
            createServiceClients();
46
        }
71
 
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()) {
72
            if (email == null) {
51
            if (orderId != 0) {
73
                return INPUT;
52
                try {
74
            }
-
 
75
 
-
 
76
            if (email.isEmpty()) {
53
                    Order order = transactionServiceClient.getClient().getOrder(orderId);
77
                if (mobileNumber != null && !mobileNumber.isEmpty()) {
54
                    user = userClient.getUserById(order.getCustomer_id());
78
                    user = userContextServiceClient.getUserByMobileNumber(Long.parseLong(mobileNumber));
55
                }
79
                }
56
                catch (Exception e) {
80
                else {
57
                    addActionError("Invalid Input");
81
                    if (orderId != 0) {
-
 
82
                        Order order = transactionServiceClient.getOrder(orderId);
-
 
83
                        user = userContextServiceClient.getUserById(order.getCustomer_id());
58
                    return "input";
84
                    }
59
                }
85
                }
60
            }
86
            }
61
        }
-
 
62
        else {
-
 
63
            try {
87
            else {
64
                user = userClient.getUserByEmail(email);
88
                user = userContextServiceClient.getUserByEmail(email);
65
            } catch (Exception e) {
-
 
66
                log.warn("Unable to get user service client", e);
-
 
67
                return "input";
-
 
68
            }
89
            }
-
 
90
 
-
 
91
            if (user == null || user.getUserId() <= 0) {
-
 
92
                addActionError("Invalid Input");
-
 
93
                return INPUT;
-
 
94
            }
-
 
95
 
-
 
96
            loadUserCommunicationDetails(userContextServiceClient);
-
 
97
 
-
 
98
            loadOrderDetails(transactionServiceClient);
-
 
99
 
-
 
100
            loadCartDetails(userContextServiceClient);
-
 
101
 
-
 
102
            loadTicketDetails(crmServiceClient);
-
 
103
 
-
 
104
            return INDEX;            
69
        }
105
        }
70
        if (user == null || user.getUserId() <= 0) {
106
        catch (Exception e) {
71
            addActionError("Invalid Input");
107
            addActionError("Invalid Input");
-
 
108
            log.error("Error occurred", e);
72
            return "input";
109
            return INPUT;
73
        }
110
        }
74
        
111
    }
-
 
112
 
-
 
113
    private void loadUserCommunicationDetails(
-
 
114
            in.shop2020.model.v1.user.UserContextService.Client userContextServiceClient)
-
 
115
            throws UserCommunicationException, TException {
75
        userCommunicationCount = userClient.getUserCommunicationByUser(user.getUserId()).size();
116
        List<UserCommunication> userCommunication = userContextServiceClient.getUserCommunicationByUser(user.getUserId());
-
 
117
        userCommunicationCount = userCommunication.size();
76
        
118
    }
-
 
119
 
-
 
120
    private void loadOrderDetails(
-
 
121
            in.shop2020.model.v1.order.TransactionService.Client transactionServiceClient)
-
 
122
            throws TransactionServiceException, TException {
77
        List<Order> allOrders = transactionServiceClient.getClient().getOrdersForCustomer(user.getUserId(), 0, (new Date()).getTime(), null);
123
        List<Order> allOrders = transactionServiceClient.getOrdersForCustomer(user.getUserId(), 0, (new Date()).getTime(), null);
78
        orderCount = allOrders.size();
124
        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
        
125
 
91
        for (Order o : allOrders) {
126
        for (Order o : allOrders) {
92
            if (o.getStatus() == OrderStatus.DELIVERY_SUCCESS) {
127
            if (o.getStatus() == OrderStatus.DELIVERY_SUCCESS) {
93
                completedOrderCount++;
128
                completedOrderCount++;
94
            }
129
            }
95
            else if (failedStatusList.contains(o.getStatus())) {
130
            else if (failedStatusList.contains(o.getStatus())) {
Line 97... Line 132...
97
            }
132
            }
98
            else {
133
            else {
99
                openOrderCount++;
134
                openOrderCount++;
100
            }
135
            }
101
        }
136
        }
-
 
137
    }
-
 
138
 
-
 
139
    private void loadCartDetails(
-
 
140
            in.shop2020.model.v1.user.UserContextService.Client userContextServiceClient)
-
 
141
            throws UserContextException, TException {
102
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
142
        UserState userState = userContextServiceClient.getUserState(user.getUserId());
103
        lastLogin = sdf.format(new Date(userClient.getUserState(user.getUserId()).getLastLogin()));
143
        lastLogin = sdf.format(new Date(userState.getLastLogin()));
-
 
144
        Cart cart = null;
-
 
145
 
104
        try {
146
        try {
105
            Cart cart = userClient.getCurrentCart(user.getUserId());
147
            cart = userContextServiceClient.getCurrentCart(user.getUserId());
106
            if (cart.getCouponCode() != null) {
148
            if (cart.getCouponCode() != null) {
107
                couponCode = cart.getCouponCode();
149
                couponCode = cart.getCouponCode();
108
            }
150
            }
109
            for (Line line : cart.getLines()) {
151
            for (Line line : cart.getLines()) {
110
                cartItems += line.getQuantity();
152
                cartItems += line.getQuantity();
111
            }
153
            }
112
        } catch (Exception e) {
154
        } catch (Exception e) {
113
            log.warn("No cart assigned for this user", e);
155
            log.warn("No cart assigned for this user", e);
114
        }
156
        }
-
 
157
    }
-
 
158
 
-
 
159
    private void loadTicketDetails(Client crmServiceClient) throws TException {
-
 
160
        // Fetching tickets' info
-
 
161
        ticketCount = 0;
-
 
162
        openTicketCount = 0;
-
 
163
        closedTicketCount = 0;
-
 
164
 
-
 
165
        List<Ticket> tickets = crmServiceClient.getTickets(user.getUserId());
-
 
166
        if (tickets != null) 
-
 
167
        {
-
 
168
            for (Ticket ticket : tickets) {
-
 
169
                ticketCount++;
-
 
170
                Activity activity = crmServiceClient.getLastActivity(ticket.getId());
-
 
171
 
-
 
172
                if (activity != null && !activity.getTicketStatus().equals(TicketStatus.CLOSED)) {
-
 
173
                    openTicketCount++;
-
 
174
                }
115
        return "index";
175
                else {
-
 
176
                    closedTicketCount++;
-
 
177
                }
-
 
178
            }
-
 
179
        }
116
    }
180
    }
117
 
181
 
118
    public void setEmail(String email) {
182
    public void setEmail(String email) {
119
        this.email = email;
183
        this.email = email;
120
    }
184
    }
Line 122... Line 186...
122
    public String getEmail() {
186
    public String getEmail() {
123
        return email;
187
        return email;
124
    }
188
    }
125
 
189
 
126
    public void setOrderId(String orderId) {
190
    public void setOrderId(String orderId) {
127
        try {
191
        if (orderId != null && !orderId.isEmpty()) {
128
            this.orderId = Long.parseLong(orderId);
192
            this.orderId = Long.parseLong(orderId);
129
        }
193
        }
130
        catch (NumberFormatException e) {
-
 
131
            log.error(e);
-
 
132
        }
-
 
133
    }
194
    }
134
 
195
 
135
    public Long getOrderId() {
196
    public Long getOrderId() {
136
        return orderId;
197
        return orderId;
137
    }
198
    }
Line 169... Line 230...
169
    }
230
    }
170
 
231
 
171
    public long getUserCommunicationCount() {
232
    public long getUserCommunicationCount() {
172
        return userCommunicationCount;
233
        return userCommunicationCount;
173
    }
234
    }
-
 
235
 
-
 
236
    public long getTicketCount() {
-
 
237
        return ticketCount;
-
 
238
    }
-
 
239
 
-
 
240
    public long getOpenTicketCount() {
-
 
241
        return openTicketCount;
-
 
242
    }
-
 
243
 
-
 
244
    public long getClosedTicketCount() {
-
 
245
        return closedTicketCount;
-
 
246
    }
-
 
247
 
-
 
248
    public void setMobileNumber(String mobileNumber) {
-
 
249
        this.mobileNumber = mobileNumber;
-
 
250
    }
174
}
251
}
175
252