Subversion Repositories SmartDukaan

Rev

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

Rev 3125 Rev 3213
Line 30... Line 30...
30
import org.apache.poi.ss.usermodel.Row;
30
import org.apache.poi.ss.usermodel.Row;
31
import org.apache.poi.ss.usermodel.Sheet;
31
import org.apache.poi.ss.usermodel.Sheet;
32
import org.apache.poi.ss.usermodel.Workbook;
32
import org.apache.poi.ss.usermodel.Workbook;
33
import org.apache.poi.ss.util.CellRangeAddress;
33
import org.apache.poi.ss.util.CellRangeAddress;
34
import org.apache.thrift.TException;
34
import org.apache.thrift.TException;
-
 
35
import org.slf4j.Logger;
-
 
36
import org.slf4j.LoggerFactory;
35
 
37
 
36
public class RegisteredUsersGenerator {
38
public class RegisteredUsersGenerator {
-
 
39
    
-
 
40
    private static Logger logger = LoggerFactory.getLogger(RegisteredUsersGenerator.class);
-
 
41
    
37
	UserClient usc;
42
	UserClient usc;
38
    in.shop2020.model.v1.user.UserContextService.Client uClient;
43
    in.shop2020.model.v1.user.UserContextService.Client uClient;
39
    
44
    
40
    TransactionClient tsc;
45
    TransactionClient tsc;
41
    in.shop2020.model.v1.order.TransactionService.Client tClient;
46
    in.shop2020.model.v1.order.TransactionService.Client tClient;
Line 46... Line 51...
46
            uClient = usc.getClient();
51
            uClient = usc.getClient();
47
            
52
            
48
            tsc = new TransactionClient();
53
            tsc = new TransactionClient();
49
            tClient = tsc.getClient();
54
            tClient = tsc.getClient();
50
        } catch (Exception e) {
55
        } catch (Exception e) {
51
            e.printStackTrace();
56
            logger.error("Error initializing connection to user or order service", e);
52
        }
57
        }
53
    }
58
    }
54
 
59
 
55
    /**
60
    /**
56
     * This method is used in RegisteredUsersController. 
61
     * This method is used in RegisteredUsersController. 
Line 63... Line 68...
63
    public ByteArrayOutputStream generateRegisteredUsersReport() {
68
    public ByteArrayOutputStream generateRegisteredUsersReport() {
64
        List<User> users = null;
69
        List<User> users = null;
65
        try {
70
        try {
66
            users = uClient.getAllUsers(UserType.USER, -1, -1);
71
            users = uClient.getAllUsers(UserType.USER, -1, -1);
67
        } catch (TException e) {
72
        } catch (TException e) {
68
            e.printStackTrace();
73
            logger.error("Unable to get all users", e);
69
        }
74
        }
70
        if(users == null || users.isEmpty()) {
75
        if(users == null || users.isEmpty()) {
71
            return null;
76
            return null;
72
        }
77
        }
73
        // Preparing XLS file for output
78
        // Preparing XLS file for output
Line 140... Line 145...
140
                calendar.setTimeInMillis(uState.getActiveSince());
145
                calendar.setTimeInMillis(uState.getActiveSince());
141
                contentRow.createCell(7).setCellValue(formatter.format(calendar.getTime()));
146
                contentRow.createCell(7).setCellValue(formatter.format(calendar.getTime()));
142
                calendar.setTimeInMillis(uState.getLastLogin());
147
                calendar.setTimeInMillis(uState.getLastLogin());
143
                contentRow.createCell(8).setCellValue(formatter.format(calendar.getTime()));
148
                contentRow.createCell(8).setCellValue(formatter.format(calendar.getTime()));
144
            } catch (UserContextException e) {
149
            } catch (UserContextException e) {
145
                e.printStackTrace();
150
                logger.error("Error while getting user state", e);
146
            } catch (TException e) {
151
            } catch (TException e) {
147
                e.printStackTrace();
152
                logger.error("Unable to get user state", e);
148
            }
153
            }
149
            
154
            
150
            try {
155
            try {
151
                List<Order> orders = tClient.getOrdersForCustomer(u.getUserId(), 0, currentDate.getTime(), null);
156
                List<Order> orders = tClient.getOrdersForCustomer(u.getUserId(), 0, currentDate.getTime(), null);
152
                int noOfFailedOrders = 0, noOfValidOrders = 0;
157
                int noOfFailedOrders = 0, noOfValidOrders = 0;
Line 158... Line 163...
158
                    }
163
                    }
159
                }
164
                }
160
                contentRow.createCell(9).setCellValue(noOfValidOrders);
165
                contentRow.createCell(9).setCellValue(noOfValidOrders);
161
                contentRow.createCell(10).setCellValue(noOfFailedOrders);
166
                contentRow.createCell(10).setCellValue(noOfFailedOrders);
162
            } catch (TransactionServiceException e) {
167
            } catch (TransactionServiceException e) {
163
                e.printStackTrace();
168
                logger.error("Error while getting orders for the customer", e);
164
            } catch (TException e) {
169
            } catch (TException e) {
165
                e.printStackTrace();
170
                logger.error("Unable to get orders for the customer", e);
166
            }
171
            }
167
            
172
            
168
            String itemList = getCartItems(u.getUserId());
173
            String itemList = getCartItems(u.getUserId());
169
            contentRow.createCell(11).setCellValue(itemList);
174
            contentRow.createCell(11).setCellValue(itemList);
170
            contentRow.getCell(11).setCellStyle(styleWT);
175
            contentRow.getCell(11).setCellStyle(styleWT);
Line 204... Line 209...
204
        // Write the workbook to the output stream
209
        // Write the workbook to the output stream
205
        try {
210
        try {
206
            wb.write(baosXLS);
211
            wb.write(baosXLS);
207
            baosXLS.close();
212
            baosXLS.close();
208
        } catch (IOException e) {
213
        } catch (IOException e) {
209
            e.printStackTrace();
214
            logger.error("Unable to write the registered orders report to the byte array", e);
210
        }
215
        }
211
        return baosXLS;
216
        return baosXLS;
212
    }
217
    }
213
    
218
    
214
    private String getCartItems(long userId) {
219
    private String getCartItems(long userId) {
Line 230... Line 235...
230
                    continue;
235
                    continue;
231
                }
236
                }
232
                itemList += ", " + line.getItemId();;
237
                itemList += ", " + line.getItemId();;
233
            }
238
            }
234
        } catch (ShoppingCartException e) {
239
        } catch (ShoppingCartException e) {
235
            e.printStackTrace();
240
            logger.error("Error while getting the cart of the user", e);
236
        } catch (TException e) {
241
        } catch (TException e) {
237
            e.printStackTrace();
242
            logger.error("Unable to get the cart of the user", e);
238
        }
243
        }
239
        return itemList;
244
        return itemList;
240
    }
245
    }
241
    
246
    
242
    private String generateAddressString(Address a) {
247
    private String generateAddressString(Address a) {
Line 266... Line 271...
266
                return;
271
                return;
267
            }
272
            }
268
            baosXLS.writeTo(f);
273
            baosXLS.writeTo(f);
269
            f.close();
274
            f.close();
270
        } catch (FileNotFoundException e) {
275
        } catch (FileNotFoundException e) {
271
            e.printStackTrace();
276
            logger.error("Unable to create the registered users report", e);
272
        } catch (IOException e) {
277
        } catch (IOException e) {
273
            e.printStackTrace();
278
            logger.error("IO Error while creating the registered users report", e);
274
        }
279
        }
275
        System.out.println("Successfully generated the registered users report");
280
        System.out.println("Successfully generated the registered users report");
276
    }
281
    }
277
}
282
}