{
JPAFacet<PersistenceCommonDescriptor> facet = project.getFacet(JPAFacet.class);
PersistenceContainer container = dataSource.getContainer();
PersistenceProvider provider = dataSource.getProvider();
PersistenceCommonDescriptor config = facet.getConfig();
PersistenceUnitCommon unit = null;
List<PersistenceUnitCommon> allPersistenceUnit = config.getAllPersistenceUnit();
for (PersistenceUnitCommon persistenceUnit : allPersistenceUnit)
{
if (unitName.equals(persistenceUnit.getName()))
{
unit = persistenceUnit;
break;
}
}
if (unit == null)
{
unit = config.createPersistenceUnit();
}
else
{
// FORGE-2049: Call all Remove methods until there is a decent way to do this in ShrinkWrap Descriptors
unit.removeAllClazz().removeAllJarFile().removeAllMappingFile().removeDescription()
.removeExcludeUnlistedClasses().removeJtaDataSource().removeName().removeNonJtaDataSource()
.removeProperties().removeProvider();
}
unit.name(unitName).description(DEFAULT_UNIT_DESC);
if (provider.getProvider() != null)
{
unit.provider(provider.getProvider());
}
container.setupConnection(unit, dataSource);
provider.configure(unit, dataSource, project);
facet.saveConfig(config);