Subversion Repositories SmartDukaan

Rev

Rev 1684 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1676 ankur.sing 1
package in.shop2020.support.services;
2
 
3
import in.shop2020.model.v1.user.Address;
4
import in.shop2020.model.v1.user.User;
5
import in.shop2020.model.v1.user.UserType;
6
import in.shop2020.thrift.clients.UserContextServiceClient;
7
 
8
import java.io.ByteArrayOutputStream;
9
import java.io.FileNotFoundException;
10
import java.io.FileOutputStream;
11
import java.io.IOException;
12
import java.util.Calendar;
13
import java.util.List;
14
 
15
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
16
import org.apache.poi.ss.usermodel.Cell;
17
import org.apache.poi.ss.usermodel.CellStyle;
18
import org.apache.poi.ss.usermodel.Font;
19
import org.apache.poi.ss.usermodel.Row;
20
import org.apache.poi.ss.usermodel.Sheet;
21
import org.apache.poi.ss.usermodel.Workbook;
22
import org.apache.poi.ss.util.CellRangeAddress;
23
import org.apache.thrift.TException;
24
 
25
public class RegisteredUsersGenerator {
26
    UserContextServiceClient usc;
27
    in.shop2020.model.v1.user.UserContextService.Client uClient;
28
 
29
    public RegisteredUsersGenerator() {
30
        try {
31
            usc = new UserContextServiceClient();
32
            uClient = usc.getClient();
33
        } catch (Exception e) {
34
            e.printStackTrace();
35
        }
36
    }
37
 
38
    /**
39
     * This method is used in RegisteredUsersController. 
40
     * If any registered user(s) exist(s), then returns ByteArrayOutputStream containing user(s) data,
41
     * otherwise returns null
42
     * @param startDate -- inclusive
43
     * @param endDate -- inclusive
44
     * @return
45
     */
46
    public ByteArrayOutputStream generateRegisteredUsersReport() {
47
        List<User> users = null;
48
        try {
49
            users = uClient.getAllUsers(UserType.USER);
50
        } catch (TException e) {
51
            // TODO Auto-generated catch block
52
            e.printStackTrace();
53
        }
54
        if(users == null || users.isEmpty()) {
55
            return null;
56
        }
57
        // Preparing XLS file for output
58
        return getSpreadSheetData(users);
59
 
60
    }
61
 
62
    // Prepares the XLS worksheet object and fills in the data with proper
63
    // formatting
64
    private ByteArrayOutputStream getSpreadSheetData(List<User> users) {
65
        ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
66
 
67
        Workbook wb = new HSSFWorkbook();
68
 
69
        Font font = wb.createFont();
70
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
71
        CellStyle style = wb.createCellStyle();
72
        style.setFont(font);
73
 
74
        CellStyle styleWT = wb.createCellStyle();
75
        styleWT.setWrapText(true);
76
 
77
        Sheet userSheet = wb.createSheet("User");
78
        short userSerialNo = 0;
79
 
80
        Row titleRow = userSheet.createRow(userSerialNo++);
81
        Cell titleCell = titleRow.createCell(0);
82
        titleCell.setCellValue("Registered Users");
83
        titleCell.setCellStyle(style);
84
 
85
        userSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
86
 
87
        userSheet.createRow(userSerialNo++);
88
 
89
        Row headerRow = userSheet.createRow(userSerialNo++);
90
        headerRow.createCell(0).setCellValue("User Id");
91
        headerRow.createCell(1).setCellValue("Email");
92
        headerRow.createCell(2).setCellValue("Name");
93
        headerRow.createCell(3).setCellValue("Communication Email");
94
        headerRow.createCell(4).setCellValue("Date of Birth");
95
        headerRow.createCell(5).setCellValue("Sex");
96
        headerRow.createCell(6).setCellValue("Mobile");
97
        headerRow.createCell(7).setCellValue("Default Address");
98
 
99
        int addrColWidth = 40;
100
        userSheet.setColumnWidth(7, 256 * addrColWidth); // set width of address column to 40 characters
101
 
102
        Calendar calendar = Calendar.getInstance();
103
        Row contentRow;
104
        float defaultRowHeight = userSheet.getDefaultRowHeightInPoints();
105
        for (User u : users) {
106
            userSerialNo++;
107
            contentRow = userSheet.createRow(userSerialNo);
108
            contentRow.createCell(0).setCellValue(u.getUserId());
109
            contentRow.createCell(1).setCellValue(u.getEmail());
110
            contentRow.createCell(2).setCellValue(u.getName());
111
            contentRow.createCell(3).setCellValue(u.getCommunicationEmail());
112
            contentRow.createCell(4).setCellValue(u.getDateOfBirth());
113
            // calendar.setTimeInMillis
114
            contentRow.createCell(5).setCellValue(u.getSex().name());
115
            contentRow.createCell(6).setCellValue(u.getMobileNumber());
116
 
117
            List<Address> addresses = u.getAddresses();
118
            if(addresses == null || addresses.isEmpty()) {
119
                continue;
120
            }
121
            int i = 0, col, defaultAddrCol = 7, maxAddrLength = 0, len;
122
            String addressString;
123
            for(Address a : addresses) {
124
                if(a.getId() == u.getDefaultAddressId()) {
125
                    col = defaultAddrCol;
126
                } else {
127
                    i++;
128
                    col = defaultAddrCol + i;
129
                    headerRow.createCell(col).setCellValue("Address " + i);
130
                }
131
                userSheet.setColumnWidth(col, addrColWidth * 256);
132
                addressString = generateAddressString(a);
133
                contentRow.createCell(col).setCellValue(addressString);
134
                contentRow.getCell(col).setCellStyle(styleWT);
135
                len = addressString.length();
136
                if(len > maxAddrLength)
137
                    maxAddrLength = len;
138
            }
139
            contentRow.setHeightInPoints((maxAddrLength / addrColWidth + 1) * defaultRowHeight); // Setting Row Height
140
        }
141
        for (int i = 0; i <= 6; i++) {
142
            userSheet.autoSizeColumn(i);
143
        }
144
 
145
        // Write the workbook to the output stream
146
        try {
147
            wb.write(baosXLS);
148
            baosXLS.close();
149
        } catch (IOException e) {
150
            e.printStackTrace();
151
        }
152
        return baosXLS;
153
    }
154
 
155
    private String generateAddressString(Address a) {
156
        String addrStr = "";
157
        addrStr += "Name: " + getStringValue(a.getName());
158
        addrStr += "; Address: " + getStringValue(a.getLine1());
159
        addrStr += ", " + getStringValue(a.getLine2());
160
        addrStr += "; City: " + getStringValue(a.getCity());
161
        addrStr += "; State: " + getStringValue(a.getState());
162
        addrStr += "; Pin: " + getStringValue(a.getPin());
163
        addrStr += "; Phone: " + getStringValue(a.getPhone());
164
        return addrStr;
165
    }
166
 
167
    private String getStringValue(String str) {
168
        return str == null ? "" : str.trim();
169
    }
170
 
171
    public static void main(String[] args) {
172
        RegisteredUsersGenerator rug = new RegisteredUsersGenerator();
173
        try {
174
            String userHome = System.getProperty("user.home");
175
            FileOutputStream f = new FileOutputStream(userHome + "/registered-users.xls");
176
            ByteArrayOutputStream baosXLS = rug.generateRegisteredUsersReport();
177
            if(baosXLS == null) {
178
                System.out.println("No data");
179
                return;
180
            }
181
            baosXLS.writeTo(f);
182
            f.close();
183
        } catch (FileNotFoundException e) {
184
            e.printStackTrace();
185
        } catch (IOException e) {
186
            e.printStackTrace();
187
        }
188
        System.out.println("Successfully generated the registered users report");
189
    }
190
}