Subversion Repositories SmartDukaan

Rev

Rev 2042 | Rev 3213 | 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
 
1879 ankur.sing 3
import in.shop2020.model.v1.order.Order;
4
import in.shop2020.model.v1.order.TransactionServiceException;
1676 ankur.sing 5
import in.shop2020.model.v1.user.Address;
1879 ankur.sing 6
import in.shop2020.model.v1.user.Cart;
7
import in.shop2020.model.v1.user.Line;
8
import in.shop2020.model.v1.user.ShoppingCartException;
1676 ankur.sing 9
import in.shop2020.model.v1.user.User;
1684 ankur.sing 10
import in.shop2020.model.v1.user.UserContextException;
11
import in.shop2020.model.v1.user.UserState;
1676 ankur.sing 12
import in.shop2020.model.v1.user.UserType;
3125 rajveer 13
import in.shop2020.thrift.clients.TransactionClient;
14
import in.shop2020.thrift.clients.UserClient;
1676 ankur.sing 15
 
16
import java.io.ByteArrayOutputStream;
17
import java.io.FileNotFoundException;
18
import java.io.FileOutputStream;
19
import java.io.IOException;
1684 ankur.sing 20
import java.text.DateFormat;
21
import java.text.SimpleDateFormat;
1676 ankur.sing 22
import java.util.Calendar;
1879 ankur.sing 23
import java.util.Date;
1676 ankur.sing 24
import java.util.List;
25
 
26
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
27
import org.apache.poi.ss.usermodel.Cell;
28
import org.apache.poi.ss.usermodel.CellStyle;
29
import org.apache.poi.ss.usermodel.Font;
30
import org.apache.poi.ss.usermodel.Row;
31
import org.apache.poi.ss.usermodel.Sheet;
32
import org.apache.poi.ss.usermodel.Workbook;
33
import org.apache.poi.ss.util.CellRangeAddress;
34
import org.apache.thrift.TException;
35
 
36
public class RegisteredUsersGenerator {
3125 rajveer 37
	UserClient usc;
1676 ankur.sing 38
    in.shop2020.model.v1.user.UserContextService.Client uClient;
1879 ankur.sing 39
 
3125 rajveer 40
    TransactionClient tsc;
1879 ankur.sing 41
    in.shop2020.model.v1.order.TransactionService.Client tClient;
42
 
1676 ankur.sing 43
    public RegisteredUsersGenerator() {
44
        try {
3125 rajveer 45
            usc = new UserClient();
1676 ankur.sing 46
            uClient = usc.getClient();
1879 ankur.sing 47
 
3125 rajveer 48
            tsc = new TransactionClient();
1879 ankur.sing 49
            tClient = tsc.getClient();
1676 ankur.sing 50
        } catch (Exception e) {
51
            e.printStackTrace();
52
        }
53
    }
54
 
55
    /**
56
     * This method is used in RegisteredUsersController. 
57
     * If any registered user(s) exist(s), then returns ByteArrayOutputStream containing user(s) data,
58
     * otherwise returns null
59
     * @param startDate -- inclusive
60
     * @param endDate -- inclusive
61
     * @return
62
     */
63
    public ByteArrayOutputStream generateRegisteredUsersReport() {
64
        List<User> users = null;
65
        try {
1891 ankur.sing 66
            users = uClient.getAllUsers(UserType.USER, -1, -1);
1676 ankur.sing 67
        } catch (TException e) {
68
            e.printStackTrace();
69
        }
70
        if(users == null || users.isEmpty()) {
71
            return null;
72
        }
73
        // Preparing XLS file for output
74
        return getSpreadSheetData(users);
75
 
76
    }
77
 
78
    // Prepares the XLS worksheet object and fills in the data with proper
79
    // formatting
80
    private ByteArrayOutputStream getSpreadSheetData(List<User> users) {
81
        ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
82
 
83
        Workbook wb = new HSSFWorkbook();
84
 
85
        Font font = wb.createFont();
86
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
87
        CellStyle style = wb.createCellStyle();
88
        style.setFont(font);
89
 
90
        CellStyle styleWT = wb.createCellStyle();
91
        styleWT.setWrapText(true);
92
 
93
        Sheet userSheet = wb.createSheet("User");
94
        short userSerialNo = 0;
95
 
96
        Row titleRow = userSheet.createRow(userSerialNo++);
97
        Cell titleCell = titleRow.createCell(0);
98
        titleCell.setCellValue("Registered Users");
99
        titleCell.setCellStyle(style);
100
 
101
        userSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
102
 
103
        Row headerRow = userSheet.createRow(userSerialNo++);
104
        headerRow.createCell(0).setCellValue("User Id");
105
        headerRow.createCell(1).setCellValue("Email");
106
        headerRow.createCell(2).setCellValue("Name");
107
        headerRow.createCell(3).setCellValue("Communication Email");
108
        headerRow.createCell(4).setCellValue("Date of Birth");
109
        headerRow.createCell(5).setCellValue("Sex");
110
        headerRow.createCell(6).setCellValue("Mobile");
1684 ankur.sing 111
        headerRow.createCell(7).setCellValue("Registered On");
112
        headerRow.createCell(8).setCellValue("Last Login");
1879 ankur.sing 113
        headerRow.createCell(9).setCellValue("Successful Orders");
114
        headerRow.createCell(10).setCellValue("Pending/Failed Orders");
115
        headerRow.createCell(11).setCellValue("Items in Cart");
116
        userSheet.setColumnWidth(11, 20 * 256); // set width of items in cart column to 20 chars
1676 ankur.sing 117
 
118
        Calendar calendar = Calendar.getInstance();
119
        Row contentRow;
120
        float defaultRowHeight = userSheet.getDefaultRowHeightInPoints();
1684 ankur.sing 121
        UserState uState;
122
        DateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm a");
1879 ankur.sing 123
        Date currentDate = new Date();
1676 ankur.sing 124
        for (User u : users) {
125
            userSerialNo++;
126
            contentRow = userSheet.createRow(userSerialNo);
127
            contentRow.createCell(0).setCellValue(u.getUserId());
128
            contentRow.createCell(1).setCellValue(u.getEmail());
129
            contentRow.createCell(2).setCellValue(u.getName());
130
            contentRow.createCell(3).setCellValue(u.getCommunicationEmail());
131
            contentRow.createCell(4).setCellValue(u.getDateOfBirth());
132
            // calendar.setTimeInMillis
133
            contentRow.createCell(5).setCellValue(u.getSex().name());
134
            contentRow.createCell(6).setCellValue(u.getMobileNumber());
135
 
1879 ankur.sing 136
 
137
 
1684 ankur.sing 138
            try {
139
                uState = uClient.getUserState(u.getUserId());
140
                calendar.setTimeInMillis(uState.getActiveSince());
141
                contentRow.createCell(7).setCellValue(formatter.format(calendar.getTime()));
142
                calendar.setTimeInMillis(uState.getLastLogin());
143
                contentRow.createCell(8).setCellValue(formatter.format(calendar.getTime()));
144
            } catch (UserContextException e) {
145
                e.printStackTrace();
146
            } catch (TException e) {
147
                e.printStackTrace();
148
            }
1879 ankur.sing 149
 
150
            try {
151
                List<Order> orders = tClient.getOrdersForCustomer(u.getUserId(), 0, currentDate.getTime(), null);
152
                int noOfFailedOrders = 0, noOfValidOrders = 0;
153
                for(Order order : orders) {
154
                    if(order.getStatus().getValue() < 3) {
155
                        noOfFailedOrders++;
156
                    } else {
157
                        noOfValidOrders++;
158
                    }
159
                }
160
                contentRow.createCell(9).setCellValue(noOfValidOrders);
161
                contentRow.createCell(10).setCellValue(noOfFailedOrders);
162
            } catch (TransactionServiceException e) {
163
                e.printStackTrace();
164
            } catch (TException e) {
165
                e.printStackTrace();
166
            }
167
 
168
            String itemList = getCartItems(u.getUserId());
169
            contentRow.createCell(11).setCellValue(itemList);
170
            contentRow.getCell(11).setCellStyle(styleWT);
1684 ankur.sing 171
 
1676 ankur.sing 172
            List<Address> addresses = u.getAddresses();
173
            if(addresses == null || addresses.isEmpty()) {
174
                continue;
175
            }
1879 ankur.sing 176
            int i = 0, col, defaultAddrCol = 12, maxAddrLength = 0, len, addrColWidth = 40;
1676 ankur.sing 177
            String addressString;
178
            for(Address a : addresses) {
179
                if(a.getId() == u.getDefaultAddressId()) {
180
                    col = defaultAddrCol;
1684 ankur.sing 181
                    headerRow.createCell(col).setCellValue("Default Address");
1676 ankur.sing 182
                } else {
183
                    i++;
184
                    col = defaultAddrCol + i;
185
                    headerRow.createCell(col).setCellValue("Address " + i);
186
                }
1684 ankur.sing 187
                userSheet.setColumnWidth(col, addrColWidth * 256);  // set width of address column to 40 characters
1676 ankur.sing 188
                addressString = generateAddressString(a);
189
                contentRow.createCell(col).setCellValue(addressString);
190
                contentRow.getCell(col).setCellStyle(styleWT);
191
                len = addressString.length();
192
                if(len > maxAddrLength)
193
                    maxAddrLength = len;
194
            }
1879 ankur.sing 195
            if((len = itemList.length()) > maxAddrLength) {
196
                maxAddrLength = len;
197
            }
1676 ankur.sing 198
            contentRow.setHeightInPoints((maxAddrLength / addrColWidth + 1) * defaultRowHeight); // Setting Row Height
199
        }
1879 ankur.sing 200
        for (int i = 0; i <= 10; i++) {
1676 ankur.sing 201
            userSheet.autoSizeColumn(i);
202
        }
203
 
204
        // Write the workbook to the output stream
205
        try {
206
            wb.write(baosXLS);
207
            baosXLS.close();
208
        } catch (IOException e) {
209
            e.printStackTrace();
210
        }
211
        return baosXLS;
212
    }
213
 
1879 ankur.sing 214
    private String getCartItems(long userId) {
215
        String itemList = "";
216
        try {
217
            Cart cart = uClient.getCurrentCart(userId);
218
            if(cart == null) {
219
                return "";
220
            }
221
            List<Line> lines = cart.getLines();
222
            if(lines == null || lines.isEmpty()) {
223
                return "";
224
            }
225
            boolean firstItem = true;
226
            for(Line line : lines) {
227
                if(firstItem) {
228
                    firstItem = false;
229
                    itemList += line.getItemId();
230
                    continue;
231
                }
232
                itemList += ", " + line.getItemId();;
233
            }
234
        } catch (ShoppingCartException e) {
235
            e.printStackTrace();
236
        } catch (TException e) {
237
            e.printStackTrace();
238
        }
239
        return itemList;
240
    }
241
 
1676 ankur.sing 242
    private String generateAddressString(Address a) {
243
        String addrStr = "";
244
        addrStr += "Name: " + getStringValue(a.getName());
245
        addrStr += "; Address: " + getStringValue(a.getLine1());
246
        addrStr += ", " + getStringValue(a.getLine2());
247
        addrStr += "; City: " + getStringValue(a.getCity());
248
        addrStr += "; State: " + getStringValue(a.getState());
249
        addrStr += "; Pin: " + getStringValue(a.getPin());
250
        addrStr += "; Phone: " + getStringValue(a.getPhone());
251
        return addrStr;
252
    }
253
 
254
    private String getStringValue(String str) {
255
        return str == null ? "" : str.trim();
256
    }
257
 
258
    public static void main(String[] args) {
259
        RegisteredUsersGenerator rug = new RegisteredUsersGenerator();
260
        try {
261
            String userHome = System.getProperty("user.home");
262
            FileOutputStream f = new FileOutputStream(userHome + "/registered-users.xls");
263
            ByteArrayOutputStream baosXLS = rug.generateRegisteredUsersReport();
264
            if(baosXLS == null) {
265
                System.out.println("No data");
266
                return;
267
            }
268
            baosXLS.writeTo(f);
269
            f.close();
270
        } catch (FileNotFoundException e) {
271
            e.printStackTrace();
272
        } catch (IOException e) {
273
            e.printStackTrace();
274
        }
275
        System.out.println("Successfully generated the registered users report");
276
    }
277
}