use of org.olat.modules.taxonomy.model.TaxonomyLevelRefImpl in project openolat by klemens.
the class TaxonomyWebService method putTaxonomyLevel.
/**
* Create or update a taxonomy level. The method changes to tree structure, a
* null parent key will make the level a root one, a new parent key will move
* the level.
*
* @response.representation.200.qname {http://www.example.com}taxonomyLevelVO
* @response.representation.200.mediaType application/xml, application/json
* @response.representation.200.doc A taxonomy level
* @response.representation.200.example {@link org.olat.modules.taxonomy.restapi.Examples#SAMPLE_TAXONOMYLEVELVO}
* @response.representation.401.doc The roles of the authenticated user are not sufficient
* @response.representation.404.doc An existant level was not found
* @param taxonomyKey The taxonomy tree where this level is
* @param httpRequest The HTTP request
* @param levelVo The roll call to update
* @return The updated roll call
*/
@PUT
@Path("levels")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response putTaxonomyLevel(TaxonomyLevelVO levelVo) {
TaxonomyLevel parentLevel = null;
if (levelVo.getParentKey() != null) {
parentLevel = taxonomyService.getTaxonomyLevel(new TaxonomyLevelRefImpl(levelVo.getParentKey()));
}
TaxonomyLevel level;
if (levelVo.getKey() != null) {
level = taxonomyService.getTaxonomyLevel(new TaxonomyLevelRefImpl(levelVo.getKey()));
if (levelVo.getIdentifier() != null) {
level.setIdentifier(levelVo.getIdentifier());
}
if (levelVo.getDisplayName() != null) {
level.setDisplayName(levelVo.getDisplayName());
}
if (levelVo.getDescription() != null) {
level.setDescription(levelVo.getDescription());
}
if (levelVo.getExternalId() != null) {
level.setExternalId(levelVo.getExternalId());
}
if (levelVo.getTypeKey() != null) {
TaxonomyLevelType type = taxonomyService.getTaxonomyLevelType(new TaxonomyLevelTypeRefImpl(levelVo.getTypeKey()));
level.setType(type);
}
if (levelVo.getManagedFlags() != null) {
level.setManagedFlags(TaxonomyLevelManagedFlag.toEnum(levelVo.getManagedFlags()));
}
level = taxonomyService.updateTaxonomyLevel(level);
} else {
level = taxonomyService.createTaxonomyLevel(levelVo.getIdentifier(), levelVo.getDisplayName(), levelVo.getDescription(), levelVo.getExternalId(), TaxonomyLevelManagedFlag.toEnum(levelVo.getManagedFlags()), parentLevel, taxonomy);
if (levelVo.getTypeKey() != null) {
TaxonomyLevelType type = taxonomyService.getTaxonomyLevelType(new TaxonomyLevelTypeRefImpl(levelVo.getTypeKey()));
level.setType(type);
level = taxonomyService.updateTaxonomyLevel(level);
}
}
if ((level.getParent() != null && levelVo.getParentKey() == null) || (level.getParent() == null && levelVo.getParentKey() != null) || (level.getParent() != null && !level.getParent().getKey().equals(levelVo.getParentKey()))) {
TaxonomyLevel newParentLevel = null;
if (levelVo.getParentKey() != null) {
newParentLevel = taxonomyService.getTaxonomyLevel(new TaxonomyLevelRefImpl(levelVo.getParentKey()));
}
level = taxonomyService.moveTaxonomyLevel(level, newParentLevel);
}
TaxonomyLevelVO newLevelVo = new TaxonomyLevelVO(level);
return Response.ok(newLevelVo).build();
}
use of org.olat.modules.taxonomy.model.TaxonomyLevelRefImpl in project openolat by klemens.
the class TaxonomyWebServiceTest method putTaxonomyLevel_subLevel.
@Test
public void putTaxonomyLevel_subLevel() throws IOException, URISyntaxException {
Taxonomy taxonomy = taxonomyService.createTaxonomy("REST-Tax-3", "Taxonomy on rest", "PUT is cool, yes!", "PUT-tax-2");
TaxonomyLevel rootLevel = taxonomyService.createTaxonomyLevel("REST-Tax-r-1", "Root level on rest", "Level", "Ext-23", null, null, taxonomy);
TaxonomyLevelType type = taxonomyService.createTaxonomyLevelType("Sub-type", "Type for a sub level", "All is in the title", "TYP-23", taxonomy);
dbInstance.commitAndCloseSession();
Assert.assertNotNull(taxonomy);
RestConnection conn = new RestConnection();
Assert.assertTrue(conn.login("administrator", "openolat"));
String uid = UUID.randomUUID().toString();
TaxonomyLevelVO levelVo = new TaxonomyLevelVO();
levelVo.setIdentifier(uid);
levelVo.setDisplayName("PUT a sub level");
levelVo.setDescription("Try to PUT a level above the root");
levelVo.setExternalId("EXT-191");
levelVo.setParentKey(rootLevel.getKey());
levelVo.setTypeKey(type.getKey());
URI request = UriBuilder.fromUri(getContextURI()).path("taxonomy").path(taxonomy.getKey().toString()).path("levels").build();
HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true);
conn.addJsonEntity(method, levelVo);
HttpResponse response = conn.execute(method);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
// check the returned value
TaxonomyLevelVO newTaxonomyLevelVo = conn.parse(response, TaxonomyLevelVO.class);
Assert.assertNotNull(newTaxonomyLevelVo);
Assert.assertNotNull(newTaxonomyLevelVo.getKey());
Assert.assertEquals(uid, newTaxonomyLevelVo.getIdentifier());
Assert.assertEquals("PUT a sub level", newTaxonomyLevelVo.getDisplayName());
Assert.assertEquals("EXT-191", newTaxonomyLevelVo.getExternalId());
Assert.assertEquals(rootLevel.getKey(), newTaxonomyLevelVo.getParentKey());
Assert.assertEquals(type.getKey(), newTaxonomyLevelVo.getTypeKey());
// check the database
TaxonomyLevel savedLevel = taxonomyService.getTaxonomyLevel(new TaxonomyLevelRefImpl(newTaxonomyLevelVo.getKey()));
Assert.assertNotNull(savedLevel);
Assert.assertEquals(newTaxonomyLevelVo.getKey(), savedLevel.getKey());
Assert.assertEquals(newTaxonomyLevelVo.getParentKey(), savedLevel.getParent().getKey());
// check parent line
List<TaxonomyLevel> parentLine = taxonomyService.getTaxonomyLevelParentLine(savedLevel, taxonomy);
Assert.assertNotNull(parentLine);
Assert.assertEquals(2, parentLine.size());
Assert.assertEquals(rootLevel, parentLine.get(0));
Assert.assertEquals(savedLevel, parentLine.get(1));
}
use of org.olat.modules.taxonomy.model.TaxonomyLevelRefImpl in project openolat by klemens.
the class TaxonomyWebServiceTest method deleteTaxonomyLevel_notPossible.
/**
* The REST method only delete something if possible. If the level
* has some children, competences... the call will not delete it.
*
* @throws IOException
* @throws URISyntaxException
*/
@Test
public void deleteTaxonomyLevel_notPossible() throws IOException, URISyntaxException {
Taxonomy taxonomy = taxonomyService.createTaxonomy("REST-Del-2", "Taxonomy on rest", "Delete is sad! But there is some hope.", "DELETE-tax-2");
TaxonomyLevel rootLevel = taxonomyService.createTaxonomyLevel("REST-Del-root", "Root level on rest", "Level", "Ext-57", null, null, taxonomy);
TaxonomyLevel levelToDelete = taxonomyService.createTaxonomyLevel("REST-Del-u-2", "Sub level on rest", "Level", "Ext-58", null, rootLevel, taxonomy);
dbInstance.commitAndCloseSession();
Assert.assertNotNull(taxonomy);
RestConnection conn = new RestConnection();
Assert.assertTrue(conn.login("administrator", "openolat"));
URI request = UriBuilder.fromUri(getContextURI()).path("taxonomy").path(taxonomy.getKey().toString()).path("levels").path(rootLevel.getKey().toString()).build();
HttpDelete method = conn.createDelete(request, MediaType.APPLICATION_JSON);
HttpResponse response = conn.execute(method);
Assert.assertEquals(304, response.getStatusLine().getStatusCode());
EntityUtils.consume(response.getEntity());
// check the updated value
TaxonomyLevel survivingLevel = taxonomyService.getTaxonomyLevel(new TaxonomyLevelRefImpl(levelToDelete.getKey()));
Assert.assertNotNull(survivingLevel);
TaxonomyLevel survivingRootLevel = taxonomyService.getTaxonomyLevel(new TaxonomyLevelRefImpl(rootLevel.getKey()));
Assert.assertNotNull(survivingRootLevel);
}
use of org.olat.modules.taxonomy.model.TaxonomyLevelRefImpl in project openolat by klemens.
the class TaxonomyWebServiceTest method updateTaxonomyLevel.
/**
* Update level.
*
* @throws IOException
* @throws URISyntaxException
*/
@Test
public void updateTaxonomyLevel() throws IOException, URISyntaxException {
Taxonomy taxonomy = taxonomyService.createTaxonomy("REST-Tax-4", "Taxonomy on rest", "PUT is cool, yes!", "PUT-tax-2");
TaxonomyLevel rootLevel = taxonomyService.createTaxonomyLevel("REST-Tax-u-1", "Root level on rest", "Level", "Ext-25", null, null, taxonomy);
TaxonomyLevel levelToUpdate = taxonomyService.createTaxonomyLevel("REST-Tax-u-1", "Sub level on rest", "Level", "Ext-26", null, rootLevel, taxonomy);
TaxonomyLevelType type = taxonomyService.createTaxonomyLevelType("Sub-type", "Type for a sub level", "All is in the title", "TYP-27", taxonomy);
dbInstance.commitAndCloseSession();
Assert.assertNotNull(taxonomy);
RestConnection conn = new RestConnection();
Assert.assertTrue(conn.login("administrator", "openolat"));
TaxonomyLevelVO levelVo = new TaxonomyLevelVO();
levelVo.setKey(levelToUpdate.getKey());
levelVo.setIdentifier("Updated id");
levelVo.setDisplayName("Updated name");
levelVo.setDescription("Updated description");
levelVo.setExternalId("Updated ext.");
levelVo.setTypeKey(type.getKey());
levelVo.setParentKey(rootLevel.getKey());
URI request = UriBuilder.fromUri(getContextURI()).path("taxonomy").path(taxonomy.getKey().toString()).path("levels").build();
HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true);
conn.addJsonEntity(method, levelVo);
HttpResponse response = conn.execute(method);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
// check the updated value
TaxonomyLevelVO updatedTaxonomyLevelVo = conn.parse(response, TaxonomyLevelVO.class);
Assert.assertNotNull(updatedTaxonomyLevelVo);
Assert.assertEquals("Updated id", updatedTaxonomyLevelVo.getIdentifier());
Assert.assertEquals("Updated name", updatedTaxonomyLevelVo.getDisplayName());
Assert.assertEquals("Updated description", updatedTaxonomyLevelVo.getDescription());
Assert.assertEquals("Updated ext.", updatedTaxonomyLevelVo.getExternalId());
Assert.assertEquals(rootLevel.getKey(), updatedTaxonomyLevelVo.getParentKey());
Assert.assertEquals(type.getKey(), updatedTaxonomyLevelVo.getTypeKey());
// check the database
TaxonomyLevel savedLevel = taxonomyService.getTaxonomyLevel(new TaxonomyLevelRefImpl(updatedTaxonomyLevelVo.getKey()));
Assert.assertNotNull(savedLevel);
Assert.assertEquals("Updated id", savedLevel.getIdentifier());
Assert.assertEquals("Updated name", savedLevel.getDisplayName());
Assert.assertEquals("Updated description", savedLevel.getDescription());
Assert.assertEquals("Updated ext.", savedLevel.getExternalId());
Assert.assertEquals(rootLevel.getKey(), savedLevel.getParent().getKey());
Assert.assertEquals(type.getKey(), savedLevel.getType().getKey());
}
Aggregations