Search in sources :

Example 16 with SeriesServiceDatabaseException

use of org.opencastproject.series.impl.SeriesServiceDatabaseException in project opencast by opencast.

the class SeriesServiceDatabaseImpl method updateOptOutStatus.

/**
 * Updates a series' opt out status.
 *
 * @param seriesId
 *          The id of the series to update the opt out status of.
 * @param optOut
 *          Whether to opt out this series or not.
 */
@Override
public void updateOptOutStatus(String seriesId, boolean optOut) throws NotFoundException, SeriesServiceDatabaseException {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        SeriesEntity entity = getSeriesEntity(seriesId, em);
        if (entity == null)
            throw new NotFoundException("Series with ID " + seriesId + " does not exist");
        entity.setOptOut(optOut);
        em.merge(entity);
        tx.commit();
    } catch (NotFoundException e) {
        throw e;
    } catch (Exception e) {
        if (tx.isActive()) {
            tx.rollback();
        }
        logger.error("Could not update series opted out status: {}", e.getMessage());
        throw new SeriesServiceDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) SeriesServiceDatabaseException(org.opencastproject.series.impl.SeriesServiceDatabaseException) NotFoundException(org.opencastproject.util.NotFoundException) NoResultException(javax.persistence.NoResultException) SeriesServiceDatabaseException(org.opencastproject.series.impl.SeriesServiceDatabaseException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) AccessControlParsingException(org.opencastproject.security.api.AccessControlParsingException)

Example 17 with SeriesServiceDatabaseException

use of org.opencastproject.series.impl.SeriesServiceDatabaseException in project opencast by opencast.

the class SeriesServiceDatabaseImpl method getSeriesProperty.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.series.impl.SeriesServiceDatabase#getSeriesProperty(java.lang.String, java.lang.String)
 */
@Override
public String getSeriesProperty(String seriesId, String propertyName) throws NotFoundException, SeriesServiceDatabaseException {
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        SeriesEntity entity = getSeriesEntity(seriesId, em);
        if (entity == null) {
            throw new NotFoundException("No series with id=" + seriesId + " exists");
        }
        if (!userHasReadAccess(entity)) {
            throw new UnauthorizedException(securityService.getUser() + " is not authorized to see series " + seriesId + " properties");
        }
        if (entity.getProperties() == null || StringUtils.isBlank(entity.getProperties().get(propertyName))) {
            throw new NotFoundException("No series property for series with id=" + seriesId + " and property name " + propertyName);
        }
        return entity.getProperties().get(propertyName);
    } catch (NotFoundException e) {
        throw e;
    } catch (Exception e) {
        logger.error("Could not update series: {}", e.getMessage());
        if (tx.isActive()) {
            tx.rollback();
        }
        throw new SeriesServiceDatabaseException(e);
    } finally {
        em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) SeriesServiceDatabaseException(org.opencastproject.series.impl.SeriesServiceDatabaseException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) NoResultException(javax.persistence.NoResultException) SeriesServiceDatabaseException(org.opencastproject.series.impl.SeriesServiceDatabaseException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) AccessControlParsingException(org.opencastproject.security.api.AccessControlParsingException)

Example 18 with SeriesServiceDatabaseException

use of org.opencastproject.series.impl.SeriesServiceDatabaseException in project opencast by opencast.

the class SeriesServiceDatabaseImpl method storeSeriesElement.

@Override
public boolean storeSeriesElement(String seriesId, String type, byte[] data) throws SeriesServiceDatabaseException {
    EntityManager em = null;
    EntityTransaction tx = null;
    final boolean success;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        SeriesEntity series = getSeriesEntity(seriesId, em);
        if (series == null) {
            success = false;
        } else {
            series.addElement(type, data);
            em.merge(series);
            tx.commit();
            success = true;
        }
    } catch (Exception e) {
        if (tx.isActive()) {
            tx.rollback();
        }
        throw new SeriesServiceDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
    return success;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) SeriesServiceDatabaseException(org.opencastproject.series.impl.SeriesServiceDatabaseException) NoResultException(javax.persistence.NoResultException) SeriesServiceDatabaseException(org.opencastproject.series.impl.SeriesServiceDatabaseException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) AccessControlParsingException(org.opencastproject.security.api.AccessControlParsingException)

Example 19 with SeriesServiceDatabaseException

use of org.opencastproject.series.impl.SeriesServiceDatabaseException in project opencast by opencast.

the class SeriesServiceDatabaseImpl method storeSeries.

/*
   * (non-Javadoc)
   *
   * @see org.opencastproject.series.impl.SeriesServiceDatabase#storeSeries(org.opencastproject.metadata.dublincore.
   * DublinCoreCatalog)
   */
@Override
public DublinCoreCatalog storeSeries(DublinCoreCatalog dc) throws SeriesServiceDatabaseException, UnauthorizedException {
    if (dc == null) {
        throw new SeriesServiceDatabaseException("Invalid value for Dublin core catalog: null");
    }
    String seriesId = dc.getFirst(DublinCore.PROPERTY_IDENTIFIER);
    String seriesXML;
    try {
        seriesXML = serializeDublinCore(dc);
    } catch (Exception e1) {
        logger.error("Could not serialize Dublin Core: {}", e1);
        throw new SeriesServiceDatabaseException(e1);
    }
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    DublinCoreCatalog newSeries = null;
    try {
        tx.begin();
        SeriesEntity entity = getSeriesEntity(seriesId, em);
        if (entity == null) {
            // no series stored, create new entity
            entity = new SeriesEntity();
            entity.setOrganization(securityService.getOrganization().getId());
            entity.setSeriesId(seriesId);
            entity.setSeries(seriesXML);
            em.persist(entity);
            newSeries = dc;
        } else {
            // Ensure this user is allowed to update this series
            String accessControlXml = entity.getAccessControl();
            if (accessControlXml != null) {
                AccessControlList acl = AccessControlParser.parseAcl(accessControlXml);
                User currentUser = securityService.getUser();
                Organization currentOrg = securityService.getOrganization();
                if (!AccessControlUtil.isAuthorized(acl, currentUser, currentOrg, Permissions.Action.WRITE.toString())) {
                    throw new UnauthorizedException(currentUser + " is not authorized to update series " + seriesId);
                }
            }
            entity.setSeries(seriesXML);
            em.merge(entity);
        }
        tx.commit();
        return newSeries;
    } catch (Exception e) {
        logger.error("Could not update series: {}", e.getMessage());
        if (tx.isActive()) {
            tx.rollback();
        }
        throw new SeriesServiceDatabaseException(e);
    } finally {
        em.close();
    }
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) User(org.opencastproject.security.api.User) Organization(org.opencastproject.security.api.Organization) SeriesServiceDatabaseException(org.opencastproject.series.impl.SeriesServiceDatabaseException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) NoResultException(javax.persistence.NoResultException) SeriesServiceDatabaseException(org.opencastproject.series.impl.SeriesServiceDatabaseException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) AccessControlParsingException(org.opencastproject.security.api.AccessControlParsingException)

Example 20 with SeriesServiceDatabaseException

use of org.opencastproject.series.impl.SeriesServiceDatabaseException in project opencast by opencast.

the class SeriesServiceDatabaseImpl method storeSeriesAccessControl.

/*
   * (non-Javadoc)
   *
   * @see org.opencastproject.series.impl.SeriesServiceDatabase#storeSeriesAccessControl(java.lang.String,
   * org.opencastproject.security.api.AccessControlList)
   */
@Override
public boolean storeSeriesAccessControl(String seriesId, AccessControlList accessControl) throws NotFoundException, SeriesServiceDatabaseException {
    if (accessControl == null) {
        logger.error("Access control parameter is <null> for series '{}'", seriesId);
        throw new IllegalArgumentException("Argument for updating ACL for series " + seriesId + " is null");
    }
    String serializedAC;
    try {
        serializedAC = AccessControlParser.toXml(accessControl);
    } catch (Exception e) {
        logger.error("Could not serialize access control parameter: {}", e.getMessage());
        throw new SeriesServiceDatabaseException(e);
    }
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    boolean updated = false;
    try {
        tx.begin();
        SeriesEntity entity = getSeriesEntity(seriesId, em);
        if (entity == null) {
            throw new NotFoundException("Series with ID " + seriesId + " does not exist.");
        }
        if (entity.getAccessControl() != null) {
            // Ensure this user is allowed to update this series
            String accessControlXml = entity.getAccessControl();
            if (accessControlXml != null) {
                AccessControlList acl = AccessControlParser.parseAcl(accessControlXml);
                User currentUser = securityService.getUser();
                Organization currentOrg = securityService.getOrganization();
                if (!AccessControlUtil.isAuthorized(acl, currentUser, currentOrg, Permissions.Action.WRITE.toString())) {
                    throw new UnauthorizedException(currentUser + " is not authorized to update ACLs on series " + seriesId);
                }
            }
            updated = true;
        }
        entity.setAccessControl(serializedAC);
        em.merge(entity);
        tx.commit();
        return updated;
    } catch (NotFoundException e) {
        throw e;
    } catch (Exception e) {
        logger.error("Could not update series: {}", e.getMessage());
        if (tx.isActive()) {
            tx.rollback();
        }
        throw new SeriesServiceDatabaseException(e);
    } finally {
        em.close();
    }
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) EntityTransaction(javax.persistence.EntityTransaction) User(org.opencastproject.security.api.User) Organization(org.opencastproject.security.api.Organization) NotFoundException(org.opencastproject.util.NotFoundException) NoResultException(javax.persistence.NoResultException) SeriesServiceDatabaseException(org.opencastproject.series.impl.SeriesServiceDatabaseException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) AccessControlParsingException(org.opencastproject.security.api.AccessControlParsingException) EntityManager(javax.persistence.EntityManager) SeriesServiceDatabaseException(org.opencastproject.series.impl.SeriesServiceDatabaseException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException)

Aggregations

SeriesServiceDatabaseException (org.opencastproject.series.impl.SeriesServiceDatabaseException)24 IOException (java.io.IOException)23 NotFoundException (org.opencastproject.util.NotFoundException)23 EntityManager (javax.persistence.EntityManager)15 NoResultException (javax.persistence.NoResultException)15 AccessControlParsingException (org.opencastproject.security.api.AccessControlParsingException)15 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)15 EntityTransaction (javax.persistence.EntityTransaction)11 SolrServerException (org.apache.solr.client.solrj.SolrServerException)8 MalformedURLException (java.net.MalformedURLException)7 SeriesException (org.opencastproject.series.api.SeriesException)7 SolrDocument (org.apache.solr.common.SolrDocument)6 AccessControlList (org.opencastproject.security.api.AccessControlList)5 Organization (org.opencastproject.security.api.Organization)4 User (org.opencastproject.security.api.User)4 SolrQuery (org.apache.solr.client.solrj.SolrQuery)3 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)3 SolrInputDocument (org.apache.solr.common.SolrInputDocument)3 DublinCoreCatalog (org.opencastproject.metadata.dublincore.DublinCoreCatalog)3 Query (javax.persistence.Query)2