Subversion Repositories SmartDukaan

Rev

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