Search in sources :

Example 46 with Session

use of org.apache.tapestry5.services.Session 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 47 with Session

use of org.apache.tapestry5.services.Session in project tapestry-5 by apache.

the class EntityPersistentFieldStrategyTest method not_an_entity.

public void not_an_entity() {
    String nonEntity = "foo";
    Session session = newMock(Session.class);
    EntityPersistentFieldStrategy strategy = new EntityPersistentFieldStrategy(session, null);
    expect(session.contains(nonEntity)).andReturn(true);
    expect(session.getEntityName(nonEntity)).andThrow(new HibernateException("error"));
    replay();
    try {
        strategy.postChange("pageName", "", "fieldName", nonEntity);
        unreachable();
    } catch (IllegalArgumentException ex) {
        assertEquals(ex.getMessage(), "Failed persisting an entity in the session. entity: foo");
    }
    verify();
}
Also used : HibernateException(org.hibernate.HibernateException) EntityPersistentFieldStrategy(org.apache.tapestry5.hibernate.web.internal.EntityPersistentFieldStrategy) Session(org.hibernate.Session)

Example 48 with Session

use of org.apache.tapestry5.services.Session 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 49 with Session

use of org.apache.tapestry5.services.Session 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 50 with Session

use of org.apache.tapestry5.services.Session 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

Test (org.testng.annotations.Test)31 Session (org.apache.tapestry5.http.services.Session)30 Request (org.apache.tapestry5.http.services.Request)14 HttpSession (javax.servlet.http.HttpSession)10 Logger (org.slf4j.Logger)8 ClusteredSessionImpl (org.apache.tapestry5.http.internal.services.ClusteredSessionImpl)7 Session (org.hibernate.Session)7 SessionImpl (org.apache.tapestry5.http.internal.services.SessionImpl)6 ApplicationStatePersistenceStrategy (org.apache.tapestry5.services.ApplicationStatePersistenceStrategy)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 Resource (org.apache.tapestry5.commons.Resource)5 SessionLock (org.apache.tapestry5.http.internal.services.SessionLock)5 MutableComponentModel (org.apache.tapestry5.model.MutableComponentModel)5 HibernateEntityValueEncoder (org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder)4 ReadOnlyBean (org.apache.tapestry5.internal.transform.pages.ReadOnlyBean)4 EntityPersistentFieldStrategy (org.apache.tapestry5.hibernate.web.internal.EntityPersistentFieldStrategy)3 RequestImpl (org.apache.tapestry5.http.internal.services.RequestImpl)3 TapestrySessionFactory (org.apache.tapestry5.http.internal.services.TapestrySessionFactory)3 PersistentFieldChange (org.apache.tapestry5.services.PersistentFieldChange)3 Enumeration (java.util.Enumeration)2