Search in sources :

Example 16 with TaxonomyLevelTypeToType

use of org.olat.modules.taxonomy.TaxonomyLevelTypeToType in project openolat by klemens.

the class TaxonomyWebService method getAllowedSubTaxonomyLevelTypes.

/**
 * Get the allowed sub-types of a specified taxonomy level's type.
 *
 * @response.representation.200.qname {http://www.example.com}taxonomyLevelTypeVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc An array of taxonomy level types
 * @response.representation.200.example {@link org.olat.modules.taxonomy.restapi.Examples#SAMPLE_TAXONOMYLEVELTYPEVO}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @response.representation.404.doc The taxonomy level type was not found
 * @param taxonomyKey The taxonomy tree
 * @param httpRequest  The HTTP request
 * @param typeKey The primary key of the taxonomy level type
 * @return An array of taxonomy level types
 */
@GET
@Path("types/{typeKey}/allowedSubTypes")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getAllowedSubTaxonomyLevelTypes(@PathParam("typeKey") Long typeKey) {
    TaxonomyLevelType type = taxonomyService.getTaxonomyLevelType(new TaxonomyLevelTypeRefImpl(typeKey));
    if (type == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    Set<TaxonomyLevelTypeToType> typeToTypes = type.getAllowedTaxonomyLevelSubTypes();
    List<TaxonomyLevelTypeVO> subTypeVOes = new ArrayList<>(typeToTypes.size());
    for (TaxonomyLevelTypeToType typeToType : typeToTypes) {
        TaxonomyLevelType subType = typeToType.getAllowedSubTaxonomyLevelType();
        subTypeVOes.add(new TaxonomyLevelTypeVO(subType));
    }
    return Response.ok(subTypeVOes.toArray(new TaxonomyLevelTypeVO[subTypeVOes.size()])).build();
}
Also used : TaxonomyLevelType(org.olat.modules.taxonomy.TaxonomyLevelType) ArrayList(java.util.ArrayList) TaxonomyLevelTypeRefImpl(org.olat.modules.taxonomy.model.TaxonomyLevelTypeRefImpl) TaxonomyLevelTypeToType(org.olat.modules.taxonomy.TaxonomyLevelTypeToType) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 17 with TaxonomyLevelTypeToType

use of org.olat.modules.taxonomy.TaxonomyLevelTypeToType in project openolat by klemens.

the class TaxonomyWebServiceTest method allowTaxonomyLevelTypeAllowedSubType.

@Test
public void allowTaxonomyLevelTypeAllowedSubType() throws IOException, URISyntaxException {
    Taxonomy taxonomy = taxonomyService.createTaxonomy("REST-Tax-4", "Taxonomy on rest", "Rest is cool", "Ext-tax-1");
    TaxonomyLevelType type = taxonomyService.createTaxonomyLevelType("REST-Type-4", "Type 4 on rest", "Type", "EXT-Type-4", taxonomy);
    TaxonomyLevelType subType1 = taxonomyService.createTaxonomyLevelType("REST-Type-4-1", "Type 4.1 on rest", "Type", "EXT-Type-4-1", taxonomy);
    TaxonomyLevelType subType2 = taxonomyService.createTaxonomyLevelType("REST-Type-4-2", "Type 4.2 on rest", "Type", "EXT-Type-4-2", taxonomy);
    dbInstance.commit();
    type = taxonomyService.updateTaxonomyLevelType(type, Collections.singletonList(subType1));
    dbInstance.commitAndCloseSession();
    RestConnection conn = new RestConnection();
    Assert.assertTrue(conn.login("administrator", "openolat"));
    URI request = UriBuilder.fromUri(getContextURI()).path("taxonomy").path(taxonomy.getKey().toString()).path("types").path(type.getKey().toString()).path("allowedSubTypes").path(subType2.getKey().toString()).build();
    HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    EntityUtils.consume(response.getEntity());
    TaxonomyLevelType reloadedType = taxonomyService.getTaxonomyLevelType(type);
    Set<TaxonomyLevelTypeToType> typeToTypes = reloadedType.getAllowedTaxonomyLevelSubTypes();
    Assert.assertEquals(2, typeToTypes.size());
    boolean found1 = false;
    boolean found2 = false;
    for (TaxonomyLevelTypeToType typeToType : typeToTypes) {
        TaxonomyLevelType subType = typeToType.getAllowedSubTaxonomyLevelType();
        if (subType1.getKey().equals(subType.getKey())) {
            found1 = true;
        } else if (subType2.getKey().equals(subType.getKey())) {
            found2 = true;
        }
    }
    Assert.assertTrue(found1);
    Assert.assertTrue(found2);
}
Also used : Taxonomy(org.olat.modules.taxonomy.Taxonomy) TaxonomyLevelType(org.olat.modules.taxonomy.TaxonomyLevelType) HttpResponse(org.apache.http.HttpResponse) TaxonomyLevelTypeToType(org.olat.modules.taxonomy.TaxonomyLevelTypeToType) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 18 with TaxonomyLevelTypeToType

use of org.olat.modules.taxonomy.TaxonomyLevelTypeToType in project openolat by klemens.

the class TaxonomyLevelTypeDAOTest method allowSubTypes.

@Test
public void allowSubTypes() {
    Taxonomy taxonomy = taxonomyDao.createTaxonomy("Tax-type", "Typed taxonomy", "A typed taxonomy", "");
    TaxonomyLevelType type = taxonomyLevelTypeDao.createTaxonomyLevelType("Type-parent", "A type", null, null, taxonomy);
    TaxonomyLevelType subType1 = taxonomyLevelTypeDao.createTaxonomyLevelType("Type-sub-1", "A type", null, null, taxonomy);
    TaxonomyLevelType subType2 = taxonomyLevelTypeDao.createTaxonomyLevelType("Type-sub-2", "A type", null, null, taxonomy);
    dbInstance.commitAndCloseSession();
    taxonomyLevelTypeToTypeDao.addAllowedSubType(type, subType1);
    taxonomyLevelTypeToTypeDao.addAllowedSubType(type, subType2);
    dbInstance.commitAndCloseSession();
    TaxonomyLevelTypeImpl reloadedType = (TaxonomyLevelTypeImpl) taxonomyLevelTypeDao.loadTaxonomyLevelTypeByKey(type.getKey());
    Assert.assertNotNull(reloadedType.getAllowedTaxonomyLevelSubTypes());
    Assert.assertEquals(2, reloadedType.getAllowedTaxonomyLevelSubTypes().size());
    Set<TaxonomyLevelTypeToType> allowedTypeSet = reloadedType.getAllowedTaxonomyLevelSubTypes();
    List<TaxonomyLevelType> allowedSubTypes = allowedTypeSet.stream().map(t -> t.getAllowedSubTaxonomyLevelType()).collect(Collectors.toList());
    Assert.assertTrue(allowedSubTypes.contains(subType1));
    Assert.assertTrue(allowedSubTypes.contains(subType2));
}
Also used : TaxonomyLevelTypeToType(org.olat.modules.taxonomy.TaxonomyLevelTypeToType) List(java.util.List) DB(org.olat.core.commons.persistence.DB) TaxonomyLevelTypeImpl(org.olat.modules.taxonomy.model.TaxonomyLevelTypeImpl) Set(java.util.Set) OlatTestCase(org.olat.test.OlatTestCase) Autowired(org.springframework.beans.factory.annotation.Autowired) Test(org.junit.Test) Assert(org.junit.Assert) TaxonomyLevelType(org.olat.modules.taxonomy.TaxonomyLevelType) Collectors(java.util.stream.Collectors) Taxonomy(org.olat.modules.taxonomy.Taxonomy) Taxonomy(org.olat.modules.taxonomy.Taxonomy) TaxonomyLevelType(org.olat.modules.taxonomy.TaxonomyLevelType) TaxonomyLevelTypeToType(org.olat.modules.taxonomy.TaxonomyLevelTypeToType) TaxonomyLevelTypeImpl(org.olat.modules.taxonomy.model.TaxonomyLevelTypeImpl) Test(org.junit.Test)

Aggregations

TaxonomyLevelTypeToType (org.olat.modules.taxonomy.TaxonomyLevelTypeToType)18 TaxonomyLevelType (org.olat.modules.taxonomy.TaxonomyLevelType)16 ArrayList (java.util.ArrayList)10 Taxonomy (org.olat.modules.taxonomy.Taxonomy)8 Test (org.junit.Test)6 TaxonomyLevel (org.olat.modules.taxonomy.TaxonomyLevel)6 URI (java.net.URI)4 HashSet (java.util.HashSet)4 List (java.util.List)4 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4 HttpResponse (org.apache.http.HttpResponse)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 HashMap (java.util.HashMap)2 Map (java.util.Map)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 HttpDelete (org.apache.http.client.methods.HttpDelete)2 HttpPut (org.apache.http.client.methods.HttpPut)2