Rev 1514 | Rev 1744 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.support.controllers;import in.shop2020.model.v1.order.LineItem;import in.shop2020.model.v1.order.Order;import in.shop2020.model.v1.order.Transaction;import in.shop2020.model.v1.order.TransactionServiceException;import in.shop2020.model.v1.user.Address;import in.shop2020.model.v1.user.User;import in.shop2020.model.v1.user.UserCommunication;import in.shop2020.model.v1.user.UserContextException;import in.shop2020.model.v1.user.UserContextService.Client;import in.shop2020.payments.Payment;import in.shop2020.thrift.clients.PaymentServiceClient;import in.shop2020.thrift.clients.TransactionServiceClient;import in.shop2020.thrift.clients.UserContextServiceClient;import java.io.BufferedReader;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.security.Principal;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Collections;import java.util.Date;import java.util.Enumeration;import java.util.HashMap;import java.util.List;import java.util.Locale;import java.util.Map;import javax.servlet.RequestDispatcher;import javax.servlet.ServletInputStream;import javax.servlet.ServletOutputStream;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;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.struts2.interceptor.ServletRequestAware;import org.apache.struts2.interceptor.ServletResponseAware;import org.apache.thrift.TException;public class UserOrdersController implements ServletResponseAware, ServletRequestAware{private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("dd/MM/yyyy HH:mm");private HttpServletRequest request;private HttpServletResponse response;private String errorMsg = "";public UserOrdersController(){}@Overridepublic void setServletRequest(HttpServletRequest req) {this.request = req;}@Overridepublic void setServletResponse(HttpServletResponse res) {this.response = res;}public String index() {return "report";}public String show(){return "report";}// Handles the POST request (Form Submission)public String create(){try {//Formatting Form input parametersString email = request.getParameter("email");String orderid = request.getParameter("orderid");TransactionServiceClient transactionServiceClient = new TransactionServiceClient();in.shop2020.model.v1.order.TransactionService.Client transactionClient = transactionServiceClient.getClient();UserContextServiceClient userContextServiceClient = new UserContextServiceClient();Client userClient = userContextServiceClient.getClient();PaymentServiceClient paymentServiceClient = new PaymentServiceClient();in.shop2020.payments.PaymentService.Client paymentClient = paymentServiceClient.getClient();User user = null;if (email != null && !email.isEmpty()) {user = userClient.getUserByEmail(email);if(user.getUserId() == -1){errorMsg = "No user for this id.";return "report";}}else {try {user = userClient.getUserById(transactionClient.getOrder(Long.parseLong(orderid)).getCustomer_id());} catch (NumberFormatException e) {errorMsg = "Order Id should be a number.";return "report";}}if (user == null) {errorMsg = "Could not find user.";return "report";}//Retrieving all user communicationsList<UserCommunication> userCommunications = userClient.getUserCommunicationByUser(user.getUserId());List<UserCommunication> allCommunications = userClient.getAllUserCommunications();//Retrieving all the transactionsList <Transaction> transactions = transactionClient.getTransactionsForCustomer(user.getUserId(), 0, (new Date()).getTime(), null);Collections.reverse(transactions);System.out.println("Total number of Transaction: " + transactions.size());//Retrieving all the paymentsList <Payment> payments = paymentClient.getPaymentsForUser(user.getUserId(), 0,(new Date()).getTime(), null, 0);Map<Long, Payment> txnIdToPaymentMap = new HashMap<Long, Payment>();for(Payment payment : payments) {txnIdToPaymentMap.put(payment.getMerchantTxnId(), payment);}// Preparing XLS file for outputresponse.setContentType("application/vnd.ms-excel");response.setHeader("Content-disposition", "inline; filename=user-orders" + ".xls");ServletOutputStream sos;try {ByteArrayOutputStream baos = getSpreadSheetData(user,transactions, txnIdToPaymentMap, userCommunications,allCommunications, userClient);sos = response.getOutputStream();baos.writeTo(sos);sos.flush();} catch (IOException e) {errorMsg = "Failed to write to response.";e.printStackTrace();}} catch (ParseException e) {errorMsg = e.getMessage();e.printStackTrace();} catch (TransactionServiceException e) {errorMsg = e.getMessage();e.printStackTrace();} catch (Exception e) {errorMsg = e.getMessage();e.printStackTrace();}return "report";}// Prepares the XLS worksheet object and fills in the data with proper formattingprivate ByteArrayOutputStream getSpreadSheetData(User user,List<Transaction> transactions,Map<Long, Payment> txnIdToPaymentMap,List<UserCommunication> userCommunications,List<UserCommunication> allCommunications, Client userClient){ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();Workbook wb = new HSSFWorkbook();Font font = wb.createFont();font.setBoldweight(Font.BOLDWEIGHT_BOLD);CellStyle style = wb.createCellStyle();style.setFont(font);createUserSheet(user, userCommunications, wb, style);createTransactionSheet(transactions, txnIdToPaymentMap, wb, style);createAllUserCommunicationSheet(allCommunications, userClient, wb, style);// Write the workbook to the output streamtry {wb.write(baosXLS);baosXLS.close();} catch (IOException e) {e.printStackTrace();}return baosXLS;}private void createAllUserCommunicationSheet(List<UserCommunication> allCommunications,Client userClient,Workbook wb,CellStyle style){// USER COMMUNICATION SHEETSheet commSheet = wb.createSheet("All Users Communications");short commSerialNo = 0;Row commTitleRow = commSheet.createRow(commSerialNo ++);Cell commTitleCell = commTitleRow.createCell(0);commTitleCell.setCellValue("All Users Communications");commTitleCell.setCellStyle(style);commSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));commSheet.createRow(commSerialNo ++);Row commHeaderRow = commSheet.createRow(commSerialNo++);commHeaderRow.createCell(0).setCellValue("User Email");commHeaderRow.createCell(1).setCellValue("User Name");commHeaderRow.createCell(2).setCellValue("Communication Email");commHeaderRow.createCell(3).setCellValue("Date of Birth");commHeaderRow.createCell(4).setCellValue("Mobile Number");commHeaderRow.createCell(5).setCellValue("User Id");commHeaderRow.createCell(6).setCellValue("Sex");commHeaderRow.createCell(7).setCellValue("Order Id");commHeaderRow.createCell(8).setCellValue("Communication Type");commHeaderRow.createCell(9).setCellValue("AirwayBill No");commHeaderRow.createCell(10).setCellValue("TimeStamp");commHeaderRow.createCell(11).setCellValue("Product Name");commHeaderRow.createCell(12).setCellValue("Reply To");commHeaderRow.createCell(13).setCellValue("Subject");commHeaderRow.createCell(14).setCellValue("Message");for( UserCommunication userComm : allCommunications) {commSerialNo++;Row commContentRow = commSheet.createRow(commSerialNo);try {User user = userClient.getUserById(userComm.getUserId());if (user.getUserId() == -1) {commContentRow.createCell(0).setCellValue(user.getEmail());commContentRow.createCell(1).setCellValue(user.getName());commContentRow.createCell(2).setCellValue(user.getCommunicationEmail());commContentRow.createCell(3).setCellValue(user.getDateOfBirth());commContentRow.createCell(4).setCellValue(user.getMobileNumber());commContentRow.createCell(5).setCellValue(user.getUserId());if (user.getSex() != null) {commContentRow.createCell(6).setCellValue(user.getSex().name());}}} catch (UserContextException e) {e.printStackTrace();} catch (TException e) {e.printStackTrace();}commContentRow.createCell(7).setCellValue(userComm.getOrderId());if (userComm.getCommunicationType() != null) {commContentRow.createCell(8).setCellValue(userComm.getCommunicationType().name());}commContentRow.createCell(9).setCellValue(userComm.getAirwaybillNo());commContentRow.createCell(10).setCellValue(DATE_TIME_FORMAT.format(userComm.getCommunication_timestamp()));commContentRow.createCell(11).setCellValue(userComm.getProductName());commContentRow.createCell(12).setCellValue(userComm.getReplyTo());commContentRow.createCell(13).setCellValue(userComm.getSubject());commContentRow.createCell(14).setCellValue(userComm.getMessage());}}private void createTransactionSheet(List<Transaction> transactions, Map<Long, Payment> txnIdToPaymentMap, Workbook wb, CellStyle style) {// TRANSACTION SHEETSheet transactionSheet = wb.createSheet("Transactions and Payments");short transactionSerialNo = 0;Row orderTitleRow = transactionSheet.createRow(transactionSerialNo ++);Cell orderTitleCell = orderTitleRow.createCell(0);orderTitleCell.setCellValue("User Transactions and Payments");orderTitleCell.setCellStyle(style);transactionSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));transactionSheet.createRow(transactionSerialNo ++);Row orderHeaderRow = transactionSheet.createRow(transactionSerialNo ++);orderHeaderRow.createCell(0).setCellValue("Transaction Id");orderHeaderRow.createCell(1).setCellValue("Transaction Date");orderHeaderRow.createCell(2).setCellValue("Transaction Status");orderHeaderRow.createCell(3).setCellValue("Order Id");orderHeaderRow.createCell(4).setCellValue("Billing Number");orderHeaderRow.createCell(5).setCellValue("Billing Date");orderHeaderRow.createCell(6).setCellValue("Order Status");orderHeaderRow.createCell(7).setCellValue("Brand");orderHeaderRow.createCell(8).setCellValue("Model Name");orderHeaderRow.createCell(9).setCellValue("Model Number");orderHeaderRow.createCell(10).setCellValue("Color");orderHeaderRow.createCell(11).setCellValue("Quantity");orderHeaderRow.createCell(12).setCellValue("Unit Price");orderHeaderRow.createCell(13).setCellValue("Total Price");orderHeaderRow.createCell(14).setCellValue("User Id");orderHeaderRow.createCell(15).setCellValue("Name");orderHeaderRow.createCell(16).setCellValue("Address1");orderHeaderRow.createCell(17).setCellValue("Address2");orderHeaderRow.createCell(17).setCellValue("City");orderHeaderRow.createCell(19).setCellValue("State");orderHeaderRow.createCell(20).setCellValue("Pin Code");orderHeaderRow.createCell(21).setCellValue("Mobile Number");orderHeaderRow.createCell(22).setCellValue("email");orderHeaderRow.createCell(23).setCellValue("Airway Bill No.");orderHeaderRow.createCell(24).setCellValue("Billed By");orderHeaderRow.createCell(25).setCellValue("Receiver");orderHeaderRow.createCell(26).setCellValue("Tracking Id");orderHeaderRow.createCell(27).setCellValue("Accepted Timestamp");orderHeaderRow.createCell(28).setCellValue("Delivery Timestamp");orderHeaderRow.createCell(29).setCellValue("Expected Delivery Time");orderHeaderRow.createCell(30).setCellValue("Payment Id");orderHeaderRow.createCell(31).setCellValue("Payment Status");orderHeaderRow.createCell(32).setCellValue("Gateway Payment Id");orderHeaderRow.createCell(33).setCellValue("Gateway Transaction Date");orderHeaderRow.createCell(34).setCellValue("Gateway Txn Id");orderHeaderRow.createCell(35).setCellValue("Gateway Txn Status");orderHeaderRow.createCell(36).setCellValue("Reference Code");orderHeaderRow.createCell(37).setCellValue("Gateway Id");orderHeaderRow.createCell(38).setCellValue("Amount");orderHeaderRow.createCell(39).setCellValue("Description");orderHeaderRow.createCell(40).setCellValue("Auth Code");orderHeaderRow.createCell(41).setCellValue("Error Code");for(Transaction transaction : transactions) {List<Order> orders = transaction.getOrders();Date transactionDate = new Date(transaction.getCreatedOn());long transactionId = transaction.getId();String transactionStatus = transaction.getStatusDescription();for(Order order : orders) {transactionSerialNo ++;Row contentRow = transactionSheet.createRow(transactionSerialNo);LineItem lineItem = order.getLineitems().get(0);Payment payment = txnIdToPaymentMap.get(transactionId);contentRow.createCell(0).setCellValue(transactionId);contentRow.createCell(1).setCellValue(DATE_TIME_FORMAT.format(transactionDate));contentRow.createCell(2).setCellValue(transactionStatus);contentRow.createCell(3).setCellValue(order.getId());contentRow.createCell(4).setCellValue(order.getInvoice_number());contentRow.createCell(5).setCellValue(DATE_TIME_FORMAT.format(new Date(order.getBilling_timestamp())));contentRow.createCell(6).setCellValue(order.getStatusDescription());contentRow.createCell(7).setCellValue(getValueForEmptyString(lineItem.getBrand()));contentRow.createCell(8).setCellValue(getValueForEmptyString(lineItem.getModel_name()));contentRow.createCell(9).setCellValue(getValueForEmptyString(lineItem.getModel_number()));contentRow.createCell(10).setCellValue(getValueForEmptyString(lineItem.getColor()));contentRow.createCell(11).setCellValue(lineItem.getQuantity());contentRow.createCell(12).setCellValue(lineItem.getUnit_price());contentRow.createCell(13).setCellValue(lineItem.getTotal_price());contentRow.createCell(14).setCellValue(order.getCustomer_id());contentRow.createCell(15).setCellValue(order.getCustomer_name());contentRow.createCell(16).setCellValue(order.getCustomer_address1());contentRow.createCell(17).setCellValue(order.getCustomer_address2());contentRow.createCell(18).setCellValue(order.getCustomer_city());contentRow.createCell(19).setCellValue(order.getCustomer_state());contentRow.createCell(20).setCellValue(order.getCustomer_pincode());contentRow.createCell(21).setCellValue(order.getCustomer_mobilenumber());contentRow.createCell(22).setCellValue(order.getCustomer_email());contentRow.createCell(23).setCellValue(order.getAirwaybill_no());contentRow.createCell(24).setCellValue(order.getBilled_by());contentRow.createCell(25).setCellValue(order.getReceiver());contentRow.createCell(26).setCellValue(order.getTracking_id());contentRow.createCell(27).setCellValue(DATE_TIME_FORMAT.format(new Date(order.getAccepted_timestamp())));contentRow.createCell(28).setCellValue(DATE_TIME_FORMAT.format(new Date(order.getDelivery_timestamp())));contentRow.createCell(29).setCellValue(DATE_TIME_FORMAT.format(new Date(order.getExpected_delivery_time())));if (payment != null) {contentRow.createCell(30).setCellValue(payment.getPaymentId());contentRow.createCell(31).setCellValue(payment.getStatus().name());contentRow.createCell(32).setCellValue(payment.getGatewayPaymentId());contentRow.createCell(33).setCellValue(payment.getGatewayTxnDate());contentRow.createCell(34).setCellValue(payment.getGatewayTxnId());contentRow.createCell(35).setCellValue(payment.getGatewayTxnStatus());contentRow.createCell(36).setCellValue(payment.getReferenceCode());contentRow.createCell(37).setCellValue(payment.getGatewayId());contentRow.createCell(38).setCellValue(payment.getAmount());contentRow.createCell(39).setCellValue(payment.getDescription());contentRow.createCell(40).setCellValue(payment.getAuthCode());contentRow.createCell(41).setCellValue(payment.getErrorCode());}}}}private String getValueForEmptyString(String s){if(s==null || s.equals(""))return "-";elsereturn s;}private void createUserSheet(User user, List<UserCommunication> userCommunications, Workbook wb, CellStyle style) {Sheet userSheet = wb.createSheet("User");short userSerialNo = 0;// Create the header row and put all the titles in it. Rows are 0 based.Row titleRow = userSheet.createRow(userSerialNo++);Cell titleCell = titleRow.createCell(0);titleCell.setCellValue("User Details");titleCell.setCellStyle(style);userSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));Row userHeaderRow = userSheet.createRow(userSerialNo++);userHeaderRow.createCell(0).setCellValue("Name");userHeaderRow.createCell(1).setCellValue("Email");userHeaderRow.createCell(2).setCellValue("Communication Email");userHeaderRow.createCell(3).setCellValue("Date Of Birth");userHeaderRow.createCell(4).setCellValue("Mobile Number");userHeaderRow.createCell(5).setCellValue("Sex");userHeaderRow.createCell(6).setCellValue("User Id");Row userContentRow = userSheet.createRow(userSerialNo++);userContentRow.createCell(0).setCellValue(user.getName());userContentRow.createCell(1).setCellValue(user.getEmail());userContentRow.createCell(2).setCellValue(user.getCommunicationEmail());userContentRow.createCell(3).setCellValue(user.getDateOfBirth());userContentRow.createCell(4).setCellValue(user.getMobileNumber());userContentRow.createCell(5).setCellValue(user.getSex().name());userContentRow.createCell(6).setCellValue(user.getUserId());userSerialNo+=2;Row addressHeaderRow = userSheet.createRow(userSerialNo++);addressHeaderRow.createCell(0).setCellValue("Name");addressHeaderRow.createCell(1).setCellValue("Line1");addressHeaderRow.createCell(2).setCellValue("Line2");addressHeaderRow.createCell(3).setCellValue("City");addressHeaderRow.createCell(4).setCellValue("State");addressHeaderRow.createCell(5).setCellValue("Pincode");addressHeaderRow.createCell(6).setCellValue("Phone");List<Address> user_addresses = user.getAddresses();if (user_addresses != null && !user_addresses.isEmpty()) {for (Address address : user.getAddresses()) {if (user.getDefaultAddressId() == address.getId()) {userContentRow = userSheet.createRow(userSerialNo);userSheet.addMergedRegion(new CellRangeAddress(userSerialNo, userSerialNo, 0, 6));userContentRow.createCell(0).setCellValue("Primary Address");userSerialNo++;userContentRow = userSheet.createRow(userSerialNo);userContentRow.createCell(0).setCellValue(address.getName());userContentRow.createCell(1).setCellValue(address.getLine1());userContentRow.createCell(2).setCellValue(address.getLine2());userContentRow.createCell(3).setCellValue(address.getCity());userContentRow.createCell(4).setCellValue(address.getState());userContentRow.createCell(5).setCellValue(address.getPin());userContentRow.createCell(6).setCellValue(address.getPhone());userSerialNo += 3;}}userContentRow = userSheet.createRow(userSerialNo);userSheet.addMergedRegion(new CellRangeAddress(userSerialNo,userSerialNo, 0, 6));userContentRow.createCell(0).setCellValue("Other Addresses");for (Address address : user.getAddresses()) {if (user.getDefaultAddressId() != address.getId()) {userSerialNo++;userContentRow = userSheet.createRow(userSerialNo);userContentRow.createCell(0).setCellValue(address.getName());userContentRow.createCell(1).setCellValue(address.getLine1());userContentRow.createCell(2).setCellValue(address.getLine2());userContentRow.createCell(3).setCellValue(address.getCity());userContentRow.createCell(4).setCellValue(address.getState());userContentRow.createCell(5).setCellValue(address.getPin());userContentRow.createCell(6).setCellValue(address.getPhone());}}userSerialNo+=2;userContentRow = userSheet.createRow(userSerialNo);userSheet.addMergedRegion(new CellRangeAddress(userSerialNo,userSerialNo, 0, 6));userContentRow.createCell(0).setCellValue("User Communication");userSerialNo++;Row commHeaderRow = userSheet.createRow(userSerialNo);commHeaderRow.createCell(0).setCellValue("Order Id");commHeaderRow.createCell(1).setCellValue("Communication Type");commHeaderRow.createCell(2).setCellValue("AirwayBill No");commHeaderRow.createCell(3).setCellValue("TimeStamp");commHeaderRow.createCell(4).setCellValue("Product Name");commHeaderRow.createCell(5).setCellValue("Reply To");commHeaderRow.createCell(6).setCellValue("Subject");commHeaderRow.createCell(7).setCellValue("Message");for( UserCommunication userComm : userCommunications) {userSerialNo++;userContentRow = userSheet.createRow(userSerialNo);userContentRow.createCell(0).setCellValue(userComm.getOrderId());if (userComm.getCommunicationType() != null) {userContentRow.createCell(1).setCellValue(userComm.getCommunicationType().name());}userContentRow.createCell(2).setCellValue(userComm.getAirwaybillNo());userContentRow.createCell(3).setCellValue(DATE_TIME_FORMAT.format(userComm.getCommunication_timestamp()));userContentRow.createCell(4).setCellValue(userComm.getProductName());userContentRow.createCell(5).setCellValue(userComm.getReplyTo());userContentRow.createCell(6).setCellValue(userComm.getSubject());userContentRow.createCell(7).setCellValue(userComm.getMessage());}}}public String getErrorMsg() {return errorMsg;}}