com.eclecticlogic.pedal.dm.internal.DAORegistryImpl Maven / Gradle / Ivy
Show all versions of pedal Show documentation
/**
* Copyright (c) 2014 Eclectic Logic LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.eclecticlogic.pedal.dm.internal;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManagerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import com.eclecticlogic.pedal.Transaction;
import com.eclecticlogic.pedal.dm.DAO;
import com.eclecticlogic.pedal.dm.DAORegistry;
import com.eclecticlogic.pedal.dm.TestableDAO;
public class DAORegistryImpl implements DAORegistry, BeanPostProcessor {
private Transaction transaction;
private EntityManagerFactory entityManagerFactory;
private Map, DAO> daosByEntityClass = new HashMap<>();
private static Logger logger = LoggerFactory.getLogger(DAORegistryImpl.class);
protected Transaction getTransaction() {
return transaction;
}
public void setTransaction(Transaction transaction) {
this.transaction = transaction;
}
protected EntityManagerFactory getEntityManagerFactory() {
return entityManagerFactory;
}
public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof DAO) {
DAO dao = (DAO) bean;
daosByEntityClass.put(dao.getEntityClass(), dao);
}
return bean;
}
@Override
@SuppressWarnings("unchecked")
public DAO get(Class clz) {
return (DAO) daosByEntityClass.get(clz);
}
@Override
@SuppressWarnings("unchecked")
public DAO get(E entity) {
return (DAO) daosByEntityClass.get(getEntityClass(entity));
}
@Override
@SuppressWarnings("unchecked")
public void testDAOs() {
for (DAO udao : daosByEntityClass.values()) {
DAO dao = getGenericizedDAO(udao);
if (dao instanceof TestableDAO) {
logger.debug("Testing DAO " + dao.getClass().getName());
P pk = ((TestableDAO) dao).getPrototypicalPrimaryKey();
logger.trace("Testing find method with pk " + pk);
dao.findById(pk);
}
}
}
/**
* This is to work around java generics coercion.
* @param dao
* @return
*/
@SuppressWarnings("unchecked")
private DAO getGenericizedDAO(
DAO dao) {
return (DAO) dao;
}
private Class getEntityClass(Object object) {
Class clz = object.getClass();
while (clz != null) {
try {
getEntityManagerFactory().getMetamodel().entity(clz);
break;
} catch (IllegalArgumentException e) {
clz = clz.getSuperclass();
}
}
return clz;
}
}