Subversion Repositories SmartDukaan

Rev

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