    001// Copyright 2008-2013 The Apache Software Foundation
    002//
    003// Licensed under the Apache License, Version 2.0 (the "License");
    004// you may not use this file except in compliance with the License.
    005// You may obtain a copy of the License at
    006//
    007// http://www.apache.org/licenses/LICENSE-2.0
    008//
    009// Unless required by applicable law or agreed to in writing, software
    010// distributed under the License is distributed on an "AS IS" BASIS,
    011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012// See the License for the specific language governing permissions and
    013// limitations under the License.
    014
    015package org.apache.tapestry5.internal.hibernate;
    016
    017import org.apache.tapestry5.ValueEncoder;
    018import org.apache.tapestry5.ioc.internal.util.InternalUtils;
    019import org.apache.tapestry5.ioc.services.PropertyAccess;
    020import org.apache.tapestry5.ioc.services.PropertyAdapter;
    021import org.apache.tapestry5.ioc.services.TypeCoercer;
    022import org.apache.tapestry5.ioc.util.ExceptionUtils;
    023import org.hibernate.Session;
    024import org.hibernate.mapping.PersistentClass;
    025import org.hibernate.mapping.Property;
    026import org.slf4j.Logger;
    027
    028import java.io.Serializable;
    029
    030public final class HibernateEntityValueEncoder<E> implements ValueEncoder<E>
    031{
    032    private final Class<E> entityClass;
    033
    034    private final Session session;
    035
    036    private final TypeCoercer typeCoercer;
    037
    038    private final PropertyAdapter propertyAdapter;
    039
    040    private final Logger logger;
    041
    042    public HibernateEntityValueEncoder(Class<E> entityClass, PersistentClass persistentClass, Session session,
    043                                       PropertyAccess propertyAccess, TypeCoercer typeCoercer, Logger logger)
    044    {
    045        this.entityClass = entityClass;
    046        this.session = session;
    047        this.typeCoercer = typeCoercer;
    048        this.logger = logger;
    049
    050        Property property = persistentClass.getIdentifierProperty();
    051
    052        propertyAdapter = propertyAccess.getAdapter(this.entityClass).getPropertyAdapter(property.getName());
    053    }
    054
    055    @Override
    056    public String toClient(E value)
    057    {
    058        if (value == null)
    059            return null;
    060
    061        Object id = propertyAdapter.get(value);
    062
    063        if (id == null)
    064        {
    065            return null;
    066        }
    067
    068        return typeCoercer.coerce(id, String.class);
    069    }
    070
    071    @Override
    072    @SuppressWarnings("unchecked")
    073    public E toValue(String clientValue)
    074    {
    075        if (InternalUtils.isBlank(clientValue))
    076            return null;
    077
    078        Object id = null;
    079
    080        try
    081        {
    082
    083            id = typeCoercer.coerce(clientValue, propertyAdapter.getType());
    084        } catch (Exception ex)
    085        {
    086            throw new RuntimeException(String.format(
    087                    "Exception converting '%s' to instance of %s (id type for entity %s): %s", clientValue,
    088                    propertyAdapter.getType().getName(), entityClass.getName(), ExceptionUtils.toMessage(ex)), ex);
    089        }
    090
    091        Serializable ser = (Serializable) id;
    092
    093        E result = (E) session.get(entityClass, ser);
    094
    095        if (result == null)
    096        {
    097            // We don't identify the entity type in the message because the logger is based on the
    098            // entity type.
    099            logger.error(String.format("Unable to convert client value '%s' into an entity instance.", clientValue));
    100        }
    101
    102        return result;
    103    }
    104
    105}