Search in sources :

Example 1 with DiscriminatorMapper

use of org.simpleflatmapper.map.mapper.DiscriminatorMapper in project SimpleFlatMapper by arnaudroger.

the class DiscriminatorMapperTest method setUp.

@Before
public void setUp() {
    Mapper<Object[], Person> studentMapper = new Mapper<Object[], Person>() {

        @Override
        public Person map(Object[] source) throws MappingException {
            return map(source, null);
        }

        @Override
        public Person map(Object[] source, MappingContext<? super Object[]> context) throws MappingException {
            StudentGS studentGS = new StudentGS();
            try {
                mapTo(source, studentGS, context);
            } catch (Exception e) {
                ErrorHelper.rethrow(e);
            }
            return studentGS;
        }

        @Override
        public void mapTo(Object[] source, Person target, MappingContext<? super Object[]> context) throws Exception {
            StudentGS studentGS = (StudentGS) target;
            studentGS.setName((String) source[1]);
            studentGS.setId((Integer) source[2]);
        }
    };
    Mapper<Object[], Person> professorMapper = new Mapper<Object[], Person>() {

        @Override
        public Person map(Object[] source) throws MappingException {
            return map(source, null);
        }

        @Override
        public Person map(Object[] source, MappingContext<? super Object[]> context) throws MappingException {
            ProfessorGS professorGS = new ProfessorGS();
            try {
                mapTo(source, professorGS, context);
            } catch (Exception e) {
                ErrorHelper.rethrow(e);
            }
            return professorGS;
        }

        @Override
        public void mapTo(Object[] source, Person target, MappingContext<? super Object[]> context) throws Exception {
            ProfessorGS professorGS = (ProfessorGS) target;
            professorGS.setName((String) source[1]);
            professorGS.setId((Integer) source[2]);
        }
    };
    List<DiscriminatorMapper.PredicatedMapper<Object[], Object[][], Person, RuntimeException>> mappers = new ArrayList<DiscriminatorMapper.PredicatedMapper<Object[], Object[][], Person, RuntimeException>>();
    mappers.add(new DiscriminatorMapper.PredicatedMapper<Object[], Object[][], Person, RuntimeException>(new Predicate<Object[]>() {

        @Override
        public boolean test(Object[] objects) {
            return "student".equals(objects[0]);
        }
    }, studentMapper, new MappingContextFactoryFromRows<Object[], Object[][], RuntimeException>() {

        @Override
        public MappingContext<? super Object[]> newMappingContext(Object[][] objects) throws RuntimeException {
            return MappingContext.INSTANCE;
        }
    }));
    mappers.add(new DiscriminatorMapper.PredicatedMapper<Object[], Object[][], Person, RuntimeException>(new Predicate<Object[]>() {

        @Override
        public boolean test(Object[] objects) {
            return "professor".equals(objects[0]);
        }
    }, professorMapper, new MappingContextFactoryFromRows<Object[], Object[][], RuntimeException>() {

        @Override
        public MappingContext<? super Object[]> newMappingContext(Object[][] objects) throws RuntimeException {
            return MappingContext.INSTANCE;
        }
    }));
    mapper = new DiscriminatorMapper<Object[], Object[][], Person, RuntimeException>(mappers, new UnaryFactory<Object[][], Enumarable<Object[]>>() {

        @Override
        public Enumarable<Object[]> newInstance(Object[][] objects) {
            return new ArrayEnumarable<Object[]>(objects);
        }
    }, new UncheckedConverter<Object[], String>() {

        @Override
        public String convert(Object[] in) {
            return Arrays.toString(in);
        }
    }, RethrowConsumerErrorHandler.INSTANCE);
}
Also used : ArrayList(java.util.ArrayList) DiscriminatorMapper(org.simpleflatmapper.map.mapper.DiscriminatorMapper) MappingContextFactoryFromRows(org.simpleflatmapper.map.context.MappingContextFactoryFromRows) MappingException(org.simpleflatmapper.map.MappingException) Predicate(org.simpleflatmapper.util.Predicate) MappingContext(org.simpleflatmapper.map.MappingContext) ProfessorGS(org.simpleflatmapper.test.beans.ProfessorGS) DiscriminatorMapper(org.simpleflatmapper.map.mapper.DiscriminatorMapper) Mapper(org.simpleflatmapper.map.Mapper) UnaryFactory(org.simpleflatmapper.util.UnaryFactory) UncheckedConverter(org.simpleflatmapper.converter.UncheckedConverter) Person(org.simpleflatmapper.test.beans.Person) ArrayEnumarable(org.simpleflatmapper.util.ArrayEnumarable) StudentGS(org.simpleflatmapper.test.beans.StudentGS) Before(org.junit.Before)

Example 2 with DiscriminatorMapper

use of org.simpleflatmapper.map.mapper.DiscriminatorMapper in project SimpleFlatMapper by arnaudroger.

the class DiscriminatorJdbcBuilder method mapper.

/**
 * @return a new jdbcMapper based on the current state
 */
public JdbcMapper<T> mapper() {
    List<DiscriminatorMapper.PredicatedMapper<ResultSet, ResultSet, T, SQLException>> mappers = new ArrayList<DiscriminatorMapper.PredicatedMapper<ResultSet, ResultSet, T, SQLException>>();
    for (DiscriminatorJdbcSubBuilder subBuilder : builders) {
        JdbcMapper<T> mapper = subBuilder.createMapper();
        Predicate<ResultSet> predicate = new ResultSetDiscriminatorPredicate(column, subBuilder.predicate);
        mappers.add(new DiscriminatorMapper.PredicatedMapper<ResultSet, ResultSet, T, SQLException>(predicate, mapper, mapper));
    }
    DiscriminatorJdbcMapper<T> discriminatorMapper = new DiscriminatorJdbcMapper<T>(mappers, new UnaryFactory<ResultSet, Enumarable<ResultSet>>() {

        @Override
        public Enumarable<ResultSet> newInstance(ResultSet resultSet) {
            return new ResultSetEnumarable(resultSet);
        }
    }, UncheckedConverterHelper.<ResultSet, String>toUnchecked(new Converter<ResultSet, String>() {

        @Override
        public String convert(ResultSet in) throws SQLException {
            return column + ":" + in.getObject(column);
        }
    }), jdbcMapperFactory.consumerErrorHandler());
    return discriminatorMapper;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) DiscriminatorMapper(org.simpleflatmapper.map.mapper.DiscriminatorMapper) ResultSetEnumarable(org.simpleflatmapper.jdbc.impl.ResultSetEnumarable) Enumarable(org.simpleflatmapper.util.Enumarable) ResultSetEnumarable(org.simpleflatmapper.jdbc.impl.ResultSetEnumarable) ResultSet(java.sql.ResultSet) UncheckedConverter(org.simpleflatmapper.converter.UncheckedConverter) Converter(org.simpleflatmapper.converter.Converter)

Aggregations

ArrayList (java.util.ArrayList)2 UncheckedConverter (org.simpleflatmapper.converter.UncheckedConverter)2 DiscriminatorMapper (org.simpleflatmapper.map.mapper.DiscriminatorMapper)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Before (org.junit.Before)1 Converter (org.simpleflatmapper.converter.Converter)1 ResultSetEnumarable (org.simpleflatmapper.jdbc.impl.ResultSetEnumarable)1 Mapper (org.simpleflatmapper.map.Mapper)1 MappingContext (org.simpleflatmapper.map.MappingContext)1 MappingException (org.simpleflatmapper.map.MappingException)1 MappingContextFactoryFromRows (org.simpleflatmapper.map.context.MappingContextFactoryFromRows)1 Person (org.simpleflatmapper.test.beans.Person)1 ProfessorGS (org.simpleflatmapper.test.beans.ProfessorGS)1 StudentGS (org.simpleflatmapper.test.beans.StudentGS)1 ArrayEnumarable (org.simpleflatmapper.util.ArrayEnumarable)1 Enumarable (org.simpleflatmapper.util.Enumarable)1 Predicate (org.simpleflatmapper.util.Predicate)1 UnaryFactory (org.simpleflatmapper.util.UnaryFactory)1