log.debug("Updating bundle type and package types for " + existingType);
existingType = entityMgr.find(ResourceType.class, existingType.getId());
// set the bundle type if one is defined
BundleType newBundleType = newType.getBundleType();
if (newBundleType != null) {
BundleType existingBundleType = existingType.getBundleType();
Set<ResourceType> targetedResourceTypes = new HashSet<ResourceType>(
newBundleType.getExplicitlyTargetedResourceTypes().size());
if (!newBundleType.getExplicitlyTargetedResourceTypes().isEmpty()) {
for (ResourceType targetType : newBundleType.getExplicitlyTargetedResourceTypes()) {
ResourceType existingTargetType = resourceTypeMgr
.getResourceTypeByNameAndPlugin(targetType.getName(), targetType.getPlugin());
if (existingTargetType == null) {
throw new IllegalStateException(
"Cannot find a resource type explicitly targeted by bundle type " + newBundleType +
". This should not happen because such type should always be persisted prior to the bundle type.");
}
targetedResourceTypes.add(existingTargetType);
}
}
// If bundleType is not null then this in a bundle plugin and we do not need to do any further
// processing because a bundle plugin cannot define any other content.
// Also note that ANY changes to the newBundleType need to be made in here and NOT in the code
// above. This is because the above code can query the database during which the changes might
// be flushed to the DB (if at least 1 of those changes involved associating the new bundle type
// with an entity from the persistence context.
if (existingBundleType != null) {
newBundleType.setId(existingBundleType.getId());
}
newBundleType.setResourceType(existingType);
newBundleType.getExplicitlyTargetedResourceTypes().clear();
newBundleType.getExplicitlyTargetedResourceTypes().addAll(targetedResourceTypes);