Search in sources :

Example 1 with OaiPmhDatabaseException

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

Example 2 with OaiPmhDatabaseException

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

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

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

the class AbstractOaiPmhDatabase method delete.

@Override
public void delete(String mediaPackageId, String repository) throws OaiPmhDatabaseException, NotFoundException {
    int i = 0;
    boolean success = false;
    while (!success && i < 5) {
        EntityManager em = null;
        EntityTransaction tx = null;
        try {
            em = getEmf().createEntityManager();
            tx = em.getTransaction();
            tx.begin();
            OaiPmhEntity oaiPmhEntity = getOaiPmhEntity(mediaPackageId, repository, em);
            if (oaiPmhEntity == null)
                throw new NotFoundException("No media package with id " + mediaPackageId + " exists");
            oaiPmhEntity.setDeleted(true);
            em.merge(oaiPmhEntity);
            tx.commit();
            success = true;
        } catch (NotFoundException e) {
            throw e;
        } catch (Exception e) {
            final String message = ExceptionUtils.getMessage(e.getCause()).toLowerCase();
            if (message.contains("unique") || message.contains("duplicate")) {
                try {
                    Thread.sleep(1100L);
                } catch (InterruptedException e1) {
                    throw new OaiPmhDatabaseException(e1);
                }
                i++;
                logger.info("Deleting OAI-PMH entry '{}' from  repository '{}' failed, retry {} times.", new String[] { mediaPackageId, repository, Integer.toString(i) });
            } else {
                logger.error("Could not delete mediapackage '{}' from OAI-PMH repository '{}': {}", new String[] { mediaPackageId, repository, ExceptionUtils.getStackTrace(e) });
                if (tx != null && tx.isActive())
                    tx.rollback();
                throw new OaiPmhDatabaseException(e);
            }
        } finally {
            if (em != null)
                em.close();
        }
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) OaiPmhEntity(org.opencastproject.oaipmh.persistence.OaiPmhEntity) EntityManager(javax.persistence.EntityManager) OaiPmhDatabaseException(org.opencastproject.oaipmh.persistence.OaiPmhDatabaseException) NotFoundException(org.opencastproject.util.NotFoundException) NoResultException(javax.persistence.NoResultException) OaiPmhDatabaseException(org.opencastproject.oaipmh.persistence.OaiPmhDatabaseException) NotFoundException(org.opencastproject.util.NotFoundException)

Example 5 with OaiPmhDatabaseException

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

the class AbstractOaiPmhDatabase method store.

@Override
public void store(MediaPackage mediaPackage, String repository) throws OaiPmhDatabaseException {
    int i = 0;
    boolean success = false;
    while (!success && i < 5) {
        EntityManager em = null;
        EntityTransaction tx = null;
        try {
            em = getEmf().createEntityManager();
            tx = em.getTransaction();
            tx.begin();
            OaiPmhEntity entity = getOaiPmhEntity(mediaPackage.getIdentifier().toString(), repository, em);
            if (entity == null) {
                // no entry found, create new entity
                entity = new OaiPmhEntity();
                updateEntity(entity, mediaPackage, repository);
                em.persist(entity);
            } else {
                // entry found, update existing
                updateEntity(entity, mediaPackage, repository);
                em.merge(entity);
            }
            tx.commit();
            success = true;
        } catch (Exception e) {
            final String message = ExceptionUtils.getMessage(e.getCause()).toLowerCase();
            if (message.contains("unique") || message.contains("duplicate")) {
                try {
                    Thread.sleep(1100L);
                } catch (InterruptedException e1) {
                    throw new OaiPmhDatabaseException(e1);
                }
                i++;
                logger.info("Storing OAI-PMH entry '{}' from  repository '{}' failed, retry {} times.", new String[] { mediaPackage.getIdentifier().toString(), repository, Integer.toString(i) });
            } else {
                logger.error("Could not store mediapackage '{}' to OAI-PMH repository '{}': {}", new String[] { mediaPackage.getIdentifier().toString(), repository, ExceptionUtils.getStackTrace(e) });
                if (tx != null && tx.isActive())
                    tx.rollback();
                throw new OaiPmhDatabaseException(e);
            }
        } finally {
            if (em != null)
                em.close();
        }
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) OaiPmhEntity(org.opencastproject.oaipmh.persistence.OaiPmhEntity) EntityManager(javax.persistence.EntityManager) OaiPmhDatabaseException(org.opencastproject.oaipmh.persistence.OaiPmhDatabaseException) NoResultException(javax.persistence.NoResultException) OaiPmhDatabaseException(org.opencastproject.oaipmh.persistence.OaiPmhDatabaseException) NotFoundException(org.opencastproject.util.NotFoundException)

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