Search in sources :

Example 6 with OaiPmhDatabaseException

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);
}
Also used : PublicationException(org.opencastproject.publication.api.PublicationException) OaiPmhDatabaseException(org.opencastproject.oaipmh.persistence.OaiPmhDatabaseException) ArrayList(java.util.ArrayList) Publication(org.opencastproject.mediapackage.Publication) NotFoundException(org.opencastproject.util.NotFoundException) SearchResult(org.opencastproject.oaipmh.persistence.SearchResult) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) DistributionException(org.opencastproject.distribution.api.DistributionException) Job(org.opencastproject.job.api.Job)

Aggregations

OaiPmhDatabaseException (org.opencastproject.oaipmh.persistence.OaiPmhDatabaseException)6 ArrayList (java.util.ArrayList)3 DistributionException (org.opencastproject.distribution.api.DistributionException)3 Job (org.opencastproject.job.api.Job)3 MediaPackage (org.opencastproject.mediapackage.MediaPackage)3 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)3 Publication (org.opencastproject.mediapackage.Publication)3 SearchResult (org.opencastproject.oaipmh.persistence.SearchResult)3 SearchResultItem (org.opencastproject.oaipmh.persistence.SearchResultItem)3 PublicationException (org.opencastproject.publication.api.PublicationException)3 NotFoundException (org.opencastproject.util.NotFoundException)3 HashSet (java.util.HashSet)2 EntityManager (javax.persistence.EntityManager)2 EntityTransaction (javax.persistence.EntityTransaction)2 NoResultException (javax.persistence.NoResultException)2 OaiPmhEntity (org.opencastproject.oaipmh.persistence.OaiPmhEntity)2 URI (java.net.URI)1 Hashtable (java.util.Hashtable)1 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)1 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)1