Subversion Repositories SmartDukaan

Rev

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

Rev 1684 Rev 1879
Line 1... Line 1...
1
package in.shop2020.support.services;
1
package in.shop2020.support.services;
2
 
2
 
-
 
3
import in.shop2020.model.v1.catalog.InventoryServiceException;
-
 
4
import in.shop2020.model.v1.catalog.Item;
-
 
5
import in.shop2020.model.v1.order.Order;
-
 
6
import in.shop2020.model.v1.order.TransactionServiceException;
3
import in.shop2020.model.v1.user.Address;
7
import in.shop2020.model.v1.user.Address;
-
 
8
import in.shop2020.model.v1.user.Cart;
-
 
9
import in.shop2020.model.v1.user.Line;
-
 
10
import in.shop2020.model.v1.user.ShoppingCartException;
4
import in.shop2020.model.v1.user.User;
11
import in.shop2020.model.v1.user.User;
5
import in.shop2020.model.v1.user.UserContextException;
12
import in.shop2020.model.v1.user.UserContextException;
6
import in.shop2020.model.v1.user.UserState;
13
import in.shop2020.model.v1.user.UserState;
7
import in.shop2020.model.v1.user.UserType;
14
import in.shop2020.model.v1.user.UserType;
-
 
15
import in.shop2020.thrift.clients.CatalogServiceClient;
-
 
16
import in.shop2020.thrift.clients.TransactionServiceClient;
8
import in.shop2020.thrift.clients.UserContextServiceClient;
17
import in.shop2020.thrift.clients.UserContextServiceClient;
9
 
18
 
10
import java.io.ByteArrayOutputStream;
19
import java.io.ByteArrayOutputStream;
11
import java.io.FileNotFoundException;
20
import java.io.FileNotFoundException;
12
import java.io.FileOutputStream;
21
import java.io.FileOutputStream;
13
import java.io.IOException;
22
import java.io.IOException;
14
import java.text.DateFormat;
23
import java.text.DateFormat;
15
import java.text.SimpleDateFormat;
24
import java.text.SimpleDateFormat;
16
import java.util.Calendar;
25
import java.util.Calendar;
-
 
26
import java.util.Date;
17
import java.util.List;
27
import java.util.List;
18
 
28
 
19
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
29
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
20
import org.apache.poi.ss.usermodel.Cell;
30
import org.apache.poi.ss.usermodel.Cell;
21
import org.apache.poi.ss.usermodel.CellStyle;
31
import org.apache.poi.ss.usermodel.CellStyle;
Line 27... Line 37...
27
import org.apache.thrift.TException;
37
import org.apache.thrift.TException;
28
 
38
 
29
public class RegisteredUsersGenerator {
39
public class RegisteredUsersGenerator {
30
    UserContextServiceClient usc;
40
    UserContextServiceClient usc;
31
    in.shop2020.model.v1.user.UserContextService.Client uClient;
41
    in.shop2020.model.v1.user.UserContextService.Client uClient;
-
 
42
    
-
 
43
    TransactionServiceClient tsc;
-
 
44
    in.shop2020.model.v1.order.TransactionService.Client tClient;
32
 
45
    
33
    public RegisteredUsersGenerator() {
46
    public RegisteredUsersGenerator() {
34
        try {
47
        try {
35
            usc = new UserContextServiceClient();
48
            usc = new UserContextServiceClient();
36
            uClient = usc.getClient();
49
            uClient = usc.getClient();
-
 
50
            
-
 
51
            tsc = new TransactionServiceClient();
-
 
52
            tClient = tsc.getClient();
37
        } catch (Exception e) {
53
        } catch (Exception e) {
38
            e.printStackTrace();
54
            e.printStackTrace();
39
        }
55
        }
40
    }
56
    }
41
 
57
 
Line 48... Line 64...
48
     * @return
64
     * @return
49
     */
65
     */
50
    public ByteArrayOutputStream generateRegisteredUsersReport() {
66
    public ByteArrayOutputStream generateRegisteredUsersReport() {
51
        List<User> users = null;
67
        List<User> users = null;
52
        try {
68
        try {
53
            users = uClient.getAllUsers(UserType.USER);
69
            users = uClient.getAllUsers(UserType.USER, -1, -1);
54
        } catch (TException e) {
70
        } catch (TException e) {
55
            // TODO Auto-generated catch block
-
 
56
            e.printStackTrace();
71
            e.printStackTrace();
57
        }
72
        }
58
        if(users == null || users.isEmpty()) {
73
        if(users == null || users.isEmpty()) {
59
            return null;
74
            return null;
60
        }
75
        }
Line 96... Line 111...
96
        headerRow.createCell(4).setCellValue("Date of Birth");
111
        headerRow.createCell(4).setCellValue("Date of Birth");
97
        headerRow.createCell(5).setCellValue("Sex");
112
        headerRow.createCell(5).setCellValue("Sex");
98
        headerRow.createCell(6).setCellValue("Mobile");
113
        headerRow.createCell(6).setCellValue("Mobile");
99
        headerRow.createCell(7).setCellValue("Registered On");
114
        headerRow.createCell(7).setCellValue("Registered On");
100
        headerRow.createCell(8).setCellValue("Last Login");
115
        headerRow.createCell(8).setCellValue("Last Login");
-
 
116
        headerRow.createCell(9).setCellValue("Successful Orders");
-
 
117
        headerRow.createCell(10).setCellValue("Pending/Failed Orders");
-
 
118
        headerRow.createCell(11).setCellValue("Items in Cart");
-
 
119
        userSheet.setColumnWidth(11, 20 * 256); // set width of items in cart column to 20 chars
101
        
120
        
102
        Calendar calendar = Calendar.getInstance();
121
        Calendar calendar = Calendar.getInstance();
103
        Row contentRow;
122
        Row contentRow;
104
        float defaultRowHeight = userSheet.getDefaultRowHeightInPoints();
123
        float defaultRowHeight = userSheet.getDefaultRowHeightInPoints();
105
        UserState uState;
124
        UserState uState;
106
        DateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm a");
125
        DateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm a");
-
 
126
        Date currentDate = new Date();
107
        for (User u : users) {
127
        for (User u : users) {
108
            userSerialNo++;
128
            userSerialNo++;
109
            contentRow = userSheet.createRow(userSerialNo);
129
            contentRow = userSheet.createRow(userSerialNo);
110
            contentRow.createCell(0).setCellValue(u.getUserId());
130
            contentRow.createCell(0).setCellValue(u.getUserId());
111
            contentRow.createCell(1).setCellValue(u.getEmail());
131
            contentRow.createCell(1).setCellValue(u.getEmail());
Line 114... Line 134...
114
            contentRow.createCell(4).setCellValue(u.getDateOfBirth());
134
            contentRow.createCell(4).setCellValue(u.getDateOfBirth());
115
            // calendar.setTimeInMillis
135
            // calendar.setTimeInMillis
116
            contentRow.createCell(5).setCellValue(u.getSex().name());
136
            contentRow.createCell(5).setCellValue(u.getSex().name());
117
            contentRow.createCell(6).setCellValue(u.getMobileNumber());
137
            contentRow.createCell(6).setCellValue(u.getMobileNumber());
118
            
138
            
-
 
139
            
-
 
140
            
119
            try {
141
            try {
120
                uState = uClient.getUserState(u.getUserId());
142
                uState = uClient.getUserState(u.getUserId());
121
                calendar.setTimeInMillis(uState.getActiveSince());
143
                calendar.setTimeInMillis(uState.getActiveSince());
122
                contentRow.createCell(7).setCellValue(formatter.format(calendar.getTime()));
144
                contentRow.createCell(7).setCellValue(formatter.format(calendar.getTime()));
123
                calendar.setTimeInMillis(uState.getLastLogin());
145
                calendar.setTimeInMillis(uState.getLastLogin());
Line 125... Line 147...
125
            } catch (UserContextException e) {
147
            } catch (UserContextException e) {
126
                e.printStackTrace();
148
                e.printStackTrace();
127
            } catch (TException e) {
149
            } catch (TException e) {
128
                e.printStackTrace();
150
                e.printStackTrace();
129
            }
151
            }
-
 
152
            
-
 
153
            try {
-
 
154
                List<Order> orders = tClient.getOrdersForCustomer(u.getUserId(), 0, currentDate.getTime(), null);
-
 
155
                int noOfFailedOrders = 0, noOfValidOrders = 0;
-
 
156
                for(Order order : orders) {
-
 
157
                    if(order.getStatus().getValue() < 3) {
-
 
158
                        noOfFailedOrders++;
-
 
159
                    } else {
-
 
160
                        noOfValidOrders++;
-
 
161
                    }
-
 
162
                }
-
 
163
                contentRow.createCell(9).setCellValue(noOfValidOrders);
-
 
164
                contentRow.createCell(10).setCellValue(noOfFailedOrders);
-
 
165
            } catch (TransactionServiceException e) {
-
 
166
                e.printStackTrace();
-
 
167
            } catch (TException e) {
-
 
168
                e.printStackTrace();
-
 
169
            }
-
 
170
            
-
 
171
            String itemList = getCartItems(u.getUserId());
-
 
172
            contentRow.createCell(11).setCellValue(itemList);
-
 
173
            contentRow.getCell(11).setCellStyle(styleWT);
130
 
174
 
131
            List<Address> addresses = u.getAddresses();
175
            List<Address> addresses = u.getAddresses();
132
            if(addresses == null || addresses.isEmpty()) {
176
            if(addresses == null || addresses.isEmpty()) {
133
                continue;
177
                continue;
134
            }
178
            }
135
            int i = 0, col, defaultAddrCol = 9, maxAddrLength = 0, len, addrColWidth = 40;
179
            int i = 0, col, defaultAddrCol = 12, maxAddrLength = 0, len, addrColWidth = 40;
136
            String addressString;
180
            String addressString;
137
            for(Address a : addresses) {
181
            for(Address a : addresses) {
138
                if(a.getId() == u.getDefaultAddressId()) {
182
                if(a.getId() == u.getDefaultAddressId()) {
139
                    col = defaultAddrCol;
183
                    col = defaultAddrCol;
140
                    headerRow.createCell(col).setCellValue("Default Address");
184
                    headerRow.createCell(col).setCellValue("Default Address");
Line 149... Line 193...
149
                contentRow.getCell(col).setCellStyle(styleWT);
193
                contentRow.getCell(col).setCellStyle(styleWT);
150
                len = addressString.length();
194
                len = addressString.length();
151
                if(len > maxAddrLength)
195
                if(len > maxAddrLength)
152
                    maxAddrLength = len;
196
                    maxAddrLength = len;
153
            }
197
            }
-
 
198
            if((len = itemList.length()) > maxAddrLength) {
-
 
199
                maxAddrLength = len;
-
 
200
            }
154
            contentRow.setHeightInPoints((maxAddrLength / addrColWidth + 1) * defaultRowHeight); // Setting Row Height
201
            contentRow.setHeightInPoints((maxAddrLength / addrColWidth + 1) * defaultRowHeight); // Setting Row Height
155
        }
202
        }
156
        for (int i = 0; i <= 8; i++) {
203
        for (int i = 0; i <= 10; i++) {
157
            userSheet.autoSizeColumn(i);
204
            userSheet.autoSizeColumn(i);
158
        }
205
        }
159
        
206
        
160
        // Write the workbook to the output stream
207
        // Write the workbook to the output stream
161
        try {
208
        try {
Line 165... Line 212...
165
            e.printStackTrace();
212
            e.printStackTrace();
166
        }
213
        }
167
        return baosXLS;
214
        return baosXLS;
168
    }
215
    }
169
    
216
    
-
 
217
    private String getCartItems(long userId) {
-
 
218
        String itemList = "";
-
 
219
        try {
-
 
220
            Cart cart = uClient.getCurrentCart(userId);
-
 
221
            if(cart == null) {
-
 
222
                return "";
-
 
223
            }
-
 
224
            List<Line> lines = cart.getLines();
-
 
225
            if(lines == null || lines.isEmpty()) {
-
 
226
                return "";
-
 
227
            }
-
 
228
            boolean firstItem = true;
-
 
229
            for(Line line : lines) {
-
 
230
                if(firstItem) {
-
 
231
                    firstItem = false;
-
 
232
                    itemList += line.getItemId();
-
 
233
                    continue;
-
 
234
                }
-
 
235
                itemList += ", " + line.getItemId();;
-
 
236
            }
-
 
237
        } catch (ShoppingCartException e) {
-
 
238
            e.printStackTrace();
-
 
239
        } catch (TException e) {
-
 
240
            e.printStackTrace();
-
 
241
        }
-
 
242
        return itemList;
-
 
243
    }
-
 
244
    
170
    private String generateAddressString(Address a) {
245
    private String generateAddressString(Address a) {
171
        String addrStr = "";
246
        String addrStr = "";
172
        addrStr += "Name: " + getStringValue(a.getName());
247
        addrStr += "Name: " + getStringValue(a.getName());
173
        addrStr += "; Address: " + getStringValue(a.getLine1());
248
        addrStr += "; Address: " + getStringValue(a.getLine1());
174
        addrStr += ", " + getStringValue(a.getLine2());
249
        addrStr += ", " + getStringValue(a.getLine2());