public ArrayList<ExcelErrorDetails> saveCustomerList(List<CustomerDirectory> customerList) throws Exception { CustomerDirectory customerDAOResult = null; UploadFileUtility upUltil = new UploadFileUtility(); int i = 1; ArrayList<ExcelErrorDetails> customerDAOErrorList = new ArrayList<ExcelErrorDetails>(); for (CustomerDirectory customerDirectoryObj : customerList) { customerDAOResult = null; try { customerDAOResult = customerDetailsDAO.saveCustomer(customerDirectoryObj); } catch (Exception e) { e.printStackTrace(); } if (customerDAOResult == null) { customerDAOErrorList.add( upUltil.getExcelErrorDetails(i, -1, "Error in saving to database")); } i++; } return customerDAOErrorList; }
@SuppressWarnings("unchecked") @Override public HashMap<String, Object> uploadFile( MultipartFile fileUploaded, HttpSession session, int confirmUploadId) throws Exception { // confirmUploadId- Default value -1, it implies that Excel is being for // the first time, If 1 It means user accepts that even if problem // exists in present excel, continue with Save operation. HashMap<String, Object> resultMap = new HashMap<String, Object>(); Utility util = new Utility(); ArrayList<ExcelErrorDetails> errorList = new ArrayList<ExcelErrorDetails>(); ArrayList<CustomerDirectory> customerList = new ArrayList<CustomerDirectory>(); UploadFileUtility upUltil = new UploadFileUtility(); CustomerDirectoryHelper customerDirectoryHelper = new CustomerDirectoryHelper(); boolean hasErrorOccured = false; boolean isValidSchema; int i; UserDetail userDetail = (UserDetail) session.getAttribute("selectedUser"); if (!fileUploaded.isEmpty()) { Workbook workBook = upUltil.readExcelFileFromMultipart(fileUploaded); if (workBook == null) { return upUltil.getErrorMessage(ConstantUtil.ERROR_FILE_READING_ERROR); } // Schema Validation - Checks Whether Row header name is same as we // specified. // Validate schema only when the document is uploaded and not when // user sends confirmation if (confirmUploadId != 1) { isValidSchema = upUltil.isSchemaValid( workBook, ConstantUtil.CUSTOMER_DIRECTORY_SHEETNUM, ConstantUtil.CUSTOMER_DIRECTORY_HEADER_ROWNUM, ConstantUtil.CUSTOMER_EXCEL_FORMAT); if (!isValidSchema) { return upUltil.getErrorMessage(ConstantUtil.ERROR_HEADER_VALIDATION_ERROR); } } // Schema Validation Ends // Check Validation For Confirmation from user-- Pending // Data Validation Starts Sheet sheet = workBook.getSheetAt(ConstantUtil.CUSTOMER_DIRECTORY_SHEETNUM); for (i = 1; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); if (row == null) { continue; } Map<String, Object> rowValidationResult = new HashMap<String, Object>(); // if (confirmUploadId != 1) { rowValidationResult = customerDirectoryHelper.validateRowDataAndFetchBean(row, userDetail); if (rowValidationResult.get("customerDirectoryBean") == null) { errorList.addAll( (Collection<? extends ExcelErrorDetails>) rowValidationResult.get("errorList")); hasErrorOccured = true; } else { customerList.add((CustomerDirectory) rowValidationResult.get("customerDirectoryBean")); } // } } if ((confirmUploadId == 1) || (confirmUploadId != 1 && !hasErrorOccured)) { errorList = saveCustomerList(customerList); if (errorList.isEmpty()) { errorList = null; } return util.responseBuilder(errorList); } else { return util.responseBuilder(errorList); } } return resultMap; }