use of org.opencastproject.oaipmh.persistence.OaiPmhDatabaseException in project opencast by opencast.
the class OaiPmhPublicationServiceImpl method publish.
protected Publication publish(Job job, MediaPackage mediaPackage, String repository, Set<String> downloadElementIds, Set<String> streamingElementIds, boolean checkAvailability) throws PublicationException, MediaPackageException {
String mpId = mediaPackage.getIdentifier().compact();
SearchResult searchResult = oaiPmhDatabase.search(QueryBuilder.queryRepo(repository).mediaPackageId(mpId).isDeleted(false).build());
// retract oai-pmh if published
if (searchResult.size() > 0) {
try {
Publication p = retract(job, mediaPackage, repository);
if (mediaPackage.contains(p))
mediaPackage.remove(p);
} catch (NotFoundException e) {
logger.debug("No OAI-PMH publication found for media package {}.", mpId, e);
// this is ok
}
}
List<Job> distributionJobs = new ArrayList<>(2);
if (downloadElementIds != null && !downloadElementIds.isEmpty()) {
// select elements for download distribution
MediaPackage mpDownloadDist = (MediaPackage) mediaPackage.clone();
for (MediaPackageElement mpe : mpDownloadDist.getElements()) {
if (downloadElementIds.contains(mpe.getIdentifier()))
continue;
mpDownloadDist.remove(mpe);
}
// publish to download
if (mpDownloadDist.getElements().length > 0) {
try {
Job downloadDistributionJob = downloadDistributionService.distribute(getPublicationChannelName(repository), mpDownloadDist, downloadElementIds, checkAvailability);
if (downloadDistributionJob != null) {
distributionJobs.add(downloadDistributionJob);
}
} catch (DistributionException e) {
throw new PublicationException(format("Unable to distribute media package %s to download distribution.", mpId), e);
}
}
}
if (streamingElementIds != null && !streamingElementIds.isEmpty()) {
// select elements for streaming distribution
MediaPackage mpStreamingDist = (MediaPackage) mediaPackage.clone();
for (MediaPackageElement mpe : mpStreamingDist.getElements()) {
if (streamingElementIds.contains(mpe.getIdentifier()))
continue;
mpStreamingDist.remove(mpe);
}
// publish to streaming
if (mpStreamingDist.getElements().length > 0) {
try {
Job streamingDistributionJob = streamingDistributionService.distribute(getPublicationChannelName(repository), mpStreamingDist, streamingElementIds);
if (streamingDistributionJob != null) {
distributionJobs.add(streamingDistributionJob);
}
} catch (DistributionException e) {
throw new PublicationException(format("Unable to distribute media package %s to streaming distribution.", mpId), e);
}
}
}
if (distributionJobs.size() < 0) {
throw new IllegalStateException(format("The media package %s does not contain any elements for publishing to OAI-PMH", mpId));
}
// wait for distribution jobs
if (!waitForJobs(job, serviceRegistry, distributionJobs).isSuccess()) {
throw new PublicationException(format("Unable to distribute elements of media package %s to distribution channels.", mpId));
}
List<MediaPackageElement> distributedElements = new ArrayList<>();
for (Job distributionJob : distributionJobs) {
String distributedElementsXml = distributionJob.getPayload();
if (StringUtils.isNotBlank(distributedElementsXml)) {
for (MediaPackageElement distributedElement : MediaPackageElementParser.getArrayFromXml(distributedElementsXml)) {
distributedElements.add(distributedElement);
}
}
}
MediaPackage oaiPmhDistMp = (MediaPackage) mediaPackage.clone();
// cleanup media package elements
for (MediaPackageElement mpe : oaiPmhDistMp.getElements()) {
// keep publications
if (MediaPackageElement.Type.Publication == mpe.getElementType())
continue;
oaiPmhDistMp.remove(mpe);
}
// ...add the distributed elements
for (MediaPackageElement mpe : distributedElements) {
oaiPmhDistMp.add(mpe);
}
// publish to oai-pmh
try {
oaiPmhDatabase.store(oaiPmhDistMp, repository);
} catch (OaiPmhDatabaseException e) {
// todo: should we retract the elements from download and streaming here?
throw new PublicationException(format("Unable to distribute media package %s to OAI-PMH repository %s", mpId, repository), e);
}
return createPublicationElement(mpId, repository);
}
Aggregations