use of org.olat.restapi.support.vo.RepositoryEntryVO in project OpenOLAT by OpenOLAT.
the class RepositoryEntryResource method updateEntry.
@POST
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response updateEntry(@PathParam("repoEntryKey") String repoEntryKey, RepositoryEntryVO vo, @Context HttpServletRequest request) {
if (!RestSecurityHelper.isAuthor(request)) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
final RepositoryEntry re = lookupRepositoryEntry(repoEntryKey);
if (re == null) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
RepositoryEntryLifecycle lifecycle = null;
RepositoryEntryLifecycleVO lifecycleVo = vo.getLifecycle();
if (lifecycleVo != null) {
RepositoryEntryLifecycleDAO lifecycleDao = CoreSpringFactory.getImpl(RepositoryEntryLifecycleDAO.class);
if (lifecycleVo.getKey() != null) {
lifecycle = lifecycleDao.loadById(lifecycleVo.getKey());
if (lifecycle.isPrivateCycle()) {
// check date
String fromStr = lifecycleVo.getValidFrom();
String toStr = lifecycleVo.getValidTo();
String label = lifecycleVo.getLabel();
String softKey = lifecycleVo.getSoftkey();
Date from = ObjectFactory.parseDate(fromStr);
Date to = ObjectFactory.parseDate(toStr);
lifecycle.setLabel(label);
lifecycle.setSoftKey(softKey);
lifecycle.setValidFrom(from);
lifecycle.setValidTo(to);
}
} else {
String fromStr = lifecycleVo.getValidFrom();
String toStr = lifecycleVo.getValidTo();
String label = lifecycleVo.getLabel();
String softKey = lifecycleVo.getSoftkey();
Date from = ObjectFactory.parseDate(fromStr);
Date to = ObjectFactory.parseDate(toStr);
lifecycle = lifecycleDao.create(label, softKey, true, from, to);
}
}
RepositoryEntry reloaded = repositoryManager.setDescriptionAndName(re, vo.getDisplayname(), vo.getDescription(), vo.getLocation(), vo.getAuthors(), vo.getExternalId(), vo.getExternalRef(), vo.getManagedFlags(), lifecycle);
RepositoryEntryVO rvo = ObjectFactory.get(reloaded);
return Response.ok(rvo).build();
}
use of org.olat.restapi.support.vo.RepositoryEntryVO in project OpenOLAT by OpenOLAT.
the class RepositoryEntryResource method replaceResource.
/**
* Replace a resource in the repository and update its display name. The implementation is
* limited to CP.
* @response.representation.mediaType multipart/form-data
* @response.representation.doc Import the resource file
* @response.representation.200.qname {http://www.example.com}repositoryEntryVO
* @response.representation.200.mediaType application/xml, application/json
* @response.representation.200.doc Replace the resource and return the updated repository entry
* @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_REPOENTRYVO}
* @response.representation.401.doc The roles of the authenticated user are not sufficient
* @param repoEntryKey The key or soft key of the repository entry
* @param filename The name of the file
* @param file The file input stream
* @param displayname The display name
* @param request The HTTP request
* @return
*/
@POST
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes({ MediaType.MULTIPART_FORM_DATA })
public Response replaceResource(@PathParam("repoEntryKey") String repoEntryKey, @Context HttpServletRequest request) {
if (!RestSecurityHelper.isAuthor(request)) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
MultipartReader reader = null;
try {
final RepositoryEntry re = lookupRepositoryEntry(repoEntryKey);
if (re == null) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
reader = new MultipartReader(request);
File tmpFile = reader.getFile();
String displayname = reader.getValue("displayname");
String location = reader.getValue("location");
String authors = reader.getValue("authors");
String description = reader.getValue("description");
String externalId = reader.getValue("externalId");
String externalRef = reader.getValue("externalRef");
String managedFlags = reader.getValue("managedFlags");
Identity identity = RestSecurityHelper.getUserRequest(request).getIdentity();
RepositoryEntry replacedRe;
if (tmpFile == null) {
replacedRe = repositoryManager.setDescriptionAndName(re, displayname, description, location, authors, externalId, externalRef, managedFlags, re.getLifecycle());
} else {
long length = tmpFile.length();
if (length == 0) {
return Response.serverError().status(Status.NO_CONTENT).build();
}
replacedRe = replaceFileResource(identity, re, tmpFile);
if (replacedRe == null) {
return Response.serverError().status(Status.NOT_FOUND).build();
} else {
replacedRe = repositoryManager.setDescriptionAndName(replacedRe, displayname, description, location, authors, externalId, externalRef, managedFlags, replacedRe.getLifecycle());
}
}
RepositoryEntryVO vo = ObjectFactory.get(replacedRe);
return Response.ok(vo).build();
} catch (Exception e) {
log.error("Error while importing a file", e);
} finally {
MultipartReader.closeQuietly(reader);
}
return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
}
use of org.olat.restapi.support.vo.RepositoryEntryVO in project OpenOLAT by OpenOLAT.
the class ObjectFactory method get.
public static RepositoryEntryVO get(RepositoryEntry entry) {
RepositoryEntryVO vo = new RepositoryEntryVO();
vo.setKey(entry.getKey());
vo.setSoftkey(entry.getSoftkey());
vo.setResourcename(entry.getResourcename());
vo.setDisplayname(entry.getDisplayname());
vo.setDescription(entry.getDescription());
vo.setAuthors(entry.getAuthors());
vo.setLocation(entry.getLocation());
vo.setResourceableId(entry.getResourceableId());
vo.setResourceableTypeName(entry.getResourceableTypeName());
OLATResource resource = entry.getOlatResource();
if (resource != null) {
vo.setOlatResourceKey(resource.getKey());
vo.setOlatResourceId(resource.getResourceableId());
vo.setOlatResourceTypeName(resource.getResourceableTypeName());
}
vo.setExternalId(entry.getExternalId());
vo.setExternalRef(entry.getExternalRef());
vo.setManagedFlags(entry.getManagedFlagsString());
if (entry.getLifecycle() != null) {
vo.setLifecycle(new RepositoryEntryLifecycleVO(entry.getLifecycle()));
}
return vo;
}
use of org.olat.restapi.support.vo.RepositoryEntryVO in project OpenOLAT by OpenOLAT.
the class RepositoryEntriesTest method testImportWiki.
@Test
public void testImportWiki() throws IOException, URISyntaxException {
URL cpUrl = RepositoryEntriesTest.class.getResource("wiki-demo.zip");
assertNotNull(cpUrl);
File cp = new File(cpUrl.toURI());
RestConnection conn = new RestConnection();
assertTrue(conn.login("administrator", "openolat"));
URI request = UriBuilder.fromUri(getContextURI()).path("repo/entries").build();
HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true);
HttpEntity entity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE).addBinaryBody("file", cp, ContentType.APPLICATION_OCTET_STREAM, cp.getName()).addTextBody("filename", "wiki-demo.zip").addTextBody("resourcename", "Wiki demo").addTextBody("displayname", "Wiki demo").build();
method.setEntity(entity);
HttpResponse response = conn.execute(method);
assertTrue(response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 201);
RepositoryEntryVO vo = conn.parse(response, RepositoryEntryVO.class);
assertNotNull(vo);
Long key = vo.getKey();
RepositoryEntry re = RepositoryManager.getInstance().lookupRepositoryEntry(key);
assertNotNull(re);
assertNotNull(re.getOlatResource());
assertEquals("Wiki demo", re.getDisplayname());
log.info(re.getOlatResource().getResourceableTypeName());
conn.shutdown();
}
use of org.olat.restapi.support.vo.RepositoryEntryVO in project OpenOLAT by OpenOLAT.
the class RepositoryEntriesTest method testGetEntry_managed.
@Test
public void testGetEntry_managed() throws IOException, URISyntaxException {
RepositoryEntry re = createRepository("Test GET repo entry");
re.setManagedFlagsString("all");
re = dbInstance.getCurrentEntityManager().merge(re);
dbInstance.commitAndCloseSession();
RestConnection conn = new RestConnection();
assertTrue(conn.login("administrator", "openolat"));
URI request = UriBuilder.fromUri(getContextURI()).path("repo").path("entries").queryParam("managed", "true").build();
HttpGet method = conn.createGet(request, MediaType.APPLICATION_JSON, true);
HttpResponse response = conn.execute(method);
assertEquals(200, response.getStatusLine().getStatusCode());
List<RepositoryEntryVO> entryVoes = parseRepoArray(response.getEntity().getContent());
Assert.assertNotNull(entryVoes);
Assert.assertFalse(entryVoes.isEmpty());
// only repo entries with managed flags
for (RepositoryEntryVO entryVo : entryVoes) {
Assert.assertNotNull(entryVo.getManagedFlags());
Assert.assertTrue(entryVo.getManagedFlags().length() > 0);
}
conn.shutdown();
}
Aggregations