Search in sources :

Example 1 with SearchResultItem

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

the class OaiPmhUpdatedEventHandler method handleEvent.

public void handleEvent(AssetManagerItem.TakeSnapshot snapshotItem) {
    if (!propagateEpisode) {
        logger.trace("Skipping automatic propagation of episode meta data to OAI-PMH since it is turned off.");
        return;
    }
    // An episode or its ACL has been updated. Construct the MediaPackage and publish it to OAI-PMH.
    logger.debug("Handling update event for media package {}", snapshotItem.getMediapackage().getIdentifier().compact());
    // We must be an administrative user to make a query to the OaiPmhPublicationService
    final User prevUser = securityService.getUser();
    final Organization prevOrg = securityService.getOrganization();
    try {
        securityService.setUser(SecurityUtil.createSystemUser(systemAccount, prevOrg));
        // Check weather the media package contains elements to republish
        MediaPackage snapshotMp = snapshotItem.getMediapackage();
        SimpleElementSelector mpeSelector = new SimpleElementSelector();
        for (String flavor : flavors) {
            mpeSelector.addFlavor(flavor);
        }
        for (String tag : tags) {
            mpeSelector.addTag(tag);
        }
        Collection<MediaPackageElement> elementsToUpdate = mpeSelector.select(snapshotMp, true);
        if (elementsToUpdate == null || elementsToUpdate.isEmpty()) {
            logger.debug("The media package {} does not contain any elements matching the given flavors and tags", snapshotMp.getIdentifier().compact());
            return;
        }
        SearchResult result = oaiPmhPersistence.search(QueryBuilder.query().mediaPackageId(snapshotMp).isDeleted(false).build());
        for (SearchResultItem searchResultItem : result.getItems()) {
            try {
                Job job = oaiPmhPublicationService.updateMetadata(snapshotMp, searchResultItem.getRepository(), flavors, tags, false);
            // we don't want to wait for job completion here because it will block the message queue
            } catch (Exception e) {
                logger.error("Unable to update OAI-PMH publication for the media package {} in repository {}", snapshotItem.getMediapackage().getIdentifier().compact(), searchResultItem.getRepository(), e);
            }
        }
    } finally {
        securityService.setOrganization(prevOrg);
        securityService.setUser(prevUser);
    }
}
Also used : User(org.opencastproject.security.api.User) Organization(org.opencastproject.security.api.Organization) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) SearchResultItem(org.opencastproject.oaipmh.persistence.SearchResultItem) SearchResult(org.opencastproject.oaipmh.persistence.SearchResult) SimpleElementSelector(org.opencastproject.mediapackage.selector.SimpleElementSelector) Job(org.opencastproject.job.api.Job) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 2 with SearchResultItem

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

the class OaiPmhUpdatedEventHandlerTest method mockOaiPmhDatabase.

private void mockOaiPmhDatabase() {
    SearchResult searchResultMock = mock(MockType.NICE, SearchResult.class);
    SearchResultItem searchResultItemMock = mock(MockType.NICE, SearchResultItem.class);
    expect(searchResultMock.getItems()).andReturn(Collections.singletonList(searchResultItemMock)).anyTimes();
    expect(searchResultItemMock.getRepository()).andReturn(OAIPMH_REPOSITORY).anyTimes();
    queryCapture = Capture.newInstance();
    expect(oaiPmhDatabaseMock.search(capture(queryCapture))).andReturn(searchResultMock);
}
Also used : SearchResultItem(org.opencastproject.oaipmh.persistence.SearchResultItem) SearchResult(org.opencastproject.oaipmh.persistence.SearchResult)

Example 3 with SearchResultItem

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

the class AbstractOaiPmhDatabase method createSearchResult.

/**
 * Creates a search result from a given JPA query
 *
 * @param query
 *          the query
 * @return The search result.
 */
private SearchResult createSearchResult(TypedQuery<OaiPmhEntity> query) {
    // Create and configure the query result
    final long offset = query.getFirstResult();
    final long limit = query.getMaxResults() != Integer.MAX_VALUE ? query.getMaxResults() : 0;
    final List<SearchResultItem> items = new ArrayList<>();
    for (OaiPmhEntity oaipmhEntity : query.getResultList()) {
        try {
            items.add(new SearchResultItemImpl(oaipmhEntity));
        } catch (Exception ex) {
            logger.warn("Unable to parse an OAI-PMH database entry", ex);
        }
    }
    return new SearchResultImpl(offset, limit, items);
}
Also used : OaiPmhEntity(org.opencastproject.oaipmh.persistence.OaiPmhEntity) SearchResultItem(org.opencastproject.oaipmh.persistence.SearchResultItem) ArrayList(java.util.ArrayList) NoResultException(javax.persistence.NoResultException) OaiPmhDatabaseException(org.opencastproject.oaipmh.persistence.OaiPmhDatabaseException) NotFoundException(org.opencastproject.util.NotFoundException)

Example 4 with SearchResultItem

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

the class OaiPmhPersistenceTest method testRetrieving.

@Test
public void testRetrieving() throws Exception {
    oaiPmhDatabase.store(mp1, REPOSITORY_ID_1);
    SearchResult search = oaiPmhDatabase.search(query().mediaPackageId(mp1).build());
    Assert.assertEquals(1, search.size());
    SearchResultItem searchResultItem = search.getItems().get(0);
    Assert.assertEquals(REPOSITORY_ID_1, searchResultItem.getRepository());
    Assert.assertEquals(mp1.getIdentifier().toString(), searchResultItem.getId());
    Assert.assertEquals(mp1, searchResultItem.getMediaPackage());
    Assert.assertFalse(searchResultItem.isDeleted());
    Assert.assertEquals(DefaultOrganization.DEFAULT_ORGANIZATION_ID, searchResultItem.getOrganization());
    Assert.assertTrue(searchResultItem.getModificationDate() != null);
    Date modificationDate = searchResultItem.getModificationDate();
    Assert.assertEquals(2, searchResultItem.getElements().size());
    for (SearchResultElementItem catalogItem : searchResultItem.getElements()) {
        Assert.assertNotNull(catalogItem.getFlavor());
        Assert.assertNotNull(catalogItem.getXml());
        Assert.assertEquals("dublincore/episode".equals(catalogItem.getFlavor().toLowerCase()), catalogItem.isEpisodeDublinCore());
        Assert.assertEquals("dublincore/series".equals(catalogItem.getFlavor().toLowerCase()), catalogItem.isSeriesDublinCore());
    }
    Date dateBeforeStoring = new Date();
    oaiPmhDatabase.store(mp1, REPOSITORY_ID_2);
    search = oaiPmhDatabase.search(query().mediaPackageId(mp1).build());
    Assert.assertEquals(2, search.size());
    search = oaiPmhDatabase.search(query().mediaPackageId(mp1).repositoryId(REPOSITORY_ID_2).build());
    Assert.assertEquals(1, search.size());
    searchResultItem = search.getItems().get(0);
    Assert.assertEquals(REPOSITORY_ID_2, searchResultItem.getRepository());
    Assert.assertEquals(mp1.getIdentifier().toString(), searchResultItem.getId());
    Assert.assertEquals(mp1, searchResultItem.getMediaPackage());
    Assert.assertFalse(searchResultItem.isDeleted());
    Assert.assertEquals(DefaultOrganization.DEFAULT_ORGANIZATION_ID, searchResultItem.getOrganization());
    Assert.assertTrue(searchResultItem.getModificationDate() != null);
    Assert.assertTrue(searchResultItem.getModificationDate().after(modificationDate));
    Assert.assertEquals(2, searchResultItem.getElements().size());
    for (SearchResultElementItem catalogItem : searchResultItem.getElements()) {
        Assert.assertNotNull(catalogItem.getFlavor());
        Assert.assertNotNull(catalogItem.getXml());
        Assert.assertEquals("dublincore/episode".equals(catalogItem.getFlavor().toLowerCase()), catalogItem.isEpisodeDublinCore());
        Assert.assertEquals("dublincore/series".equals(catalogItem.getFlavor().toLowerCase()), catalogItem.isSeriesDublinCore());
    }
    search = oaiPmhDatabase.search(query().mediaPackageId(mp1).repositoryId(REPOSITORY_ID_1).build());
    Assert.assertEquals(1, search.size());
    search = oaiPmhDatabase.search(query().mediaPackageId(mp1).repositoryId(REPOSITORY_ID_2).build());
    Assert.assertEquals(1, search.size());
    search = oaiPmhDatabase.search(query().mediaPackageId(mp1).repositoryId(REPOSITORY_ID_2).modifiedAfter(new Date()).build());
    Assert.assertEquals(0, search.size());
    search = oaiPmhDatabase.search(query().mediaPackageId(mp1).repositoryId(REPOSITORY_ID_2).modifiedAfter(dateBeforeStoring).build());
    Assert.assertEquals(1, search.size());
    search = oaiPmhDatabase.search(query().modifiedAfter(dateBeforeStoring).build());
    Assert.assertEquals(1, search.size());
    Date dateBeforeDeletion = new Date();
    oaiPmhDatabase.delete(mp2.getIdentifier().toString(), REPOSITORY_ID_2);
    Thread.sleep(10);
    search = oaiPmhDatabase.search(query().modifiedAfter(new Date()).build());
    Assert.assertEquals(0, search.size());
    search = oaiPmhDatabase.search(query().modifiedAfter(dateBeforeDeletion).build());
    Assert.assertEquals(1, search.size());
}
Also used : SearchResultElementItem(org.opencastproject.oaipmh.persistence.SearchResultElementItem) SearchResultItem(org.opencastproject.oaipmh.persistence.SearchResultItem) SearchResult(org.opencastproject.oaipmh.persistence.SearchResult) Date(java.util.Date) Test(org.junit.Test)

Example 5 with SearchResultItem

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

the class OaiPmhPublicationServiceImpl method retract.

protected Publication retract(Job job, MediaPackage mediaPackage, String repository) throws PublicationException, NotFoundException {
    String mpId = mediaPackage.getIdentifier().compact();
    // track elements for retraction
    MediaPackage oaiPmhMp = null;
    SearchResult searchResult = oaiPmhDatabase.search(QueryBuilder.queryRepo(repository).mediaPackageId(mpId).isDeleted(false).build());
    for (SearchResultItem searchResultItem : searchResult.getItems()) {
        if (oaiPmhMp == null) {
            oaiPmhMp = searchResultItem.getMediaPackage();
        } else {
            for (MediaPackageElement mpe : searchResultItem.getMediaPackage().getElements()) {
                oaiPmhMp.add(mpe);
            }
        }
    }
    // retract oai-pmh
    try {
        oaiPmhDatabase.delete(mpId, repository);
    } catch (OaiPmhDatabaseException e) {
        throw new PublicationException(format("Unable to retract media package %s from OAI-PMH repository %s", mpId, repository), e);
    }
    if (oaiPmhMp != null && oaiPmhMp.getElements().length > 0) {
        // retract files from distribution channels
        Set<String> mpeIds = new HashSet<>();
        for (MediaPackageElement mpe : oaiPmhMp.elements()) {
            if (MediaPackageElement.Type.Publication == mpe.getElementType())
                continue;
            mpeIds.add(mpe.getIdentifier());
        }
        if (!mpeIds.isEmpty()) {
            List<Job> retractionJobs = new ArrayList<>();
            // retract download
            try {
                Job retractDownloadJob = downloadDistributionService.retract(getPublicationChannelName(repository), oaiPmhMp, mpeIds);
                if (retractDownloadJob != null) {
                    retractionJobs.add(retractDownloadJob);
                }
            } catch (DistributionException e) {
                throw new PublicationException(format("Unable to create retraction job from distribution channel download for the media package %s ", mpId), e);
            }
            // retract streaming
            try {
                Job retractDownloadJob = streamingDistributionService.retract(getPublicationChannelName(repository), oaiPmhMp, mpeIds);
                if (retractDownloadJob != null) {
                    retractionJobs.add(retractDownloadJob);
                }
            } catch (DistributionException e) {
                throw new PublicationException(format("Unable to create retraction job from distribution channel streaming for the media package %s ", mpId), e);
            }
            if (retractionJobs.size() > 0) {
                // wait for distribution jobs
                if (!waitForJobs(job, serviceRegistry, retractionJobs).isSuccess())
                    throw new PublicationException(format("Unable to retract elements of media package %s from distribution channels.", mpId));
            }
        }
    }
    String publicationChannel = getPublicationChannelName(repository);
    for (Publication p : mediaPackage.getPublications()) {
        if (StringUtils.equals(publicationChannel, p.getChannel()))
            return p;
    }
    return null;
}
Also used : OaiPmhDatabaseException(org.opencastproject.oaipmh.persistence.OaiPmhDatabaseException) PublicationException(org.opencastproject.publication.api.PublicationException) SearchResultItem(org.opencastproject.oaipmh.persistence.SearchResultItem) ArrayList(java.util.ArrayList) Publication(org.opencastproject.mediapackage.Publication) 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) HashSet(java.util.HashSet)

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