Search in sources :

Example 1 with HibernateEntityValueEncoder

use of org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder in project tapestry-5 by apache.

the class HibernateModule method contributeValueEncoderSource.

/**
 * Contributes {@link ValueEncoderFactory}s for all registered Hibernate entity classes. Encoding and decoding are
 * based on the id property value of the entity using type coercion. Hence, if the id can be coerced to a String and
 * back then the entity can be coerced.
 */
@SuppressWarnings("unchecked")
public static void contributeValueEncoderSource(MappedConfiguration<Class, ValueEncoderFactory> configuration, @Symbol(HibernateSymbols.PROVIDE_ENTITY_VALUE_ENCODERS) boolean provideEncoders, final HibernateSessionSource sessionSource, final Session session, final TypeCoercer typeCoercer, final PropertyAccess propertyAccess, final LoggerSource loggerSource) {
    if (!provideEncoders)
        return;
    Set<EntityType<?>> entities = sessionSource.getSessionFactory().getMetamodel().getEntities();
    for (EntityType<?> entityType : entities) {
        Class<?> entityClass = entityType.getJavaType();
        if (entityClass != null) {
            SingularAttribute<?, ?> id = entityType.getId(entityType.getIdType().getJavaType());
            final String idenfierPropertyName = id.getName();
            ValueEncoderFactory factory = new ValueEncoderFactory() {

                @Override
                public ValueEncoder create(Class type) {
                    return new HibernateEntityValueEncoder(entityClass, idenfierPropertyName, session, propertyAccess, typeCoercer, loggerSource.getLogger(entityClass));
                }
            };
            configuration.add(entityClass, factory);
        }
    }
}
Also used : EntityType(javax.persistence.metamodel.EntityType) HibernateEntityValueEncoder(org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder) ValueEncoderFactory(org.apache.tapestry5.services.ValueEncoderFactory)

Example 2 with HibernateEntityValueEncoder

use of org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder in project tapestry-5 by apache.

the class HibernateEntityValueEncoderTest method to_client_id_null.

@Test
public void to_client_id_null() {
    Session session = mockSession();
    Logger logger = mockLogger();
    replay();
    SampleEntity entity = new SampleEntity();
    HibernateEntityValueEncoder<SampleEntity> encoder = new HibernateEntityValueEncoder<SampleEntity>(SampleEntity.class, "id", session, access, typeCoercer, logger);
    assertNull(encoder.toClient(entity));
    verify();
}
Also used : HibernateEntityValueEncoder(org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder) Logger(org.slf4j.Logger) Session(org.hibernate.Session) Test(org.testng.annotations.Test)

Example 3 with HibernateEntityValueEncoder

use of org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder in project tapestry-5 by apache.

the class HibernateEntityValueEncoderTest method to_value_not_found.

@Test
public void to_value_not_found() {
    Session session = mockSession();
    Logger logger = mockLogger();
    expect(session.get(SampleEntity.class, new Long(12345))).andReturn(null);
    logger.error("Unable to convert client value '12345' into an entity instance.");
    replay();
    SampleEntity entity = new SampleEntity();
    HibernateEntityValueEncoder<SampleEntity> encoder = new HibernateEntityValueEncoder<SampleEntity>(SampleEntity.class, "id", session, access, typeCoercer, logger);
    assertNull(encoder.toValue("12345"));
    verify();
}
Also used : HibernateEntityValueEncoder(org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder) Logger(org.slf4j.Logger) Session(org.hibernate.Session) Test(org.testng.annotations.Test)

Example 4 with HibernateEntityValueEncoder

use of org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder in project tapestry-5 by apache.

the class HibernateEntityValueEncoderTest method to_value_bad_type_coercion.

@Test
public void to_value_bad_type_coercion() {
    Session session = mockSession();
    Logger logger = mockLogger();
    replay();
    HibernateEntityValueEncoder<SampleEntity> encoder = new HibernateEntityValueEncoder<SampleEntity>(SampleEntity.class, "id", session, access, typeCoercer, logger);
    try {
        encoder.toValue("xyz");
        unreachable();
    } catch (RuntimeException ex) {
        assertMessageContains(ex, "Exception converting 'xyz' to instance of java.lang.Long (id type for entity org.apache.tapestry5.internal.hibernate.SampleEntity)");
    }
    assertNull(encoder.toValue(""));
    verify();
}
Also used : HibernateEntityValueEncoder(org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder) Logger(org.slf4j.Logger) Session(org.hibernate.Session) Test(org.testng.annotations.Test)

Aggregations

HibernateEntityValueEncoder (org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder)4 Session (org.hibernate.Session)3 Logger (org.slf4j.Logger)3 Test (org.testng.annotations.Test)3 EntityType (javax.persistence.metamodel.EntityType)1 ValueEncoderFactory (org.apache.tapestry5.services.ValueEncoderFactory)1