Search in sources :

Example 6 with SearchResultItem

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

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

the class OaiPmhRepositoryTest method searchResultItem.

private SearchResultItem searchResultItem(String id, Date modified, boolean deleted) {
    final String seriesDcXml = IoSupport.loadFileFromClassPathAsString("/series-dublincore.xml").get();
    final String episodeDcXml = IoSupport.loadFileFromClassPathAsString("/episode-dublincore.xml").get();
    final DublinCoreCatalog seriesDc = DublinCores.read(IOUtils.toInputStream(seriesDcXml));
    final DublinCoreCatalog episodeDc = DublinCores.read(IOUtils.toInputStream(episodeDcXml));
    final String mpXml = IoSupport.loadFileFromClassPathAsString("/manifest-full.xml").get();
    final String xacml = IoSupport.loadFileFromClassPathAsString("/xacml.xml").get();
    // 
    SearchResultItem item = EasyMock.createNiceMock(SearchResultItem.class);
    EasyMock.expect(item.getModificationDate()).andReturn(modified).anyTimes();
    EasyMock.expect(item.getId()).andReturn(id).anyTimes();
    EasyMock.expect(item.isDeleted()).andReturn(deleted).anyTimes();
    EasyMock.expect(item.getMediaPackageXml()).andReturn(mpXml).anyTimes();
    SearchResultElementItem episodeDcElement = EasyMock.createNiceMock(SearchResultElementItem.class);
    EasyMock.expect(episodeDcElement.getType()).andReturn("catalog").anyTimes();
    EasyMock.expect(episodeDcElement.getFlavor()).andReturn("dublincore/episode").anyTimes();
    EasyMock.expect(episodeDcElement.getXml()).andReturn(episodeDcXml).anyTimes();
    EasyMock.expect(episodeDcElement.isEpisodeDublinCore()).andReturn(true).anyTimes();
    EasyMock.expect(episodeDcElement.isSeriesDublinCore()).andReturn(false).anyTimes();
    try {
        EasyMock.expect(episodeDcElement.asDublinCore()).andReturn(episodeDc).anyTimes();
    } catch (OaiPmhDatabaseException ex) {
    }
    SearchResultElementItem seriesDcElement = EasyMock.createNiceMock(SearchResultElementItem.class);
    EasyMock.expect(seriesDcElement.getType()).andReturn("catalog").anyTimes();
    EasyMock.expect(seriesDcElement.getFlavor()).andReturn("dublincore/series").anyTimes();
    EasyMock.expect(seriesDcElement.getXml()).andReturn(seriesDcXml).anyTimes();
    EasyMock.expect(seriesDcElement.isEpisodeDublinCore()).andReturn(false).anyTimes();
    EasyMock.expect(seriesDcElement.isSeriesDublinCore()).andReturn(true).anyTimes();
    try {
        EasyMock.expect(seriesDcElement.asDublinCore()).andReturn(seriesDc).anyTimes();
    } catch (OaiPmhDatabaseException ex) {
    }
    SearchResultElementItem securityXacmlElement = EasyMock.createNiceMock(SearchResultElementItem.class);
    EasyMock.expect(securityXacmlElement.getType()).andReturn("catalog").anyTimes();
    EasyMock.expect(securityXacmlElement.getFlavor()).andReturn("security/xacml+series").anyTimes();
    EasyMock.expect(securityXacmlElement.getXml()).andReturn(xacml).anyTimes();
    EasyMock.expect(securityXacmlElement.isEpisodeDublinCore()).andReturn(false).anyTimes();
    EasyMock.expect(securityXacmlElement.isSeriesDublinCore()).andReturn(false).anyTimes();
    try {
        EasyMock.expect(securityXacmlElement.asDublinCore()).andThrow(new OaiPmhDatabaseException("this is not a dublincore catalog")).anyTimes();
    } catch (OaiPmhDatabaseException ex) {
    }
    EasyMock.expect(item.getElements()).andReturn(Collections.list(episodeDcElement, seriesDcElement, securityXacmlElement)).anyTimes();
    try {
        EasyMock.expect(item.getEpisodeDublinCore()).andReturn(episodeDc).anyTimes();
        EasyMock.expect(item.getSeriesDublinCore()).andReturn(seriesDc).anyTimes();
    } catch (OaiPmhDatabaseException ex) {
    }
    EasyMock.replay(item, episodeDcElement, seriesDcElement, securityXacmlElement);
    return item;
}
Also used : OaiPmhDatabaseException(org.opencastproject.oaipmh.persistence.OaiPmhDatabaseException) SearchResultElementItem(org.opencastproject.oaipmh.persistence.SearchResultElementItem) SearchResultItem(org.opencastproject.oaipmh.persistence.SearchResultItem) XpathReturnType.returningAString(org.xmlmatchers.xpath.XpathReturnType.returningAString) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog)

Example 8 with SearchResultItem

use of org.opencastproject.oaipmh.persistence.SearchResultItem 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 9 with SearchResultItem

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

the class OaiXmlGen method resumptionToken.

/**
 * Create the resumption token and store the query.
 */
Node resumptionToken(final Option<String> resumptionToken, final String metadataPrefix, final SearchResult result, Date until, Option<String> set) {
    // compute the token value...
    final Option<Option<String>> token;
    if (result.size() == result.getLimit()) {
        SearchResultItem lastResult = result.getItems().get((int) (result.size() - 1));
        // more to come...
        token = some(some(repository.saveQuery(new ResumableQuery(metadataPrefix, lastResult.getModificationDate(), until, set))));
    } else if (resumptionToken.isSome()) {
        // last page reached
        token = some(Option.<String>none());
    } else {
        token = none();
    }
    // ... then transform it into a node
    return token.map(new Function<Option<String>, Node>() {

        @Override
        public Node apply(Option<String> token) {
            return $e("resumptionToken", // $a("cursor", Integer.toString(offset)),
            token.map(mkText).getOrElse(nodeZero));
        }
    }).getOrElse(nodeZero);
}
Also used : Function(org.opencastproject.util.data.Function) SearchResultItem(org.opencastproject.oaipmh.persistence.SearchResultItem) Option(org.opencastproject.util.data.Option)

Example 10 with SearchResultItem

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

the class OaiPmhPersistenceTest method testRemovalOfOrphanedElements.

@Test
public void testRemovalOfOrphanedElements() throws Exception {
    oaiPmhDatabase.store(mp1, REPOSITORY_ID_1);
    int count = 0;
    for (SearchResultItem searchResultItem : oaiPmhDatabase.search(queryRepo(REPOSITORY_ID_1).mediaPackageId(mp1).build()).getItems()) {
        count++;
        Assert.assertNotNull(searchResultItem.getMediaPackage());
        Assert.assertNotNull(searchResultItem.getMediaPackage().getElementById("catalog-1"));
    }
    Assert.assertEquals(1, count);
    mp1.removeElementById("catalog-1");
    oaiPmhDatabase.store(mp1, REPOSITORY_ID_1);
    count = 0;
    for (SearchResultItem searchResultItem : oaiPmhDatabase.search(queryRepo(REPOSITORY_ID_1).mediaPackageId(mp1).build()).getItems()) {
        count++;
        Assert.assertNotNull(searchResultItem.getMediaPackage());
        Assert.assertNull(searchResultItem.getMediaPackage().getElementById("catalog-1"));
    }
    Assert.assertEquals(1, count);
}
Also used : SearchResultItem(org.opencastproject.oaipmh.persistence.SearchResultItem) Test(org.junit.Test)

Aggregations

SearchResultItem (org.opencastproject.oaipmh.persistence.SearchResultItem)13 SearchResult (org.opencastproject.oaipmh.persistence.SearchResult)8 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 MediaPackage (org.opencastproject.mediapackage.MediaPackage)4 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)4 OaiPmhDatabaseException (org.opencastproject.oaipmh.persistence.OaiPmhDatabaseException)4 Job (org.opencastproject.job.api.Job)3 HashSet (java.util.HashSet)2 DistributionException (org.opencastproject.distribution.api.DistributionException)2 Publication (org.opencastproject.mediapackage.Publication)2 OaiPmhDatabase (org.opencastproject.oaipmh.persistence.OaiPmhDatabase)2 Query (org.opencastproject.oaipmh.persistence.Query)2 SearchResultElementItem (org.opencastproject.oaipmh.persistence.SearchResultElementItem)2 PublicationException (org.opencastproject.publication.api.PublicationException)2 XpathReturnType.returningAString (org.xmlmatchers.xpath.XpathReturnType.returningAString)2 URI (java.net.URI)1 Date (java.util.Date)1 Hashtable (java.util.Hashtable)1 NoResultException (javax.persistence.NoResultException)1