    001// Copyright 2011 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 java.util.Map;
    018import java.util.Set;
    019
    020import javax.persistence.EntityManager;
    021import javax.persistence.EntityManagerFactory;
    022import javax.persistence.PersistenceContext;
    023import javax.persistence.metamodel.EntityType;
    024import javax.persistence.metamodel.Metamodel;
    025
    026import org.apache.tapestry5.ioc.internal.util.InternalUtils;
    027import org.apache.tapestry5.jpa.EntityManagerManager;
    028import org.apache.tapestry5.jpa.JpaConstants;
    029
    030public class JpaInternalUtils
    031{
    032    public static PersistedEntity convertApplicationValueToPersisted(
    033            final EntityManagerManager entityManagerManager, final Object newValue)
    034    {
    035        final EntityManager em = getEntityManagerFactory(entityManagerManager, newValue);
    036
    037        final EntityManagerFactory emf = em.getEntityManagerFactory();
    038
    039        final Map<String, Object> properties = emf.getProperties();
    040
    041        final String persistenceUnitName = (String) properties
    042                .get(JpaConstants.PERSISTENCE_UNIT_NAME);
    043
    044        final Object id = emf.getPersistenceUnitUtil().getIdentifier(newValue);
    045
    046        return new PersistedEntity(newValue.getClass(), id, persistenceUnitName);
    047    }
    048
    049    private static EntityManager getEntityManagerFactory(
    050            final EntityManagerManager entityManagerManager, final Object entity)
    051    {
    052        final Map<String, EntityManager> entityManagers = entityManagerManager.getEntityManagers();
    053
    054        for (final EntityManager em : entityManagers.values())
    055        {
    056            final EntityManagerFactory emf = em.getEntityManagerFactory();
    057
    058            final Metamodel metamodel = emf.getMetamodel();
    059
    060            final Set<EntityType<?>> entities = metamodel.getEntities();
    061
    062            for (final EntityType<?> entityType : entities)
    063            {
    064                if (entityType.getJavaType().equals(entity.getClass()))
    065                {
    066                    if (em.contains(entity))
    067                    {
    068                        return em;
    069                    }
    070                }
    071            }
    072        }
    073
    074        throw new IllegalArgumentException(
    075                String.format(
    076                        "Failed persisting the entity. The entity '%s' does not belong to any of the existing persistence contexts.",
    077                        entity));
    078    }
    079
    080    public static EntityManager getEntityManager(EntityManagerManager entityManagerManager,
    081                                                 PersistenceContext annotation)
    082    {
    083        String unitName = annotation == null ? null : annotation.unitName();
    084
    085        if (InternalUtils.isNonBlank(unitName))
    086            return entityManagerManager.getEntityManager(unitName);
    087
    088        Map<String, EntityManager> entityManagers = entityManagerManager.getEntityManagers();
    089
    090        if (entityManagers.size() == 1)
    091            return entityManagers.values().iterator().next();
    092
    093        throw new RuntimeException("Unable to locate a single EntityManager. " +
    094                "You must provide the persistence unit name as defined in the persistence.xml using the @PersistenceContext annotation.");
    095    }
    096}