package dao; import java.util.HashMap; import java.util.Map; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import dao.entities.DMPDaoImpl; import dao.entities.DMPProfileDaoImpl; import dao.entities.DMPResearcherDaoImpl; import dao.entities.DataRepositoryDaoImpl; import dao.entities.DatasetDaoImpl; import dao.entities.DatasetProfileDaoImpl; import dao.entities.DatasetProfileRulesetDaoImpl; import dao.entities.DatasetProfileViewstyleDaoImpl; import dao.entities.DatasetRegistryDaoImpl; import dao.entities.DatasetServiceDaoImpl; import dao.entities.OrganisationDaoImpl; import dao.entities.ProjectDaoImpl; import dao.entities.RegistryDaoImpl; import dao.entities.ResearcherDaoImpl; import dao.entities.ServiceDaoImpl; import entities.DMP; import entities.DMPProfile; import entities.DMPResearcher; import entities.DataRepository; import entities.Dataset; import entities.DatasetProfile; import entities.DatasetProfileRuleset; import entities.DatasetProfileViewstyle; import entities.DatasetRegistry; import entities.DatasetService; import entities.Organisation; import entities.Project; import entities.Registry; import entities.Researcher; import entities.Service; /** * A DAO factory for Spring managed environment */ public class SpringJpaDaoFactory implements DaoFactory { public static class SpringApplicationContext implements ApplicationContextAware { private static ApplicationContext CONTEXT; /** * This method is called from within the ApplicationContext once it is * done starting up, it will stick a reference to itself into this bean. * @param context a reference to the ApplicationContext. */ public void setApplicationContext(ApplicationContext context) throws BeansException { CONTEXT = context; } /** * This is about the same as context.getBean("beanName"), except it has its * own static handle to the Spring context, so calling this method statically * will give access to the beans by name in the Spring application context. * As in the context.getBean("beanName") call, the caller must cast to the * appropriate target class. If the bean does not exist, then a Runtime error * will be thrown. * @param beanName the name of the bean to get. * @return an Object reference to the named bean. */ public static Object getBean(String beanName) { return CONTEXT.getBean(beanName); } } private static String persistenceUnit = null; private static Map daoImpls = null; public static void setPersistenceContext(String persistenceUnit) { SpringJpaDaoFactory.persistenceUnit = persistenceUnit; } private static String getBeanName(String className) { return Character.toLowerCase(className.charAt(0)) + (className.length() > 1 ? className.substring(1) : ""); } private static void populateMappings() { daoImpls = new HashMap(); daoImpls.put(DataRepository.class.getName(), getBeanName(DataRepositoryDaoImpl.class.getSimpleName())); daoImpls.put(Dataset.class.getName(), getBeanName(DatasetDaoImpl.class.getSimpleName())); daoImpls.put(DatasetProfile.class.getName(), getBeanName(DatasetProfileDaoImpl.class.getSimpleName())); daoImpls.put(DatasetProfileRuleset.class.getName(), getBeanName(DatasetProfileRulesetDaoImpl.class.getSimpleName())); daoImpls.put(DatasetProfileViewstyle.class.getName(), getBeanName(DatasetProfileViewstyleDaoImpl.class.getSimpleName())); daoImpls.put(DatasetRegistry.class.getName(), getBeanName(DatasetRegistryDaoImpl.class.getSimpleName())); daoImpls.put(DatasetService.class.getName(), getBeanName(DatasetServiceDaoImpl.class.getSimpleName())); daoImpls.put(DMP.class.getName(), getBeanName(DMPDaoImpl.class.getSimpleName())); daoImpls.put(DMPProfile.class.getName(), getBeanName(DMPProfileDaoImpl.class.getSimpleName())); daoImpls.put(DMPResearcher.class.getName(), getBeanName(DMPResearcherDaoImpl.class.getSimpleName())); daoImpls.put(Organisation.class.getName(), getBeanName(OrganisationDaoImpl.class.getSimpleName())); daoImpls.put(Project.class.getName(), getBeanName(ProjectDaoImpl.class.getSimpleName())); daoImpls.put(Registry.class.getName(), getBeanName(RegistryDaoImpl.class.getSimpleName())); daoImpls.put(Researcher.class.getName(), getBeanName(ResearcherDaoImpl.class.getSimpleName())); daoImpls.put(Service.class.getName(), getBeanName(ServiceDaoImpl.class.getSimpleName())); } @SuppressWarnings("rawtypes") public Dao getDao(Class type) throws Exception { if(daoImpls == null) populateMappings(); return (Dao)SpringApplicationContext.getBean(daoImpls.get(type.getName())); } public void overrideMappings(Map mappings) { populateMappings(); daoImpls.putAll(mappings); } }