Friday, April 25, 2014

Feature of the week: creating concepts via the REST API

As a careful reader of our blog, you will have notived that tag relations, or concepts are a feature of BibSonomy which is important to us but probably not often used. Some previous blog posts where we promoted concepts are
We recently started to better support concepts with BibSonomy's REST API by implementing the creation of new concepts in the Java REST client. In this post I give you an example how you can access this functionality. Since we start from scratch, this post is also a good introduction on how to use the Java REST client in general.

Setting up Maven

Using Maven for build management really simplifies many things, so let's use it. First, create a directory for the project and change to that directory. Everything you do, will now happen there. Then inside that directory, create a directory for the source code:

mkdir -p src/main/java

and then create a file called pom.xml with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"                                                                                                        
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                                                                                               
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <name>Test</name>
  <groupId>com.example</groupId>
  <artifactId>test</artifactId>
  <version>0.0.1</version>
  <repositories>
    <repository>
      <id>dev.bibsonomy.org</id>
      <url>http://dev.bibsonomy.org/maven2/</url>
    </repository>
  </repositories>
  <dependencies>
    <dependency>
      <groupId>org.bibsonomy</groupId>
      <artifactId>bibsonomy-rest-client</artifactId>
      <version>2.0.43</version>
    </dependency>
    <dependency>
      <groupId>org.bibsonomy</groupId>
      <artifactId>bibsonomy-model</artifactId>
        <version>2.0.43</version>
    </dependency>
  </dependencies>
</project>
You can now already test, if everything is OK by calling mvn install which should print you some lines with one of the last being

[INFO] BUILD SUCCESS

Writing and calling Java code

Now you can start to write the first Java code. Create the file src/main/java/Test.java with the following content:

class Test {

    public static void main(String args[]) {
       System.out.println("Hello World!");
    }
}

You can compile this file into a JAR by calling mvn install again. The resulting JAR file test-0.0.1.jar will be in the target folder. You can run this class with the command

java -cp target/test-0.0.1.jar Test

or alternatively with
mvn exec:java -Dexec.mainClass="Test"

The latter is the preferred choice for the next steps, since you will include some libraries whose dependencies Maven automatically resolves.

Calling the REST API

Change the file Test.java to the following code:

import java.util.*;                                                                                                                                          
import org.bibsonomy.common.enums.*;
import org.bibsonomy.model.*;                                                                                                                                
import org.bibsonomy.model.logic.*;
import org.bibsonomy.model.enums.*;                                                                                                                          
import org.bibsonomy.rest.client.*;

class Test {

    public static void main(String args[]) {
        RestLogicFactory rlf = new RestLogicFactory("http://www.bibsonomy.org/api");

        LogicInterface logic = rlf.getLogicAccess("jaeschke", "YOUR-API-KEY-HERE");

        List<Post<BibTex>> posts = logic.getPosts(BibTex.class, GroupingEntity.USER, "jaeschke", null, null, null, null, Order.ADDED, null, null, 0, 1);

        for (Post<BibTex> post: posts) {
        BibTex publication = post.getResource();
            System.out.println(publication.getTitle());
        }
    }
}
 
Note that you have to change the line

LogicInterface logic = rlf.getLogicAccess("jaeschke", "YOUR-API-KEY-HERE");

to use your BibSonomy user name instead of "jaeschke". You also must insert your API key which you can get from BibSonomy's settings page. When you compile (mvn install) and then run (mvn exec:java -Dexec.mainClass="Test") this class, it should print the title of my last publication post. You now change that code again, to finally create a concept.

Creating a concept with the REST API

import java.util.*;
import org.bibsonomy.common.enums.*;
import org.bibsonomy.model.*;
import org.bibsonomy.model.logic.*;
import org.bibsonomy.model.enums.*;
import org.bibsonomy.model.util.*;
import org.bibsonomy.rest.client.*;

class Test {

    public static void main(String args[]) {
        LogicInterface logic = new RestLogicFactory().getLogicAccess("jaeschke", "YOUR-API-KEY-HERE");

        Tag concept = new Tag("science");
        concept.addSubTag(new Tag("physics"));
        concept.addSubTag(new Tag("chemistry"));
        concept.addSubTag(new Tag("biology"));

        logic.createConcept(concept, GroupingEntity.USER, "jaeschke");
    }
}

Again, you have to change your credentials in the call to getLogicAccess().

What does the code do? First, it creates an object for the concept "science"

Tag concept = new Tag("science");

and then adds the subconcepts "physics", "chemistry", and "biology":

concept.addSubTag(new Tag("physics"));
concept.addSubTag(new Tag("chemistry"));
concept.addSubTag(new Tag("biology"));
Finally, the concept is stored on the server:

logic.createConcept(concept, GroupingEntity.USER, "jaeschke");

Note that you have to exchange "jaeschke" here by your user name, since one can only create concepts for one's own account. You can then find the concept in your sidebar:


That's it! :-)


Happy programming & tagging!

Popular Posts