argos/dmp-backend/src/main/java/eu/eudat/dao/JpaDaoFactory.java

85 lines
3.4 KiB
Java

package eu.eudat.dao;
import java.util.HashMap;
import java.util.Map;
import eu.eudat.dao.entities.DMPDaoImpl;
import eu.eudat.dao.entities.DMPProfileDaoImpl;
import eu.eudat.dao.entities.DMPResearcherDaoImpl;
import eu.eudat.dao.entities.DataRepositoryDaoImpl;
import eu.eudat.dao.entities.DatasetDaoImpl;
import eu.eudat.dao.entities.DatasetProfileDaoImpl;
import eu.eudat.dao.entities.DatasetProfileRulesetDaoImpl;
import eu.eudat.dao.entities.DatasetProfileViewstyleDaoImpl;
import eu.eudat.dao.entities.DatasetRegistryDaoImpl;
import eu.eudat.dao.entities.DatasetServiceDaoImpl;
import eu.eudat.dao.entities.OrganisationDaoImpl;
import eu.eudat.dao.entities.ProjectDaoImpl;
import eu.eudat.dao.entities.RegistryDaoImpl;
import eu.eudat.dao.entities.ResearcherDaoImpl;
import eu.eudat.dao.entities.ServiceDaoImpl;
import eu.eudat.entities.DMP;
import eu.eudat.entities.DMPProfile;
import eu.eudat.entities.DMPResearcher;
import eu.eudat.entities.DataRepository;
import eu.eudat.entities.Dataset;
import eu.eudat.entities.DatasetProfile;
import eu.eudat.entities.DatasetProfileRuleset;
import eu.eudat.entities.DatasetProfileViewstyle;
import eu.eudat.entities.DatasetRegistry;
import eu.eudat.entities.DatasetService;
import eu.eudat.entities.Organisation;
import eu.eudat.entities.Project;
import eu.eudat.entities.Registry;
import eu.eudat.entities.Researcher;
import eu.eudat.entities.Service;
/**
* A DAO factory for non-managed environments
*/
public class JpaDaoFactory implements DaoFactory
{
private static String persistenceUnit = null;
private static Map<String, String> daoImpls = null;
public static void setPersistenceContext(String persistenceUnit)
{
JpaDaoFactory.persistenceUnit = persistenceUnit;
}
private static void populateMappings()
{
daoImpls = new HashMap<String, String>();
daoImpls.put(Organisation.class.getName(), OrganisationDaoImpl.class.getName());
daoImpls.put(DataRepository.class.getName(), (DataRepositoryDaoImpl.class.getName()));
daoImpls.put(Dataset.class.getName(), (DatasetDaoImpl.class.getName()));
daoImpls.put(DatasetProfile.class.getName(), (DatasetProfileDaoImpl.class.getName()));
daoImpls.put(DatasetProfileRuleset.class.getName(), (DatasetProfileRulesetDaoImpl.class.getName()));
daoImpls.put(DatasetProfileViewstyle.class.getName(), (DatasetProfileViewstyleDaoImpl.class.getName()));
daoImpls.put(DatasetRegistry.class.getName(), (DatasetRegistryDaoImpl.class.getName()));
daoImpls.put(DatasetService.class.getName(), (DatasetServiceDaoImpl.class.getName()));
daoImpls.put(DMP.class.getName(), (DMPDaoImpl.class.getName()));
daoImpls.put(DMPProfile.class.getName(), (DMPProfileDaoImpl.class.getName()));
daoImpls.put(DMPResearcher.class.getName(), (DMPResearcherDaoImpl.class.getName()));
daoImpls.put(Organisation.class.getName(), (OrganisationDaoImpl.class.getName()));
daoImpls.put(Project.class.getName(), (ProjectDaoImpl.class.getName()));
daoImpls.put(Registry.class.getName(), (RegistryDaoImpl.class.getName()));
daoImpls.put(Researcher.class.getName(), (ResearcherDaoImpl.class.getName()));
daoImpls.put(Service.class.getName(), (ServiceDaoImpl.class.getName()));
}
@SuppressWarnings("rawtypes")
public Dao getDao(Class<?> type) throws Exception
{
if(daoImpls == null) populateMappings();
return (Dao)Class.forName(daoImpls.get(type.getName())).newInstance();
}
public void overrideMappings(Map<String, String> mappings)
{
populateMappings();
daoImpls.putAll(mappings);
}
}