XlsRowImportTemplate { @SuppressWarnings("unchecked") public List<T> execute() throws IOException { List<T> results = new ArrayList<>(); try (Workbook wb = importer.createWorkbook(bytes)) { Sheet sheet = wb.getSheetAt(sheetIndex); int i = 0; while (i <= sheet.getLastRowNum()) { if (i >= firstRowNumber) { try { if (importer.isRowEmpty(sheet.getRow(i))) { break; } else { i++; } T t = importer.processRows(sheet, i, colIndex, clazz); if (t != null && extractKey(t) != null) { ID key = extractKey(t); if (key instanceof String) { key = (ID) ((String) key).toLowerCase(); } if (checkForDuplicates) { if (!keys.contains(key)) { keys.add(key); results.add(t); } else { errors.add(messageService.getMessage("ocs.duplicate.row", new Locale(SystemPropertyUtils.getDefaultLocale()), i + 1, key)); } } else { results.add(t); } } } catch (OCSImportException ex) { errors.add(String.format("Row %d: %s", i + 1, ex.getMessage())); } i += recordLength; } else { i++; } } return results; } } XlsRowImportTemplate(BaseXlsImporter importer, MessageService messageService, byte[] bytes,
			List<String> errors, Class<T> clazz, int sheetIndex, int firstRowNumber, int colIndex, int recordLength,
			boolean checkForDuplicates); @SuppressWarnings("unchecked") List<T> execute();  }
@Test public void test() throws IOException { byte[] bytes = readFile("importer_rows.xlsx"); List<String> errors = new ArrayList<>(); XlsRowImportTemplate<String, PersonDTO> template = new XlsRowImportTemplate<String, PersonDTO>(importer, messageService, bytes, errors, PersonDTO.class, 0, 0, 1, 9, false) { @Override protected String extractKey(PersonDTO record) { return record.getName(); } }; List<PersonDTO> result = template.execute(); assertEquals(2, result.size()); assertTrue(errors.isEmpty()); PersonDTO person = result.get(0); assertEquals("Bas", person.getName()); assertEquals(com.ocs.dynamo.importer.impl.PersonDTO.Gender.M, person.getGender()); assertEquals(DateUtils.createLocalDate("04042014"), person.getDate()); assertEquals(2.4, person.getFactor().doubleValue(), 0.001); assertEquals(Boolean.TRUE, person.getAbool()); }
@Test public void test2() throws IOException { byte[] bytes = readFile("importer_rows2.xlsx"); List<String> errors = new ArrayList<>(); XlsRowImportTemplate<String, PersonDTO> template = new XlsRowImportTemplate<String, PersonDTO>(importer, messageService, bytes, errors, PersonDTO.class, 0, 0, 1, 8, false) { @Override protected String extractKey(PersonDTO record) { return record.getName(); } }; List<PersonDTO> result = template.execute(); assertEquals(2, result.size()); }
@Test public void test3_Duplicates() throws IOException { byte[] bytes = readFile("importer_rows3.xlsx"); List<String> errors = new ArrayList<>(); XlsRowImportTemplate<String, PersonDTO> template = new XlsRowImportTemplate<String, PersonDTO>(importer, messageService, bytes, errors, PersonDTO.class, 0, 0, 1, 9, true) { @Override protected String extractKey(PersonDTO record) { return record.getName(); } }; List<PersonDTO> result = template.execute(); assertEquals(1, result.size()); assertEquals(1, errors.size()); assertEquals("ocs.duplicate.row", errors.get(0)); }
@Test public void test4_Missing() throws IOException { byte[] bytes = readFile("importer_rows4.xlsx"); List<String> errors = new ArrayList<>(); XlsRowImportTemplate<String, PersonDTO> template = new XlsRowImportTemplate<String, PersonDTO>(importer, messageService, bytes, errors, PersonDTO.class, 0, 0, 1, 8, true) { @Override protected String extractKey(PersonDTO record) { return record.getName(); } }; List<PersonDTO> result = template.execute(); assertEquals(1, result.size()); assertEquals(1, errors.size()); assertTrue(errors.get(0).contains("Required value for field 'number' is missing")); }