Using REST to Manage Books for a Bookstore

This REST Bookstore Sample Using a Database shows how to use the RESTful service to add, delete, update, and retrieve books from Bookstore.

The sample uses the following HTTP methods for the Books REST resource:

  • POST: Add books to the bookstore
  • GET: Get books from the bookstore
  • PUT: Updates books to the bookstore
  • DELETE: Deletes books from the bookstore

Prerequisites

Install the latest Google Chrome version at the standard default location.

Install the PostgreSQL Database:
  1. Download and install PostgreSQL from http://www.postgresql.org/download/.
  2. Go to SQL Shell (psql) from the installed program.
  3. The PostgreSQL command line window starts.
  4. Open the DB and table creation script, dbsetup.sql from the scripts folder under TIBCO_HOME/bw/n.n/samples/binding/rest/BookStore/scripts.
  5. Run the script from the command line as per the instructions given in the readme file present under the scripts folder.

You see the database and the tables created from the pgAdmin UI from the installed program.

Note: If you are accessing the sample from the Welcome page of TIBCO Business Studio™ for BusinessWorks™, go directly to Step 3.

Procedure

  1. In the samples directory, select binding > rest > Bookstore and double-click tibco.bw.sample.binding.rest.BookStore. For more information, see Accessing Samples.
  2. In Project Explorer expand the tibco.bw.sample.binding.rest.BookStore project.
  3. Set the default application profile to match the OS you are running on. For more information, see Setting the Default Application Profile.


  4. Expand tibco.bw.sample.binding.rest.Bookstore.application > Package Unit folder. Provide valid values for the application properties. Provide a valid username, password and database URL to connect to your PostgreSQL database in the module properties, if different from the default setting.
  5. Verify your JDBC connection.
    1. Fully expand the Resources directory.
    2. Double-click JDBCConnectionResource.jdbcResource.
    3. In JDBC Driver, click Click Here to Set Preferences.
    4. Set the JDBC driver folder directory preference to the folder where you have downloaded PostgreSQL JDBC Driver as mentioned in the Prerequisites section. Click Apply and then click OK.
    5. Click the Test Connection button to verify the connection.
  6. Click File > Save.
  7. Fully expand the Processes directory and double-click Books.bwp.
  8. Click Run > Debug Configurations.
  9. At the left hand tree of Debug Configuration wizard, expand BusinessWorks Application and select BWApplication.
  10. Click Applications tab and then click the Deselect All button if you have multiple applications. Select the check box next to tibco.bw.sample.binding.rest.BookStore.
  11. Click Debug.
    The sample runs in Debug mode. The console window shows engine messages similar to: Started BW Application [tibco.bw.sample.binding.rest.BookStore.application:1.0]. The OSGI command to list REST and Swagger URLs is lrestdoc, which lists the discovery URL: [Application Name]: tibco.bw.sample.binding.rest.BookStore.application [Discovery Url]: http://localhost:7777/tibco.bw.sample.binding.rest.BookStore.application.
  12. Launch Google Chrome browser and open http://localhost:7777/tibco.bw.sample.binding.rest.BookStore.application. The Google Chrome page displays as follows.
  13. Click on any of the operations, such as POST, GET, PUT, DELETE as displayed on the web page.
  14. After you have finished with the operations on the page, in TIBCO Business Studio for BusinessWorks, click the Terminate icon to stop the process.

Result

The web page lists the following operations for Books and Events:

Books

  • Post books
  • GET books
  • GET book by ISBN
  • PUT book by ISBN
  • DELETE book by ISBN
  • GET book by Price
Events
  • POST Events
  • GET Events
  • GET Event by EventID
  • PUT Event by EventID
  • DELETE Event by EventID

GET books returns an output similar to the following:

{
  "Book": [
    {
      "isbn": "0061122416",
      "name": "The Alchemist",
      "description": "Every few decades a book is published that changes the lives of its readers forever. The Alchemist is such a book",
      "authorName": "Paul Coelho",
      "releaseDate": "2006-04-25",
      "vintage": true,
      "signed": true,
      "price": 11.9
    },
    {
      "isbn": "0071450149",
      "name": "The Power to Predict",
      "description": "How Real Time Businesses Anticipate Customer Needs, Create Opportunities, and Beat the Competition",
      "authorName": "Vivek Ranadive",
      "releaseDate": "2006-01-26",
      "vintage": false,
      "signed": true,
      "price": 15.999
    }
 ]    
}

GET books by ISBN returns an output similar to the following for the ISBN 0061122416:

  {
      "isbn": "0061122416",
      "name": "The Alchemist",
      "description": "Every few decades a book is published that changes the lives of its readers forever. The Alchemist is such a book",
      "authorName": "Paul Coelho",
      "releaseDate": "2006-04-25",
      "vintage": true,
      "signed": true,
      "price": 11.9
    }

The books.log file is generated with the following information:

POST Books------->{"Book":[{"isbn":"1451648537","name":"Steve Jobs","description":"Biography of Apple Co-Founder Steve Jobs","authorName":"Walter Isaacson","releaseDate":"2012-10-24","vintage":false,"signed":false,"price":21},{"isbn":"0385537859","name":"Inferno","description":"Robert Langdon returns in Dan Brown's latest fast paced action thirller","authorName":"Dan Brown","releaseDate":"2013-05-14","vintage":false,"signed":true,"price":14.09},{"isbn":"0399103421","name":"The Godfather","description":"The Godfather is an epic story of a New York's top mafia family, loyalty, and how men of honor live in their own world, and die by their own laws.","authorName":"Mario Puzo","releaseDate":"1969-03-10","vintage":true,"signed":true,"price":50}]}**************************************************************

GET Books------->{"Book":[{"isbn":"1451648537","name":"Steve Jobs","description":"Biography of Apple Co-Founder Steve Jobs","authorName":"Walter Isaacson","releaseDate":"2012-10-24+05:30","vintage":false,"signed":false,"price":21},{"isbn":"0385537859","name":"Inferno","description":"Robert Langdon returns in Dan Brown's latest fast paced action thirller","authorName":"Dan Brown","releaseDate":"2013-05-14+05:30","vintage":false,"signed":true,"price":14.09},{"isbn":"0399103421","name":"The Godfather","description":"The Godfather is an epic story of a New York's top mafia family, loyalty, and how men of honor live in their own world, and die by their own laws.","authorName":"Mario Puzo","releaseDate":"1969-03-10+05:30","vintage":true,"signed":true,"price":50}]}**************************************************************

GET Book By ISBN------->{"isbn":"1451648537","name":"Steve Jobs","description":"Biography of Apple Co-Founder Steve Jobs","authorName":"Walter Isaacson","releaseDate":"2012-10-24+05:30","vintage":false,"signed":false,"price":21}**************************************************************

DELETE Book By ISBN-------->"Deleted book with ISBN - 1451648537"**************************************************************

GET Events By ISBN---->{}**************************************************************

GET Books By Price More than 10---->{"Book":[{"isbn":"0385537859","name":"Inferno","description":"Robert Langdon returns in Dan Brown's latest fast paced action thirller","authorName":"Dan Brown","releaseDate":"2013-05-14+05:30","vintage":false,"signed":true,"price":14.09},{"isbn":"0399103421","name":"The Godfather","description":"The Godfather is an epic story of a New York's top mafia family, loyalty, and how men of honor live in their own world, and die by their own laws.","authorName":"Mario Puzo","releaseDate":"1969-03-10+05:30","vintage":true,"signed":true,"price":50},{"isbn":"0385537829","name":"Angels and Demons","description":"Robert Langdon returns in Dan Brown's fast paced action thirller","authorName":"Dan Brown","releaseDate":"2013-05-14+05:30","vintage":false,"signed":true,"price":15.09},{"isbn":"0399103439","name":"The Godfather1","description":"The Godfather is an epic story of a New York's top mafia family, loyalty, and how men of honor live in their own world, and die by their own laws.","authorName":"Mario Puzo","releaseDate":"1969-03-10+05:30","vintage":true,"signed":true,"price":50}]}**************************************************************

GET Books By Price More than 50---->{"Book":[null]}**************************************************************

Understanding the Configuration

The project contains three processes: Books, Events, Invoke_Client, RESTInvoke.bwp and two subprocesses; BooksDB and EventsDB.

Books Process: In this process, the Book REST service performs the post, get, put, delete operations. It posts one or more books to the bookstore and retrieves all the books from the bookstore. For the post operation, a sub process is called to add books to the database. The addBooks operation is implemented in the BooksDB subprocess. The Book REST service performs three operations: getting a book from the bookstore by ISBN, updating a book in the bookstore using ISBN, and deleting a specific book from the bookstore using ISBN. ISBN is passed as a parameter to these operations. The booksbyprice service gets books from the bookstore using price, which is passed as a query parameter to the GET operation. This service responds with an array of Books if there are books whose price is greater than the provided input (price). If there are no such books, the service returns a Null array.

BooksDB Subprocess: The subprocess handles deletes, updates, retrievals of books from the database. The BooksPersist service implements the following operations: adding books to database, getting all books from database, getting a book by ISBN, updating a book by ISBN, deleting a book by ISBN and getting a book by price. Using BooksPersist/addBooks, books are added to the database.

Events Process: It is similar to the Books process and is used to add, delete, update, and retrieve events.



EventsDB Process: It is similar to the BooksDB process and is used to add, delete, update, and retrieve events from the database.



Invoke_Client Process: This is a REST Client process which uses Invoke REST API activity to retrieve data from the REST Server. It is similar to Swagger UI. Using this process, you can POST, GET, PUT, and DELETE books by specifying the HTTP Client, Resource Path, HTTP Method, Request Type, and Response Type in the Invoke REST API activity.

For example, in POST_Books, we provide HTTP Client Resource which listens to the same host and port as that of server. The Resource Path is "/books", HTTP Method is "POST", and Request and Response type is JSON. In the Message Body, we provide the JSON payload which is read from the file present at BW_HOME\samples\binding\rest\BookStore\samplejson\books.json. This posts books from the books.json payload into the database and you can view the data posted into the output file.



RESTInvoke Process:This is a REST Client process which uses the REST Invoke activity to retrieve data from the REST Server. In addition to invoking the existing services, it invokes the service getBooksByPrice which demonstrates null and array responses.

Troubleshooting

  • If you are unable to insert rows into the database using the dbsetup.sql script in the scripts folder, use the sample json payload from the samplejson folder to post the data.
  • If you do not see the REST Swagger API page, verify whether the application has started.
  • If you see any problem markers in the project, clean the project by invoking Project > Clean or try switching to a clean new workspace.
  • If you get any File was not found exception, verify whether books.json and book_put.json file is present at the location as specified in the Input_File and Input_File_1 Module Property.
  • If you get an error about unregistered user, open the pgAdmin UI and select all check boxes under the Role Privileges tab for the bwuser.