Search in sources :

Example 6 with Theme

use of org.opencastproject.themes.Theme in project opencast by opencast.

the class ThemesEndpoint method getThemes.

@GET
@Produces({ MediaType.APPLICATION_JSON })
@Path("themes.json")
@RestQuery(name = "getThemes", description = "Return all of the known themes on the system", restParameters = { @RestParameter(name = "filter", isRequired = false, description = "The filter used for the query. They should be formated like that: 'filter1:value1,filter2:value2'", type = STRING), @RestParameter(defaultValue = "0", description = "The maximum number of items to return per page.", isRequired = false, name = "limit", type = RestParameter.Type.INTEGER), @RestParameter(defaultValue = "0", description = "The page number.", isRequired = false, name = "offset", type = RestParameter.Type.INTEGER), @RestParameter(name = "sort", isRequired = false, description = "The sort order. May include any of the following: NAME, CREATOR.  Add '_DESC' to reverse the sort order (e.g. CREATOR_DESC).", type = STRING) }, reponses = { @RestResponse(description = "A JSON representation of the themes", responseCode = HttpServletResponse.SC_OK) }, returnDescription = "")
public Response getThemes(@QueryParam("filter") String filter, @QueryParam("limit") int limit, @QueryParam("offset") int offset, @QueryParam("sort") String sort) {
    Option<Integer> optLimit = Option.option(limit);
    Option<Integer> optOffset = Option.option(offset);
    Option<String> optSort = Option.option(trimToNull(sort));
    ThemeSearchQuery query = new ThemeSearchQuery(securityService.getOrganization().getId(), securityService.getUser());
    // If the limit is set to 0, this is not taken into account
    if (optLimit.isSome() && limit == 0) {
        optLimit = Option.none();
    }
    if (optLimit.isSome())
        query.withLimit(optLimit.get());
    if (optOffset.isSome())
        query.withOffset(offset);
    Map<String, String> filters = RestUtils.parseFilter(filter);
    for (String name : filters.keySet()) {
        if (ThemesListQuery.FILTER_CREATOR_NAME.equals(name))
            query.withCreator(filters.get(name));
        if (ThemesListQuery.FILTER_TEXT_NAME.equals(name))
            query.withText(QueryPreprocessor.sanitize(filters.get(name)));
    }
    if (optSort.isSome()) {
        Set<SortCriterion> sortCriteria = RestUtils.parseSortQueryParameter(optSort.get());
        for (SortCriterion criterion : sortCriteria) {
            switch(criterion.getFieldName()) {
                case ThemeIndexSchema.NAME:
                    query.sortByName(criterion.getOrder());
                    break;
                case ThemeIndexSchema.DESCRIPTION:
                    query.sortByDescription(criterion.getOrder());
                    break;
                case ThemeIndexSchema.CREATOR:
                    query.sortByCreator(criterion.getOrder());
                    break;
                case ThemeIndexSchema.DEFAULT:
                    query.sortByDefault(criterion.getOrder());
                    break;
                case ThemeIndexSchema.CREATION_DATE:
                    query.sortByCreatedDateTime(criterion.getOrder());
                    break;
                default:
                    logger.info("Unknown sort criteria {}", criterion.getFieldName());
                    return Response.status(SC_BAD_REQUEST).build();
            }
        }
    }
    logger.trace("Using Query: " + query.toString());
    SearchResult<org.opencastproject.index.service.impl.index.theme.Theme> results = null;
    try {
        results = searchIndex.getByQuery(query);
    } catch (SearchIndexException e) {
        logger.error("The admin UI Search Index was not able to get the themes list:", e);
        return RestUtil.R.serverError();
    }
    List<JValue> themesJSON = new ArrayList<JValue>();
    // If the results list if empty, we return already a response.
    if (results.getPageSize() == 0) {
        logger.debug("No themes match the given filters.");
        return okJsonList(themesJSON, nul(offset).getOr(0), nul(limit).getOr(0), 0);
    }
    for (SearchResultItem<org.opencastproject.index.service.impl.index.theme.Theme> item : results.getItems()) {
        org.opencastproject.index.service.impl.index.theme.Theme theme = item.getSource();
        themesJSON.add(themeToJSON(theme, false));
    }
    return okJsonList(themesJSON, nul(offset).getOr(0), nul(limit).getOr(0), results.getHitCount());
}
Also used : SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) ArrayList(java.util.ArrayList) ThemeSearchQuery(org.opencastproject.index.service.impl.index.theme.ThemeSearchQuery) SortCriterion(org.opencastproject.matterhorn.search.SortCriterion) JValue(com.entwinemedia.fn.data.json.JValue) Theme(org.opencastproject.themes.Theme) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 7 with Theme

use of org.opencastproject.themes.Theme in project opencast by opencast.

the class TestThemesEndpoint method addData.

private void addData() throws MailServiceException, ThemesServiceDatabaseException {
    Theme theme = new Theme(Option.some(theme1Id), creationDate, true, user, "The Theme name", "The Theme description", true, "bumper-file", true, "trailer-file", true, "title,room,date", "title-background-file", true, "license-background-file", "The license description", true, "watermark-file", "top-left");
    themesServiceDatabaseImpl.updateTheme(theme);
}
Also used : Theme(org.opencastproject.themes.Theme)

Example 8 with Theme

use of org.opencastproject.themes.Theme in project opencast by opencast.

the class ThemesServiceDatabaseImpl method repopulate.

@Override
public void repopulate(final String indexName) {
    final String destinationId = ThemeItem.THEME_QUEUE_PREFIX + WordUtils.capitalize(indexName);
    for (final Organization organization : organizationDirectoryService.getOrganizations()) {
        SecurityUtil.runAs(securityService, organization, SecurityUtil.createSystemUser(cc, organization), new Effect0() {

            @Override
            protected void run() {
                try {
                    final List<Theme> themes = getThemes();
                    int total = themes.size();
                    int current = 1;
                    logger.info("Re-populating '{}' index with themes from organization {}. There are {} theme(s) to add to the index.", indexName, securityService.getOrganization().getId(), total);
                    for (Theme theme : themes) {
                        messageSender.sendObjectMessage(destinationId, MessageSender.DestinationType.Queue, ThemeItem.update(toSerializableTheme(theme)));
                        messageSender.sendObjectMessage(IndexProducer.RESPONSE_QUEUE, MessageSender.DestinationType.Queue, IndexRecreateObject.update(indexName, IndexRecreateObject.Service.Themes, total, current));
                        current++;
                    }
                } catch (ThemesServiceDatabaseException e) {
                    logger.error("Unable to get themes from the database because: {}", ExceptionUtils.getStackTrace(e));
                    throw new IllegalStateException(e);
                }
            }
        });
    }
    Organization organization = new DefaultOrganization();
    SecurityUtil.runAs(securityService, organization, SecurityUtil.createSystemUser(cc, organization), new Effect0() {

        @Override
        protected void run() {
            messageSender.sendObjectMessage(IndexProducer.RESPONSE_QUEUE, MessageSender.DestinationType.Queue, IndexRecreateObject.end(indexName, IndexRecreateObject.Service.Themes));
        }
    });
}
Also used : Organization(org.opencastproject.security.api.Organization) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) Effect0(org.opencastproject.util.data.Effect0) Theme(org.opencastproject.themes.Theme) SerializableTheme(org.opencastproject.message.broker.api.theme.SerializableTheme) ArrayList(java.util.ArrayList) List(java.util.List) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization)

Example 9 with Theme

use of org.opencastproject.themes.Theme in project opencast by opencast.

the class ThemesServiceDatabaseImpl method getThemes.

private List<Theme> getThemes() throws ThemesServiceDatabaseException {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        String orgId = securityService.getOrganization().getId();
        TypedQuery<ThemeDto> q = em.createNamedQuery("Themes.findByOrg", ThemeDto.class).setParameter("org", orgId);
        List<ThemeDto> themeDtos = q.getResultList();
        List<Theme> themes = new ArrayList<>();
        for (ThemeDto themeDto : themeDtos) {
            themes.add(themeDto.toTheme(userDirectoryService));
        }
        return themes;
    } catch (Exception e) {
        logger.error("Could not get themes: {}", ExceptionUtils.getStackTrace(e));
        throw new ThemesServiceDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) ArrayList(java.util.ArrayList) Theme(org.opencastproject.themes.Theme) SerializableTheme(org.opencastproject.message.broker.api.theme.SerializableTheme) NoResultException(javax.persistence.NoResultException) NotFoundException(org.opencastproject.util.NotFoundException)

Aggregations

Theme (org.opencastproject.themes.Theme)9 ArrayList (java.util.ArrayList)5 Path (javax.ws.rs.Path)5 NotFoundException (org.opencastproject.util.NotFoundException)5 RestQuery (org.opencastproject.util.doc.rest.RestQuery)5 IOException (java.io.IOException)4 ThemesServiceDatabaseException (org.opencastproject.themes.persistence.ThemesServiceDatabaseException)4 JValue (com.entwinemedia.fn.data.json.JValue)2 GET (javax.ws.rs.GET)2 Produces (javax.ws.rs.Produces)2 SearchIndexException (org.opencastproject.matterhorn.search.SearchIndexException)2 SerializableTheme (org.opencastproject.message.broker.api.theme.SerializableTheme)2 InputStream (java.io.InputStream)1 Date (java.util.Date)1 List (java.util.List)1 EntityManager (javax.persistence.EntityManager)1 NoResultException (javax.persistence.NoResultException)1 DELETE (javax.ws.rs.DELETE)1 POST (javax.ws.rs.POST)1 PUT (javax.ws.rs.PUT)1