001// Copyright 2011-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.jpa;
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.apache.tapestry5.jpa.EntityManagerManager;
024import org.slf4j.Logger;
025
026import javax.persistence.EntityManager;
027import javax.persistence.metamodel.EntityType;
028import javax.persistence.metamodel.SingularAttribute;
029import javax.persistence.metamodel.Type;
030
031public class JpaValueEncoder<E> implements ValueEncoder<E>
032{
033    private final EntityType<E> entity;
034    private final EntityManagerManager entityManagerManager;
035    private final String persistenceUnitName;
036    private final TypeCoercer typeCoercer;
037    private final Logger logger;
038    private final String idPropertyName;
039    private final PropertyAdapter propertyAdapter;
040
041    public JpaValueEncoder(final EntityType<E> entity,
042            final EntityManagerManager entityManagerManager, final String persistenceUnitName,
043            final PropertyAccess propertyAccess, final TypeCoercer typeCoercer, final Logger logger)
044    {
045        super();
046        this.entity = entity;
047        this.entityManagerManager = entityManagerManager;
048        this.persistenceUnitName = persistenceUnitName;
049        this.typeCoercer = typeCoercer;
050        this.logger = logger;
051
052        final Type<?> idType = this.entity.getIdType();
053
054        final SingularAttribute<? super E, ?> idAttribute = this.entity.getId(idType.getJavaType());
055
056        idPropertyName = idAttribute.getName();
057
058        propertyAdapter = propertyAccess.getAdapter(entity.getJavaType()).getPropertyAdapter(
059                idPropertyName);
060
061    }
062
063    /**
064     * {@inheritDoc}
065     */
066    public String toClient(final E value)
067    {
068        if (value == null)
069            return null;
070
071        final Object id = propertyAdapter.get(value);
072
073        if (id == null)
074        {
075            return null;
076        }
077
078        return typeCoercer.coerce(id, String.class);
079    }
080
081    /**
082     * {@inheritDoc}
083     */
084    public E toValue(final String clientValue)
085    {
086        if (InternalUtils.isBlank(clientValue))
087            return null;
088
089        Object id = null;
090        final Class<E> entityClass = entity.getJavaType();
091
092        try
093        {
094
095            id = typeCoercer.coerce(clientValue, propertyAdapter.getType());
096        }
097        catch (final Exception ex)
098        {
099            throw new RuntimeException(String.format(
100                    "Exception converting '%s' to instance of %s (id type for entity %s): %s",
101                    clientValue, propertyAdapter.getType().getName(), entityClass.getName(),
102                    ExceptionUtils.toMessage(ex)), ex);
103        }
104
105        final EntityManager em = entityManagerManager.getEntityManager(persistenceUnitName);
106
107        final E result = em.find(entityClass, id);
108
109        if (result == null)
110        {
111            // We don't identify the entity type in the message because the logger is based on the
112            // entity type.
113            logger.error(String.format(
114                    "Unable to convert client value '%s' into an entity instance.", clientValue));
115        }
116
117        return result;
118    }
119}