Search in sources :

Example 6 with CourseDBEntry

use of org.olat.course.db.CourseDBEntry in project openolat by klemens.

the class GetUserCourseDBFunction method call.

/**
 * @see org.olat.course.condition.interpreter.AbstractFunction#call(java.lang.Object[])
 */
@Override
public Object call(Object[] inStack) {
    /*
		 * argument check
		 */
    if (inStack.length > 2) {
        return handleException(new ArgumentParseException(ArgumentParseException.NEEDS_FEWER_ARGUMENTS, name, "", "error.fewerargs", "solution.providetwo.attrvalue"));
    } else if (inStack.length < 1) {
        return handleException(new ArgumentParseException(ArgumentParseException.NEEDS_MORE_ARGUMENTS, name, "", "error.moreargs", "solution.providetwo.attrvalue"));
    }
    /*
		 * argument type check
		 */
    if (!(inStack[0] instanceof String)) {
        return handleException(new ArgumentParseException(ArgumentParseException.WRONG_ARGUMENT_FORMAT, name, "", "error.argtype.attributename", "solution.example.name.infunction"));
    }
    CourseEditorEnv cev = getUserCourseEnv().getCourseEditorEnv();
    if (cev != null) {
        // return emtyp string to continue with condition evaluation test
        return defaultValue();
    }
    CourseDBManager courseDbManager = CoreSpringFactory.getImpl(CourseDBManager.class);
    Identity ident = getUserCourseEnv().getIdentityEnvironment().getIdentity();
    String category = null;
    String key = null;
    if (inStack.length == 1) {
        category = null;
        key = (String) inStack[1];
    } else if (inStack.length == 2) {
        category = (String) inStack[0];
        key = (String) inStack[1];
    }
    Long courseId = getUserCourseEnv().getCourseEnvironment().getCourseResourceableId();
    Object value;
    try {
        CourseDBEntry entry = courseDbManager.getValue(courseId, ident, category, key);
        if (entry == null) {
            return defaultValue();
        }
        value = entry.getValue();
        if (value == null) {
            return defaultValue();
        }
    } catch (Exception e) {
        log.error("", e);
        return defaultValue();
    }
    return value.toString();
}
Also used : CourseDBManager(org.olat.course.db.CourseDBManager) CourseEditorEnv(org.olat.course.editor.CourseEditorEnv) ArgumentParseException(org.olat.course.condition.interpreter.ArgumentParseException) Identity(org.olat.core.id.Identity) CourseDBEntry(org.olat.course.db.CourseDBEntry) ArgumentParseException(org.olat.course.condition.interpreter.ArgumentParseException)

Example 7 with CourseDBEntry

use of org.olat.course.db.CourseDBEntry in project openolat by klemens.

the class CourseDBTest method createEntry_get.

@Test
public void createEntry_get() throws IOException, URISyntaxException {
    String category = createRndCategory();
    String key = "getit";
    String value = "get a value";
    CourseDBEntry entry = courseDbManager.setValue(course, auth, category, key, value);
    dbInstance.commitAndCloseSession();
    Assert.assertNotNull(entry);
    RestConnection conn = new RestConnection();
    Assert.assertTrue(conn.login(auth.getName(), JunitTestHelper.PWD));
    UriBuilder uri = getUriBuilder(course.getResourceableId(), category).path("values").path(key);
    HttpGet get = conn.createGet(uri.build(), MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(get);
    assertEquals(200, response.getStatusLine().getStatusCode());
    KeyValuePair savedEntry = conn.parse(response, KeyValuePair.class);
    conn.shutdown();
    Assert.assertNotNull(savedEntry);
    Assert.assertEquals(key, savedEntry.getKey());
    Assert.assertEquals(value, savedEntry.getValue());
}
Also used : KeyValuePair(org.olat.restapi.support.vo.KeyValuePair) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) UriBuilder(javax.ws.rs.core.UriBuilder) CourseDBEntry(org.olat.course.db.CourseDBEntry) Test(org.junit.Test)

Example 8 with CourseDBEntry

use of org.olat.course.db.CourseDBEntry in project openolat by klemens.

the class CourseDBTest method createEntry_post.

@Test
public void createEntry_post() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    Assert.assertTrue(conn.login(auth.getName(), JunitTestHelper.PWD));
    String category = createRndCategory();
    String key = "postit";
    String value = "create the value by POST";
    UriBuilder uri = getUriBuilder(course.getResourceableId(), category).path("values").path(key);
    HttpPost put = conn.createPost(uri.build(), MediaType.APPLICATION_JSON);
    conn.addEntity(put, new BasicNameValuePair("val", value));
    HttpResponse response = conn.execute(put);
    assertEquals(200, response.getStatusLine().getStatusCode());
    EntityUtils.consume(response.getEntity());
    conn.shutdown();
    CourseDBEntry entry = courseDbManager.getValue(course, auth, category, key);
    Assert.assertNotNull(entry);
    Assert.assertEquals(key, entry.getName());
    Assert.assertEquals(value, entry.getValue());
    Assert.assertEquals(category, entry.getCategory());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HttpResponse(org.apache.http.HttpResponse) UriBuilder(javax.ws.rs.core.UriBuilder) CourseDBEntry(org.olat.course.db.CourseDBEntry) Test(org.junit.Test)

Example 9 with CourseDBEntry

use of org.olat.course.db.CourseDBEntry in project openolat by klemens.

the class CourseDbWebService method getValue.

/**
 * Retrieve a value of an authenticated user.
 * @response.representation.200.qname {http://www.example.com}keyValuePair
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The value in the course
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_KEYVALUEVO}
 * @response.representation.404.doc The entry cannot be found
 * @param courseId The course resourceable's id
 * @param category The name of the database
 * @parma name The name of the key value pair
 * @param request The HTTP request
 * @return
 */
@GET
@Path("values/{name}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getValue(@PathParam("courseId") Long courseId, @PathParam("category") String category, @PathParam("name") String name, @Context HttpServletRequest request) {
    ICourse course = loadCourse(courseId);
    UserRequest ureq = RestSecurityHelper.getUserRequest(request);
    CourseDBEntry entry = CoreSpringFactory.getImpl(CourseDBManager.class).getValue(course, ureq.getIdentity(), category, name);
    if (entry == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    Object value = entry.getValue();
    KeyValuePair pair = new KeyValuePair(name, value == null ? "" : value.toString());
    return Response.ok(pair).build();
}
Also used : CourseDBManager(org.olat.course.db.CourseDBManager) KeyValuePair(org.olat.restapi.support.vo.KeyValuePair) ICourse(org.olat.course.ICourse) UserRequest(org.olat.core.gui.UserRequest) CourseDBEntry(org.olat.course.db.CourseDBEntry) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 10 with CourseDBEntry

use of org.olat.course.db.CourseDBEntry in project openolat by klemens.

the class CourseDbWebService method getValuePlain.

/**
 * Retrieve a value of an authenticated user.
 * @response.representation.200.qname {http://www.example.com}keyValuePair
 * @response.representation.200.mediaType text/plain, text/html
 * @response.representation.200.doc A value of the course
 * @response.representation.200.example Green
 * @response.representation.404.doc The entry cannot be found
 * @param courseId The course resourceable's id
 * @param category The name of the database
 * @param name The name of the key value pair
 * @param request The HTTP request
 * @return
 */
@GET
@Path("values/{name}")
@Produces({ MediaType.TEXT_PLAIN, MediaType.TEXT_HTML })
public Response getValuePlain(@PathParam("courseId") Long courseId, @PathParam("category") String category, @PathParam("name") String name, @Context HttpServletRequest request) {
    ICourse course = loadCourse(courseId);
    UserRequest ureq = RestSecurityHelper.getUserRequest(request);
    CourseDBEntry entry = CoreSpringFactory.getImpl(CourseDBManager.class).getValue(course, ureq.getIdentity(), category, name);
    if (entry == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    Object value = entry.getValue();
    String val = value == null ? "" : value.toString();
    return Response.ok(val).build();
}
Also used : CourseDBManager(org.olat.course.db.CourseDBManager) ICourse(org.olat.course.ICourse) UserRequest(org.olat.core.gui.UserRequest) CourseDBEntry(org.olat.course.db.CourseDBEntry) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

CourseDBEntry (org.olat.course.db.CourseDBEntry)18 CourseDBManager (org.olat.course.db.CourseDBManager)10 UriBuilder (javax.ws.rs.core.UriBuilder)8 HttpResponse (org.apache.http.HttpResponse)8 Test (org.junit.Test)8 UserRequest (org.olat.core.gui.UserRequest)8 ICourse (org.olat.course.ICourse)8 GET (javax.ws.rs.GET)6 Path (javax.ws.rs.Path)6 Produces (javax.ws.rs.Produces)6 KeyValuePair (org.olat.restapi.support.vo.KeyValuePair)6 HttpPut (org.apache.http.client.methods.HttpPut)4 HttpGet (org.apache.http.client.methods.HttpGet)2 HttpPost (org.apache.http.client.methods.HttpPost)2 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)2 Identity (org.olat.core.id.Identity)2 OLATResourceable (org.olat.core.id.OLATResourceable)2 ArgumentParseException (org.olat.course.condition.interpreter.ArgumentParseException)2 CourseEditorEnv (org.olat.course.editor.CourseEditorEnv)2 RepositoryEntry (org.olat.repository.RepositoryEntry)2