CsvBulkExportServiceImpl extends AbstractExportService<CsvExportDescriptor> implements ExportService { @Override protected void doExport(final JobStatusListener statusListener, final String csvExportDescriptorName, final CsvExportDescriptor csvExportDescriptor, final String fileToExport) throws Exception { statusListener.notifyMessage("export file : {}", fileToExport); CsvFileWriter csvFileWriter = new CsvFileWriterImpl(); ResultsIterator<Object> results = null; try { final String filename = fileToExport; final List<String> headers = new ArrayList<>(csvExportDescriptor.getColumns().size()); for (final CsvExportColumn column : csvExportDescriptor.getColumns()) { headers.add(column.getColumnHeader()); } csvFileWriter.open( filename, headers.toArray(new String[headers.size()]), csvExportDescriptor.getExportFileDescriptor().getColumnDelimiter(), csvExportDescriptor.getExportFileDescriptor().getTextQualifier(), csvExportDescriptor.getExportFileDescriptor().getLineEnd(), csvExportDescriptor.getExportFileDescriptor().getFileEncoding(), csvExportDescriptor.getExportFileDescriptor().isPrintHeader()); results = getExistingEntities(csvExportDescriptor, csvExportDescriptor.getSelectCmd(), null, null); while (results.hasNext()) { final Object entity = results.next(); final CsvExportTuple tuple = new CsvExportTupleImpl(entity); csvFileWriter.writeLine(doExportTuple(statusListener, tuple, csvExportDescriptorName, csvExportDescriptor, null)); releaseEntity(entity); } final int lines = (csvExportDescriptor.getExportFileDescriptor().isPrintHeader() ? csvFileWriter.getRowsWritten() - 1 : csvFileWriter.getRowsWritten()); statusListener.count("total exported data lines", lines); statusListener.notifyMessage("total data lines : {}", lines); } catch (UnsupportedEncodingException e) { statusListener.notifyError("wrong file encoding in xml descriptor : {} {}", e, csvExportDescriptor.getExportFileDescriptor().getFileEncoding(), e.getMessage()); } catch (IOException e) { statusListener.notifyError("cannot write the csv file : {} {}", e, fileToExport, e.getMessage()); } finally { try { if (results != null) { results.close(); } } catch (Exception exp) { statusListener.notifyError("cannot close the csv resultset : {} {}", exp, fileToExport, exp.getMessage()); } try { csvFileWriter.close(); } catch (IOException ioe) { statusListener.notifyError("cannot close the csv file : {} {}", ioe, fileToExport, ioe.getMessage()); } } } CsvBulkExportServiceImpl(final FederationFacade federationFacade); void setGenericDAO(GenericDAO<Object, Long> genericDAO); void setValueDataAdapter(final CsvValueAdapter valueDataAdapter); void setValueLanguageAdapter(final CsvValueAdapter valueLanguageAdapter); void setColumnLookUpQueryParameterStrategy(final LookUpQueryParameterStrategy<CsvExportDescriptor, CsvExportTuple, CsvValueAdapter> columnLookUpQueryParameterStrategy);  }
@Test public void testDoExport() throws Exception { try { final JobStatusListener listener = mockery.mock(JobStatusListener.class, "listener"); mockery.checking(new Expectations() {{ allowing(listener).notifyPing(); allowing(listener).notifyPing(with(any(String.class)), with(any(Object[].class))); allowing(listener).notifyMessage(with(any(String.class)), with(any(Object[].class))); allowing(listener).count(with(any(String.class)), with(any(Integer.class))); }}); Set<String> importedFilesSet = new HashSet<>(); ResultSet rs; rs = getConnection().getConnection().createStatement().executeQuery ( "select count(a.ATTRIBUTE_ID) as cnt from TATTRIBUTE a where a.ATTRIBUTEGROUP = 'PRODUCT'"); rs.next(); long cntProductAttr = rs.getLong("cnt"); rs.close(); long dt = System.currentTimeMillis(); String fileToExport = "target/attributenames-export-" + UUID.randomUUID().toString() + ".csv"; bulkExportService.doExport(createContext("src/test/resources/export/csv/attributenames.xml", listener, fileToExport)); final long attrs = System.currentTimeMillis() - dt; System.out.println(String.format("%5d", cntProductAttr) + " attributes in " + attrs + "millis (~" + (attrs / cntProductAttr) + " per item)"); String content = FileUtils.readFileToString(new File(fileToExport), "UTF-8"); assertTrue(content.contains("\"PRODUCT\";\"COLOR\";\"Color\";;;;\"false\";\"false\";\"Failover is: false\";\"500\";\"String\"")); rs = getConnection().getConnection().createStatement().executeQuery ("select count(*) from TPRODUCT "); rs.next(); long cntProd = rs.getLong(1); rs.close(); dt = System.currentTimeMillis(); fileToExport = "target/productnames-export-" + UUID.randomUUID().toString() + ".csv"; bulkExportService.doExport(createContext("src/test/resources/export/csv/productnames.xml", listener, fileToExport)); final long prods = System.currentTimeMillis() - dt; System.out.println(String.format("%5d", cntProd) + " products in " + prods + "millis (~" + (prods / cntProd) + " per item)"); content = FileUtils.readFileToString(new File(fileToExport), "UTF-8"); assertTrue(content.contains("\"BENDER\";\"BENDER\";\"Bender Bending Rodriguez\";\"Бендер Згибатель Родригес\";\"Brand is: FutureRobots\"")); assertTrue(content.contains("#IMAGE0#\t#sobot-picture.jpeg#")); assertTrue(content.contains(";\"sobot-picture.jpeg\";\"#101#\t#Big Boys Gadgets#\"")); assertTrue(content.contains(";\"sobot-picture.jpeg\";;")); assertTrue(content.contains("\"PREORDER-BACK-TO-FLOW0\";\"PREORDER-BACK-TO-FLOW0\";;;\"Brand is: PreorderCompany\";\"\";\"\";\"\";;\"11.10\";\"11.10\"\n")); assertTrue(content.contains("\"PREORDER-BACK-TO-FLOW2\";\"PREORDER-BACK-TO-FLOW2\";;;\"Brand is: PreorderCompany\";\"\";\"\";\"\";;\"12.11\";\"12.11\"\n")); mockery.assertIsSatisfied(); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }