}
@SuppressWarnings("unchecked")
@RequiredPermission(Permission.MANAGE_REPOSITORIES)
public void addContentSourcesToRepo(Subject subject, int repoId, int[] contentSourceIds) throws Exception {
Repo repo = entityManager.find(Repo.class, repoId);
if (repo == null) {
throw new Exception("There is no repo with an ID [" + repoId + "]");
}
repo.setLastModifiedDate(System.currentTimeMillis());
log.debug("User [" + subject + "] is adding content sources to repo [" + repo + "]");
ContentServerPluginContainer pc = ContentManagerHelper.getPluginContainer();
Query q = entityManager.createNamedQuery(PackageVersionContentSource.QUERY_FIND_BY_CONTENT_SOURCE_ID_NO_FETCH);
for (int id : contentSourceIds) {
ContentSource contentSource = entityManager.find(ContentSource.class, id);
if (contentSource == null) {
throw new Exception("There is no content source with id [" + id + "]");
}
Set<ContentSource> alreadyAssociatedContentSources = repo.getContentSources();
// Only add it if it's not already associated with this repo.
if (!alreadyAssociatedContentSources.contains(contentSource)) {
RepoContentSource repoContentSourceMapping = repo.addContentSource(contentSource);
entityManager.persist(repoContentSourceMapping);
}
Set<PackageVersion> alreadyAssociatedPackageVersions = new HashSet<PackageVersion>(
repo.getPackageVersions());
// Automatically associate all of the content source's package versions with this repo,
// but *skip* over the ones that are already linked to this repo from a previous association.
q.setParameter("id", contentSource.getId());
List<PackageVersionContentSource> pvcss = q.getResultList();