Rev 1879 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.support.services;import in.shop2020.model.v1.user.Address;import in.shop2020.model.v1.user.User;import in.shop2020.model.v1.user.UserType;import in.shop2020.thrift.clients.UserContextServiceClient;import java.io.ByteArrayOutputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Calendar;import java.util.List;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.Font;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.ss.util.CellRangeAddress;import org.apache.thrift.TException;public class RegisteredUsersGenerator {UserContextServiceClient usc;in.shop2020.model.v1.user.UserContextService.Client uClient;public RegisteredUsersGenerator() {try {usc = new UserContextServiceClient();uClient = usc.getClient();} catch (Exception e) {e.printStackTrace();}}/*** This method is used in RegisteredUsersController.* If any registered user(s) exist(s), then returns ByteArrayOutputStream containing user(s) data,* otherwise returns null* @param startDate -- inclusive* @param endDate -- inclusive* @return*/public ByteArrayOutputStream generateRegisteredUsersReport() {List<User> users = null;try {users = uClient.getAllUsers(UserType.USER);} catch (TException e) {// TODO Auto-generated catch blocke.printStackTrace();}if(users == null || users.isEmpty()) {return null;}// Preparing XLS file for outputreturn getSpreadSheetData(users);}// Prepares the XLS worksheet object and fills in the data with proper// formattingprivate ByteArrayOutputStream getSpreadSheetData(List<User> users) {ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();Workbook wb = new HSSFWorkbook();Font font = wb.createFont();font.setBoldweight(Font.BOLDWEIGHT_BOLD);CellStyle style = wb.createCellStyle();style.setFont(font);CellStyle styleWT = wb.createCellStyle();styleWT.setWrapText(true);Sheet userSheet = wb.createSheet("User");short userSerialNo = 0;Row titleRow = userSheet.createRow(userSerialNo++);Cell titleCell = titleRow.createCell(0);titleCell.setCellValue("Registered Users");titleCell.setCellStyle(style);userSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));userSheet.createRow(userSerialNo++);Row headerRow = userSheet.createRow(userSerialNo++);headerRow.createCell(0).setCellValue("User Id");headerRow.createCell(1).setCellValue("Email");headerRow.createCell(2).setCellValue("Name");headerRow.createCell(3).setCellValue("Communication Email");headerRow.createCell(4).setCellValue("Date of Birth");headerRow.createCell(5).setCellValue("Sex");headerRow.createCell(6).setCellValue("Mobile");headerRow.createCell(7).setCellValue("Default Address");int addrColWidth = 40;userSheet.setColumnWidth(7, 256 * addrColWidth); // set width of address column to 40 charactersCalendar calendar = Calendar.getInstance();Row contentRow;float defaultRowHeight = userSheet.getDefaultRowHeightInPoints();for (User u : users) {userSerialNo++;contentRow = userSheet.createRow(userSerialNo);contentRow.createCell(0).setCellValue(u.getUserId());contentRow.createCell(1).setCellValue(u.getEmail());contentRow.createCell(2).setCellValue(u.getName());contentRow.createCell(3).setCellValue(u.getCommunicationEmail());contentRow.createCell(4).setCellValue(u.getDateOfBirth());// calendar.setTimeInMilliscontentRow.createCell(5).setCellValue(u.getSex().name());contentRow.createCell(6).setCellValue(u.getMobileNumber());List<Address> addresses = u.getAddresses();if(addresses == null || addresses.isEmpty()) {continue;}int i = 0, col, defaultAddrCol = 7, maxAddrLength = 0, len;String addressString;for(Address a : addresses) {if(a.getId() == u.getDefaultAddressId()) {col = defaultAddrCol;} else {i++;col = defaultAddrCol + i;headerRow.createCell(col).setCellValue("Address " + i);}userSheet.setColumnWidth(col, addrColWidth * 256);addressString = generateAddressString(a);contentRow.createCell(col).setCellValue(addressString);contentRow.getCell(col).setCellStyle(styleWT);len = addressString.length();if(len > maxAddrLength)maxAddrLength = len;}contentRow.setHeightInPoints((maxAddrLength / addrColWidth + 1) * defaultRowHeight); // Setting Row Height}for (int i = 0; i <= 6; i++) {userSheet.autoSizeColumn(i);}// Write the workbook to the output streamtry {wb.write(baosXLS);baosXLS.close();} catch (IOException e) {e.printStackTrace();}return baosXLS;}private String generateAddressString(Address a) {String addrStr = "";addrStr += "Name: " + getStringValue(a.getName());addrStr += "; Address: " + getStringValue(a.getLine1());addrStr += ", " + getStringValue(a.getLine2());addrStr += "; City: " + getStringValue(a.getCity());addrStr += "; State: " + getStringValue(a.getState());addrStr += "; Pin: " + getStringValue(a.getPin());addrStr += "; Phone: " + getStringValue(a.getPhone());return addrStr;}private String getStringValue(String str) {return str == null ? "" : str.trim();}public static void main(String[] args) {RegisteredUsersGenerator rug = new RegisteredUsersGenerator();try {String userHome = System.getProperty("user.home");FileOutputStream f = new FileOutputStream(userHome + "/registered-users.xls");ByteArrayOutputStream baosXLS = rug.generateRegisteredUsersReport();if(baosXLS == null) {System.out.println("No data");return;}baosXLS.writeTo(f);f.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}System.out.println("Successfully generated the registered users report");}}