Search in sources :

Example 1 with RepositoryEntryVOes

use of org.olat.restapi.support.vo.RepositoryEntryVOes in project OpenOLAT by OpenOLAT.

the class RepositoryEntriesResource method getEntries.

/**
 * List all entries in the OLAT repository
 * @response.representation.200.qname {http://www.example.com}repositoryEntryVO
 * @response.representation.200.mediaType text/plain, text/html, application/xml, application/json
 * @response.representation.200.doc List all entries in the repository
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_REPOENTRYVOes}
 * @param start (optional)
 * @param limit (optional)
 * @param managed (optional)
 * @param externalId External ID (optional)
 * @param externalRef External reference number (optional)
 * @param resourceType The resource type (CourseModule) (optional)
 * @param httpRequest The HTTP request
 * @param request The RESt request
 * @return
 */
@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getEntries(@QueryParam("start") @DefaultValue("0") Integer start, @QueryParam("limit") @DefaultValue("25") Integer limit, @QueryParam("managed") Boolean managed, @QueryParam("externalId") String externalId, @QueryParam("externalRef") String externalRef, @QueryParam("resourceType") String resourceType, @Context HttpServletRequest httpRequest, @Context Request request) {
    try {
        // list of courses open for everybody
        Roles roles = getRoles(httpRequest);
        Identity identity = getIdentity(httpRequest);
        RepositoryManager rm = RepositoryManager.getInstance();
        SearchRepositoryEntryParameters params = new SearchRepositoryEntryParameters(identity, roles);
        params.setManaged(managed);
        if (StringHelper.containsNonWhitespace(externalId)) {
            params.setExternalId(externalId);
        }
        if (StringHelper.containsNonWhitespace(externalRef)) {
            params.setExternalRef(externalRef);
        }
        if (StringHelper.containsNonWhitespace(resourceType)) {
            params.setResourceTypes(Collections.singletonList(resourceType));
        }
        if (MediaTypeVariants.isPaged(httpRequest, request)) {
            int totalCount = rm.countGenericANDQueryWithRolesRestriction(params);
            List<RepositoryEntry> res = rm.genericANDQueryWithRolesRestriction(params, start, limit, true);
            RepositoryEntryVOes voes = new RepositoryEntryVOes();
            voes.setRepositoryEntries(toArrayOfVOes(res));
            voes.setTotalCount(totalCount);
            return Response.ok(voes).build();
        } else {
            List<RepositoryEntry> res = rm.genericANDQueryWithRolesRestriction(params, 0, -1, false);
            RepositoryEntryVO[] voes = toArrayOfVOes(res);
            return Response.ok(voes).build();
        }
    } catch (Exception e) {
        throw new WebApplicationException(e);
    }
}
Also used : SearchRepositoryEntryParameters(org.olat.repository.model.SearchRepositoryEntryParameters) RepositoryEntryVOes(org.olat.restapi.support.vo.RepositoryEntryVOes) RepositoryEntryVO(org.olat.restapi.support.vo.RepositoryEntryVO) WebApplicationException(javax.ws.rs.WebApplicationException) RestSecurityHelper.getRoles(org.olat.restapi.security.RestSecurityHelper.getRoles) Roles(org.olat.core.id.Roles) RepositoryManager(org.olat.repository.RepositoryManager) RepositoryEntry(org.olat.repository.RepositoryEntry) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) Identity(org.olat.core.id.Identity) WebApplicationException(javax.ws.rs.WebApplicationException) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with RepositoryEntryVOes

use of org.olat.restapi.support.vo.RepositoryEntryVOes in project OpenOLAT by OpenOLAT.

the class RepositoryEntriesTest method testGetEntriesWithPaging.

@Test
public void testGetEntriesWithPaging() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login("administrator", "openolat"));
    URI uri = UriBuilder.fromUri(getContextURI()).path("repo").path("entries").queryParam("start", "0").queryParam("limit", "25").build();
    HttpGet method = conn.createGet(uri, MediaType.APPLICATION_JSON + ";pagingspec=1.0", true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    RepositoryEntryVOes entryVoes = conn.parse(response, RepositoryEntryVOes.class);
    assertNotNull(entryVoes);
    assertNotNull(entryVoes.getRepositoryEntries());
    assertTrue(entryVoes.getRepositoryEntries().length <= 25);
    assertTrue(entryVoes.getTotalCount() >= entryVoes.getRepositoryEntries().length);
    conn.shutdown();
}
Also used : RepositoryEntryVOes(org.olat.restapi.support.vo.RepositoryEntryVOes) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) Test(org.junit.Test)

Example 3 with RepositoryEntryVOes

use of org.olat.restapi.support.vo.RepositoryEntryVOes in project openolat by klemens.

the class RepositoryEntriesTest method testGetEntriesWithPaging.

@Test
public void testGetEntriesWithPaging() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login("administrator", "openolat"));
    URI uri = UriBuilder.fromUri(getContextURI()).path("repo").path("entries").queryParam("start", "0").queryParam("limit", "25").build();
    HttpGet method = conn.createGet(uri, MediaType.APPLICATION_JSON + ";pagingspec=1.0", true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    RepositoryEntryVOes entryVoes = conn.parse(response, RepositoryEntryVOes.class);
    assertNotNull(entryVoes);
    assertNotNull(entryVoes.getRepositoryEntries());
    assertTrue(entryVoes.getRepositoryEntries().length <= 25);
    assertTrue(entryVoes.getTotalCount() >= entryVoes.getRepositoryEntries().length);
    conn.shutdown();
}
Also used : RepositoryEntryVOes(org.olat.restapi.support.vo.RepositoryEntryVOes) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) Test(org.junit.Test)

Example 4 with RepositoryEntryVOes

use of org.olat.restapi.support.vo.RepositoryEntryVOes in project openolat by klemens.

the class RepositoryEntriesResource method getEntries.

/**
 * List all entries in the OLAT repository
 * @response.representation.200.qname {http://www.example.com}repositoryEntryVO
 * @response.representation.200.mediaType text/plain, text/html, application/xml, application/json
 * @response.representation.200.doc List all entries in the repository
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_REPOENTRYVOes}
 * @param start (optional)
 * @param limit (optional)
 * @param managed (optional)
 * @param externalId External ID (optional)
 * @param externalRef External reference number (optional)
 * @param resourceType The resource type (CourseModule) (optional)
 * @param httpRequest The HTTP request
 * @param request The RESt request
 * @return
 */
@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getEntries(@QueryParam("start") @DefaultValue("0") Integer start, @QueryParam("limit") @DefaultValue("25") Integer limit, @QueryParam("managed") Boolean managed, @QueryParam("externalId") String externalId, @QueryParam("externalRef") String externalRef, @QueryParam("resourceType") String resourceType, @Context HttpServletRequest httpRequest, @Context Request request) {
    try {
        // list of courses open for everybody
        Roles roles = getRoles(httpRequest);
        Identity identity = getIdentity(httpRequest);
        RepositoryManager rm = RepositoryManager.getInstance();
        SearchRepositoryEntryParameters params = new SearchRepositoryEntryParameters(identity, roles);
        params.setManaged(managed);
        if (StringHelper.containsNonWhitespace(externalId)) {
            params.setExternalId(externalId);
        }
        if (StringHelper.containsNonWhitespace(externalRef)) {
            params.setExternalRef(externalRef);
        }
        if (StringHelper.containsNonWhitespace(resourceType)) {
            params.setResourceTypes(Collections.singletonList(resourceType));
        }
        if (MediaTypeVariants.isPaged(httpRequest, request)) {
            int totalCount = rm.countGenericANDQueryWithRolesRestriction(params);
            List<RepositoryEntry> res = rm.genericANDQueryWithRolesRestriction(params, start, limit, true);
            RepositoryEntryVOes voes = new RepositoryEntryVOes();
            voes.setRepositoryEntries(toArrayOfVOes(res));
            voes.setTotalCount(totalCount);
            return Response.ok(voes).build();
        } else {
            List<RepositoryEntry> res = rm.genericANDQueryWithRolesRestriction(params, 0, -1, false);
            RepositoryEntryVO[] voes = toArrayOfVOes(res);
            return Response.ok(voes).build();
        }
    } catch (Exception e) {
        throw new WebApplicationException(e);
    }
}
Also used : SearchRepositoryEntryParameters(org.olat.repository.model.SearchRepositoryEntryParameters) RepositoryEntryVOes(org.olat.restapi.support.vo.RepositoryEntryVOes) RepositoryEntryVO(org.olat.restapi.support.vo.RepositoryEntryVO) WebApplicationException(javax.ws.rs.WebApplicationException) RestSecurityHelper.getRoles(org.olat.restapi.security.RestSecurityHelper.getRoles) Roles(org.olat.core.id.Roles) RepositoryManager(org.olat.repository.RepositoryManager) RepositoryEntry(org.olat.repository.RepositoryEntry) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) Identity(org.olat.core.id.Identity) WebApplicationException(javax.ws.rs.WebApplicationException) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

RepositoryEntryVOes (org.olat.restapi.support.vo.RepositoryEntryVOes)4 URI (java.net.URI)2 GET (javax.ws.rs.GET)2 Produces (javax.ws.rs.Produces)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 HttpResponse (org.apache.http.HttpResponse)2 HttpGet (org.apache.http.client.methods.HttpGet)2 Test (org.junit.Test)2 Identity (org.olat.core.id.Identity)2 Roles (org.olat.core.id.Roles)2 RepositoryEntry (org.olat.repository.RepositoryEntry)2 RepositoryManager (org.olat.repository.RepositoryManager)2 SearchRepositoryEntryParameters (org.olat.repository.model.SearchRepositoryEntryParameters)2 RestSecurityHelper.getIdentity (org.olat.restapi.security.RestSecurityHelper.getIdentity)2 RestSecurityHelper.getRoles (org.olat.restapi.security.RestSecurityHelper.getRoles)2 RepositoryEntryVO (org.olat.restapi.support.vo.RepositoryEntryVO)2