/** * Test should read succesfully. * * @throws Exception */ @Test public void testSuccessfulReading() throws Exception { // init linetokenizer DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer(); lineTokenizer.setNames(new String[] {"id", "value"}); // init linemapper DefaultLineMapper<FieldSet> lineMapper = new DefaultLineMapper<FieldSet>(); lineMapper.setLineTokenizer(lineTokenizer); lineMapper.setFieldSetMapper(new PassThroughFieldSetMapper()); // init reader reader.setLineMapper(lineMapper); reader.setResource(new FileSystemResource(INPUT_FILE)); // open, provide "mock" ExecutionContext reader.open(MetaDataInstanceFactory.createStepExecution().getExecutionContext()); // read try { int count = 0; FieldSet line; while ((line = reader.read()) != null) { // really test for the fieldSet names and values assertEquals("id", line.getNames()[0]); assertEquals(String.valueOf(count), line.getValues()[0]); assertEquals("value", line.getNames()[1]); // csv contains entry like '0,foo0' assertEquals("foo" + String.valueOf(count), line.getValues()[1]); count++; } assertEquals(EXPECTED_COUNT, count); } catch (Exception e) { throw e; } finally { reader.close(); } }
@Override public Employee mapFieldSet(FieldSet fieldSet) throws BindException { Employee employee = new Employee(); employee.setAge(fieldSet.readInt("age")); employee.setName(fieldSet.readString("name")); return employee; }
// TODO: set reader().setNames to correct order in csv. then update fields in this mapper class by // index @Override public Maintenance mapFieldSet(FieldSet fieldSet) throws BindException { // System.out.println("MARKER"+Arrays.asList(fieldSet.getValues())); // System.out.println("MARKER"+fieldSet.readString(10)+","+fieldSet.readString(11)+","+fieldSet.readString(12)+","+fieldSet.readString(13)); // System.out.println("planned"); Maintenance unplannedMaintenance = new Maintenance(); unplannedMaintenance.setNominalId(fieldSet.readString("H3G_Nominal_ID")); try { unplannedMaintenance.setStartDate( dateFormat.parse(fieldSet.readString("Planned_Outage_Start_Time"))); unplannedMaintenance.setFinishDate( dateFormat.parse(fieldSet.readString("Planned_Outage_End_Time"))); unplannedMaintenance.setCreatedDate(new Date()); } catch (ParseException pe) { // TODO: } // unplannedMaintenance.setDescription(fieldSet.readString("Description")); unplannedMaintenance.setNetwork(fieldSet.readString("Network_Type")); unplannedMaintenance.setMBNL_Site_ID(fieldSet.readString("MBNL_Site_ID")); unplannedMaintenance.setOutage("Planned"); unplannedMaintenance.setStatus( "Completed".equals(fieldSet.readString("Status")) ? fieldSet.readString("Status") : "Closed"); // unplannedMaintenance.setService(fieldSet.readString("Products_List")); //Products_List unplannedMaintenance.setReference(fieldSet.readString("CMID")); return unplannedMaintenance; }
@Override public Player mapFieldSet(FieldSet fs) { if (fs == null) { return null; } Player player = new Player(); player.setId(fs.readString("ID")); player.setLastName(fs.readString("lastName")); player.setFirstName(fs.readString("firstName")); player.setPosition(fs.readString("position")); player.setDebutYear(fs.readInt("debutYear")); player.setBirthYear(fs.readInt("birthYear")); return player; }
/** * Map the {@link FieldSet} to an object retrieved from the enclosing Spring context, or to a new * instance of the required type if no prototype is available. * * @throws BindException if there is a type conversion or other error (if the {@link DataBinder} * from {@link #createBinder(Object)} has errors after binding). * @throws NotWritablePropertyException if the {@link FieldSet} contains a field that cannot be * mapped to a bean property. * @see org.springframework.batch.item.file.mapping.FieldSetMapper#mapFieldSet(FieldSet) */ public T mapFieldSet(FieldSet fs) throws BindException { T copy = getBean(); DataBinder binder = createBinder(copy); binder.bind(new MutablePropertyValues(getBeanProperties(copy, fs.getProperties()))); if (binder.getBindingResult().hasErrors()) { throw new BindException(binder.getBindingResult()); } return copy; }
@Override public CustomerDTO read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException { CustomerDTO customerDTO = null; // Boucle sur les lignes du fichier for (FieldSet ligne = delegate.read(); ligne != null; ligne = delegate.read()) { String prefix = ligne.readString(0); if (prefix.equals("BEGIN")) { customerDTO = new CustomerDTO(); } else if (prefix.equals("NAME")) { Assert.notNull(customerDTO, "No 'BEGIN' was found."); customerDTO.setFirstName(ligne.readString(1)); customerDTO.setLastName(ligne.readString(2)); } else if (prefix.equals("AGE")) { Assert.notNull(customerDTO, "No 'BEGIN' was found."); customerDTO.setAge(ligne.readInt(1)); } else if (prefix.equals("GENDER")) { Assert.notNull(customerDTO, "No 'BEGIN' was found."); customerDTO.setGender(ligne.readString(1)); } else if (prefix.equals("END")) { return customerDTO; } } // Si BEGIN mais pas de END if (customerDTO != null) { Assert.notNull(customerDTO, "No 'END' was found."); } return null; }
public Geoname mapFieldSet(FieldSet fieldSet) throws BindException { if (fieldSet == null) return null; Geoname geoname = new Geoname(); geoname.setGeonameId(fieldSet.readInt(0)); geoname.setName(fieldSet.readString(1)); geoname.setAsciiname(fieldSet.readString(2)); geoname.setAlternatenames(fieldSet.readString(3)); geoname.setLatitude(fieldSet.readDouble(4)); geoname.setLongitude(fieldSet.readDouble(5)); geoname.setFeatureClass(fieldSet.readString(6)); geoname.setFeatureCode(fieldSet.readString(7)); geoname.setCountryCode(fieldSet.readString(8)); geoname.setCC2(fieldSet.readString(9)); geoname.setAdmin1Code(fieldSet.readString(10)); geoname.setAdmin2Code(fieldSet.readString(11)); geoname.setAdmin3Code(fieldSet.readString(12)); geoname.setAdmin4Code(fieldSet.readString(13)); geoname.setPopulation(fieldSet.readInt(14)); geoname.setElevation(fieldSet.readInt(15, 0)); geoname.setDem(fieldSet.readInt(16)); geoname.setTimezone(fieldSet.readString(17)); geoname.setModificationDate(fieldSet.readDate(18)); return geoname; }