Search in sources :

Example 6 with SearchResult

use of org.opencastproject.oaipmh.persistence.SearchResult in project opencast by opencast.

the class OaiPmhPublicationServiceImpl method updateMetadata.

protected Publication updateMetadata(Job job, MediaPackage mediaPackage, String repository, Set<String> flavors, Set<String> tags, boolean checkAvailability) throws PublicationException {
    final Set<MediaPackageElementFlavor> parsedFlavors = new HashSet<>();
    for (String flavor : flavors) {
        parsedFlavors.add(MediaPackageElementFlavor.parseFlavor(flavor));
    }
    final MediaPackage filteredMp;
    final SearchResult result = oaiPmhDatabase.search(QueryBuilder.queryRepo(repository).mediaPackageId(mediaPackage).isDeleted(false).build());
    if (result.size() == 1) {
        // apply tags and flavors to the current media package
        try {
            logger.debug("filter elements with flavors {} and tags {} on media package {}", StringUtils.join(flavors, ", "), StringUtils.join(tags, ", "), MediaPackageParser.getAsXml(mediaPackage));
            filteredMp = filterMediaPackage(mediaPackage, parsedFlavors, tags);
        } catch (MediaPackageException e) {
            throw new PublicationException("Error filtering media package", e);
        }
    } else if (result.size() == 0) {
        logger.info(format("Skipping update of media package %s since it is not currently published to %s", mediaPackage, repository));
        return null;
    } else {
        final String msg = format("More than one media package with id %s found", mediaPackage.getIdentifier().compact());
        logger.warn(msg);
        throw new PublicationException(msg);
    }
    // re-distribute elements to download
    Set<String> elementIdsToDistribute = new HashSet<>();
    for (MediaPackageElement mpe : filteredMp.getElements()) {
        // do not distribute publications
        if (MediaPackageElement.Type.Publication == mpe.getElementType())
            continue;
        elementIdsToDistribute.add(mpe.getIdentifier());
    }
    if (elementIdsToDistribute.isEmpty()) {
        logger.debug("The media package {} does not contain any elements to update. " + "Skip OAI-PMH metadata update operation for repository {}", mediaPackage.getIdentifier().compact(), repository);
        return null;
    }
    logger.debug("distribute elements {}", StringUtils.join(elementIdsToDistribute, ", "));
    final List<MediaPackageElement> distributedElements = new ArrayList<>();
    try {
        Job distJob = downloadDistributionService.distribute(getPublicationChannelName(repository), filteredMp, elementIdsToDistribute, checkAvailability);
        if (job == null)
            throw new PublicationException("The distribution service can not handle this type of media package elements.");
        if (!waitForJobs(job, serviceRegistry, distJob).isSuccess()) {
            throw new PublicationException(format("Unable to distribute updated elements from media package %s to the download distribution service", mediaPackage.getIdentifier().compact()));
        }
        if (distJob.getPayload() != null) {
            for (MediaPackageElement mpe : MediaPackageElementParser.getArrayFromXml(distJob.getPayload())) {
                distributedElements.add(mpe);
            }
        }
    } catch (DistributionException | MediaPackageException e) {
        throw new PublicationException(format("Unable to distribute updated elements from media package %s to the download distribution service", mediaPackage.getIdentifier().compact()), e);
    }
    // update elements (URLs)
    for (MediaPackageElement e : filteredMp.getElements()) {
        if (MediaPackageElement.Type.Publication.equals(e.getElementType()))
            continue;
        filteredMp.remove(e);
    }
    for (MediaPackageElement e : distributedElements) {
        filteredMp.add(e);
    }
    MediaPackage publishedMp = merge(filteredMp, removeMatchingNonExistantElements(filteredMp, (MediaPackage) result.getItems().get(0).getMediaPackage().clone(), parsedFlavors, tags));
    // Does the media package have a title and track?
    if (!MediaPackageSupport.isPublishable(publishedMp)) {
        throw new PublicationException("Media package does not meet criteria for publication");
    }
    // Publish the media package to OAI-PMH
    try {
        logger.debug(format("Updating metadata of media package %s in %s", publishedMp.getIdentifier().compact(), repository));
        oaiPmhDatabase.store(publishedMp, repository);
    } catch (OaiPmhDatabaseException e) {
        throw new PublicationException(format("Media package %s could not be updated", publishedMp.getIdentifier().compact()));
    }
    // retract orphaned elements from download distribution
    // orphaned elements are all those elements to which the updated media package no longer refers (in terms of element uri)
    Map<URI, MediaPackageElement> elementUriMap = new Hashtable<>();
    for (SearchResultItem oaiPmhSearchResultItem : result.getItems()) {
        for (MediaPackageElement mpe : oaiPmhSearchResultItem.getMediaPackage().getElements()) {
            if (MediaPackageElement.Type.Publication == mpe.getElementType() || null == mpe.getURI())
                continue;
            elementUriMap.put(mpe.getURI(), mpe);
        }
    }
    for (MediaPackageElement publishedMpe : publishedMp.getElements()) {
        if (MediaPackageElement.Type.Publication == publishedMpe.getElementType())
            continue;
        if (elementUriMap.containsKey(publishedMpe.getURI()))
            elementUriMap.remove(publishedMpe.getURI());
    }
    Set<String> orphanedElementIds = new HashSet<>();
    for (MediaPackageElement orphanedMpe : elementUriMap.values()) {
        orphanedElementIds.add(orphanedMpe.getIdentifier());
    }
    if (!orphanedElementIds.isEmpty()) {
        for (SearchResultItem oaiPmhSearchResultItem : result.getItems()) {
            try {
                Job retractJob = downloadDistributionService.retract(getPublicationChannelName(repository), oaiPmhSearchResultItem.getMediaPackage(), orphanedElementIds);
                if (retractJob != null) {
                    if (!waitForJobs(job, serviceRegistry, retractJob).isSuccess())
                        logger.warn("The download distribution retract job for the orphaned elements from media package {} does not end successfully", oaiPmhSearchResultItem.getMediaPackage().getIdentifier().compact());
                }
            } catch (DistributionException e) {
                logger.warn("Unable to retract orphaned elements from download distribution service for the media package {} channel {}", oaiPmhSearchResultItem.getMediaPackage().getIdentifier().compact(), getPublicationChannelName(repository), e);
            }
        }
    }
    // return the publication
    String publicationChannel = getPublicationChannelName(repository);
    for (Publication p : mediaPackage.getPublications()) {
        if (StringUtils.equals(publicationChannel, p.getChannel()))
            return p;
    }
    return null;
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) PublicationException(org.opencastproject.publication.api.PublicationException) OaiPmhDatabaseException(org.opencastproject.oaipmh.persistence.OaiPmhDatabaseException) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) SearchResultItem(org.opencastproject.oaipmh.persistence.SearchResultItem) Publication(org.opencastproject.mediapackage.Publication) SearchResult(org.opencastproject.oaipmh.persistence.SearchResult) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) URI(java.net.URI) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) DistributionException(org.opencastproject.distribution.api.DistributionException) Job(org.opencastproject.job.api.Job) HashSet(java.util.HashSet)

Example 7 with SearchResult

use of org.opencastproject.oaipmh.persistence.SearchResult in project opencast by opencast.

the class OaiPmhRepositoryTest method testResumption.

@Ignore
@Test
@SuppressWarnings("unchecked")
public void testResumption() throws Exception {
    List<SearchResultItem> items1 = new ArrayList<SearchResultItem>();
    items1.add(searchResultItem("id-1", utcDate(2011, 5, 10), false));
    items1.add(searchResultItem("id-2", utcDate(2011, 5, 11), false));
    items1.add(searchResultItem("id-3", utcDate(2011, 5, 12), false));
    List<SearchResultItem> items2 = new ArrayList<SearchResultItem>();
    items2.add(searchResultItem("id-4", utcDate(2011, 5, 13), false));
    items2.add(searchResultItem("id-5", utcDate(2011, 5, 14), false));
    // setup episode service mock
    // this setup is really ugly since it needs knowledge about implementation details
    OaiPmhDatabase persistence = EasyMock.createMock(OaiPmhDatabase.class);
    SearchResult result = EasyMock.createMock(SearchResult.class);
    EasyMock.expect(result.getItems()).andReturn(items1).times(3).andReturn(items2).times(3);
    EasyMock.expect(result.getLimit()).andReturn(RESULT_LIMIT).anyTimes();
    EasyMock.expect(result.getOffset()).andReturn(0L).times(3).andReturn(RESULT_LIMIT).anyTimes();
    EasyMock.expect(result.size()).andReturn((long) items1.size()).times(4).andReturn((long) items2.size()).times(4);
    EasyMock.expect(persistence.search(EasyMock.<Query>anyObject())).andReturn(result).anyTimes();
    EasyMock.replay(persistence);
    EasyMock.replay(result);
    // do testing
    final OaiPmhRepository repo = repo(persistence, Granularity.DAY);
    runChecks(OaiPmhConstants.VERB_LIST_IDENTIFIERS, repo.selectVerb(params("ListIdentifiers", null, "oai_dc", null, null, null)), some(IsValid), list(hasXPath("count(//oai20:ListIdentifiers/oai20:header)", NS_CTX, returningANumber(), equalTo(3.0)), hasXPath("//oai20:ListIdentifiers/oai20:resumptionToken/text()", NS_CTX, returningAString(), equalTo("r-token")), hasXPath("//oai20:ListIdentifiers/oai20:header[1]/oai20:identifier/text()", NS_CTX, returningAString(), equalTo("id-1")), hasXPath("//oai20:ListIdentifiers/oai20:header[2]/oai20:identifier/text()", NS_CTX, returningAString(), equalTo("id-2")), hasXPath("//oai20:ListIdentifiers/oai20:header[3]/oai20:identifier/text()", NS_CTX, returningAString(), equalTo("id-3"))));
    // resume query
    runChecks(OaiPmhConstants.VERB_LIST_IDENTIFIERS, repo.selectVerb(params("ListIdentifiers", null, null, null, null, "r-token")), some(IsValid), list(hasXPath("count(//oai20:ListIdentifiers/oai20:header)", NS_CTX, returningANumber(), equalTo(2.0)), hasXPath("//oai20:ListIdentifiers/oai20:header[1]/oai20:identifier/text()", NS_CTX, returningAString(), equalTo("id-4")), hasXPath("//oai20:ListIdentifiers/oai20:header[2]/oai20:identifier/text()", NS_CTX, returningAString(), equalTo("id-5")), // token must be empty now since there are no more pages
    hasXPath("//oai20:ListIdentifiers/oai20:resumptionToken/text()", NS_CTX, returningAString(), equalTo(""))));
    EasyMock.verify(repo.getPersistence());
}
Also used : Query(org.opencastproject.oaipmh.persistence.Query) SearchResultItem(org.opencastproject.oaipmh.persistence.SearchResultItem) ArrayList(java.util.ArrayList) SearchResult(org.opencastproject.oaipmh.persistence.SearchResult) OaiPmhDatabase(org.opencastproject.oaipmh.persistence.OaiPmhDatabase) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with SearchResult

use of org.opencastproject.oaipmh.persistence.SearchResult in project opencast by opencast.

the class OaiPmhPersistenceTest method testLimitOffset.

@Test
public void testLimitOffset() throws Exception {
    oaiPmhDatabase.store(mp1, REPOSITORY_ID_1);
    MediaPackage mp2 = (MediaPackage) mp1.clone();
    mp2.setIdentifier(IdBuilderFactory.newInstance().newIdBuilder().createNew());
    oaiPmhDatabase.store(mp2, REPOSITORY_ID_2);
    SearchResult search = oaiPmhDatabase.search(query().limit(2).build());
    Assert.assertEquals(2, search.size());
    search = oaiPmhDatabase.search(query().limit(1).build());
    Assert.assertEquals(1, search.size());
    Assert.assertEquals(mp1.getIdentifier().toString(), search.getItems().get(0).getId());
    search = oaiPmhDatabase.search(query().offset(1).build());
    Assert.assertEquals(1, search.size());
    Assert.assertEquals(mp2.getIdentifier().toString(), search.getItems().get(0).getId());
}
Also used : MediaPackage(org.opencastproject.mediapackage.MediaPackage) SearchResult(org.opencastproject.oaipmh.persistence.SearchResult) Test(org.junit.Test)

Example 9 with SearchResult

use of org.opencastproject.oaipmh.persistence.SearchResult in project opencast by opencast.

the class OaiPmhUpdatedEventHandlerTest method testEpisodeNotKnownByOaiPmh.

/**
 * Tests if publishing to OAI-PMH is skipped, if the episode is not known by OAI-PMH.
 */
@Test
public void testEpisodeNotKnownByOaiPmh() throws Exception {
    Catalog episodeCatalog = CatalogImpl.newInstance();
    episodeCatalog.setURI(URI.create("/episode.xml"));
    episodeCatalog.setFlavor(MediaPackageElementFlavor.parseFlavor("dublincore/episode"));
    episodeCatalog.addTag("archive");
    MediaPackage updatedMp = createMediaPackage(episodeCatalog);
    // these are the interactions we expect with the security service
    mockSecurityService();
    // mock the OAI-PMH database
    SearchResult searchResultMock = mock(MockType.NICE, SearchResult.class);
    expect(searchResultMock.getItems()).andReturn(Collections.EMPTY_LIST).anyTimes();
    queryCapture = Capture.newInstance();
    expect(oaiPmhDatabaseMock.search(capture(queryCapture))).andReturn(searchResultMock);
    replayAll();
    cut.handleEvent(createSnapshot(updatedMp));
    // the OAI-PMH publication service should not be called as the media package isn't mocked in the OAI-PMH database
    verifyAll();
}
Also used : MediaPackage(org.opencastproject.mediapackage.MediaPackage) SearchResult(org.opencastproject.oaipmh.persistence.SearchResult) Catalog(org.opencastproject.mediapackage.Catalog) Test(org.junit.Test)

Example 10 with SearchResult

use of org.opencastproject.oaipmh.persistence.SearchResult 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

SearchResult (org.opencastproject.oaipmh.persistence.SearchResult)12 SearchResultItem (org.opencastproject.oaipmh.persistence.SearchResultItem)8 MediaPackage (org.opencastproject.mediapackage.MediaPackage)7 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 Job (org.opencastproject.job.api.Job)4 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)4 DistributionException (org.opencastproject.distribution.api.DistributionException)3 Publication (org.opencastproject.mediapackage.Publication)3 OaiPmhDatabaseException (org.opencastproject.oaipmh.persistence.OaiPmhDatabaseException)3 PublicationException (org.opencastproject.publication.api.PublicationException)3 HashSet (java.util.HashSet)2 OaiPmhDatabase (org.opencastproject.oaipmh.persistence.OaiPmhDatabase)2 Query (org.opencastproject.oaipmh.persistence.Query)2 NotFoundException (org.opencastproject.util.NotFoundException)2 URI (java.net.URI)1 Date (java.util.Date)1 Hashtable (java.util.Hashtable)1 Ignore (org.junit.Ignore)1 Catalog (org.opencastproject.mediapackage.Catalog)1