Search in sources :

Example 1 with OaiPmhEntity

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

the class AbstractOaiPmhDatabase method search.

@Override
public SearchResult search(Query query) {
    EntityManager em = null;
    try {
        em = getEmf().createEntityManager();
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<OaiPmhEntity> q = cb.createQuery(OaiPmhEntity.class);
        Root<OaiPmhEntity> c = q.from(OaiPmhEntity.class);
        q.select(c);
        // create predicates joined in an "and" expression
        final List<Predicate> predicates = new ArrayList<Predicate>();
        predicates.add(cb.equal(c.get("organization"), getSecurityService().getOrganization().getId()));
        for (String p : query.getMediaPackageId()) predicates.add(cb.equal(c.get("mediaPackageId"), p));
        for (String p : query.getRepositoryId()) predicates.add(cb.equal(c.get("repositoryId"), p));
        for (String p : query.getSeriesId()) predicates.add(cb.equal(c.get("series"), p));
        for (Boolean p : query.isDeleted()) predicates.add(cb.equal(c.get("deleted"), p));
        if (!query.isSubsequentRequest()) {
            for (Date p : query.getModifiedAfter()) predicates.add(cb.greaterThanOrEqualTo(c.get("modificationDate").as(Date.class), p));
        } else {
            for (Date p : query.getModifiedAfter()) predicates.add(cb.greaterThan(c.get("modificationDate").as(Date.class), p));
        }
        for (Date p : query.getModifiedBefore()) predicates.add(cb.lessThanOrEqualTo(c.get("modificationDate").as(Date.class), p));
        q.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
        q.orderBy(cb.asc(c.get("modificationDate")));
        TypedQuery<OaiPmhEntity> typedQuery = em.createQuery(q);
        for (int maxResult : query.getLimit()) typedQuery.setMaxResults(maxResult);
        for (int startPosition : query.getOffset()) typedQuery.setFirstResult(startPosition);
        return createSearchResult(typedQuery);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ArrayList(java.util.ArrayList) Date(java.util.Date) Predicate(javax.persistence.criteria.Predicate) OaiPmhEntity(org.opencastproject.oaipmh.persistence.OaiPmhEntity) EntityManager(javax.persistence.EntityManager)

Example 2 with OaiPmhEntity

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

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

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

OaiPmhEntity (org.opencastproject.oaipmh.persistence.OaiPmhEntity)4 EntityManager (javax.persistence.EntityManager)3 NoResultException (javax.persistence.NoResultException)3 OaiPmhDatabaseException (org.opencastproject.oaipmh.persistence.OaiPmhDatabaseException)3 NotFoundException (org.opencastproject.util.NotFoundException)3 ArrayList (java.util.ArrayList)2 EntityTransaction (javax.persistence.EntityTransaction)2 Date (java.util.Date)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 Predicate (javax.persistence.criteria.Predicate)1 SearchResultItem (org.opencastproject.oaipmh.persistence.SearchResultItem)1