This tutorial will explain how one could use his own existing database with Shindig.
In this tutorial the social requests to Shindig will be taking data from
your own database. Before this tutorial you should have an idea of
how one could provide his own implementation of PersonService, AppDateService, etc.
and how one could add a database to shindig with JPA.

To start with one has to have a working version of shindig-database connection as described in another tutorial.

1) Imagine I have a social MySQL database with only one table 'people' and three colums:

table 'people'
   column oid (bigint)
   column person_id(varchar 255)
   column display_name(varchar 255)

This will be my own social database that I want to connect to Shindig. (To make life easier you can just remove tables and redundant columns from shindig database (generated in a previous post) and rename 'person' table to 'people' table). Add the following data into table: .

  oid = 1
  person_id = user1
  display_name = Shindig

2) Now we have to tell shindig not to generate database every time it loads. Normally it should be possible to do through java/samples/src/main/resources/META-INF/persistence.xml but it seems Shindig's samples implementation doesn't take properly those properties from that file. So I had to change the following file: java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/eclipselink/

    // TODO: this should be taken from persistence.xml
    properties.put(PersistenceUnitProperties.DDL_GENERATION, "none");
    properties.put(PersistenceUnitProperties.DROP_JDBC_DDL_FILE, "drop.sql");
    properties.put(PersistenceUnitProperties.CREATE_JDBC_DDL_FILE, "create.sql");

    // The default shindig settings
    // properties.put(PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.CREATE_ONLY);
    // properties.put(PersistenceUnitProperties.DROP_JDBC_DDL_FILE, "drop.sql");
    // properties.put(PersistenceUnitProperties.CREATE_JDBC_DDL_FILE, "create.sql");
    // properties.put(PersistenceUnitProperties.DDL_GENERATION_MODE,
    //     PersistenceUnitProperties.DDL_BOTH_GENERATION);

3) Now we have to change the file java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ so that it takes needed for us data from the database. Read the explanations in comments.

// table renamed from 'person' to 'people'
// add query is changed to return a PersonDb object (read JPQL syntax for more info, link at the bottom of a post)
@Table(name = "people")
@NamedQueries(value = {
    @NamedQuery(name = PersonDb.FINDBY_PERSONID,
        query = "select NEW, p.displayName) from PersonDb p where = :id "),
    @NamedQuery(name = PersonDb.FINDBY_LIKE_PERSONID,
        query = "select p from PersonDb p where like :id") })

  // removed third parameter and commented
  // since we do not have this object in the database anymore
  public PersonDb(String id, String displayName) { = id;
    // = name;
    this.displayName = displayName;

4) Now recompile project and run jetty server

# Compilation: in shindig root folder
# To build samples jar
$mvn -Dmaven.test.skip -P samples
# To build server jar
$mvn -Dmaven.test.skip
# Running server
$cd java/server
$mvn jetty:run

5) Now execute a social request in your browser.

//'user1' is a person_id in our database

You should get back a json response (I had to reload page 3 times to get it)

{"value":"XA","displayValue":"Extended Away"},

So, now you are ready to change, etc. files according to your own social database implementation! Concerning JPQL syntax I found the following useful: JPQL Language Reference.

  • No labels