Employee-Self-Service: Reactive and Non-Blocking Database Access using Play Framework and Anorm – (Part-4)


Last week, We have added Database Access as Reactive and Non-Blocking behaviour in Employee-Self-Service  application.

Now we have added some more features. These are following:

  • Achieving, Futures to use more idiomatic error handling.
  • Replaced the embedded JS & CSS libraries with WebJars.
  • Achieving, table pagination and sorting functionality.

Before:

def list() = Action.async { implicit request =>
 val futureEmpList = scala.concurrent.Future { Employee.list }
 val timeoutFuture = Promise.timeout("Oops", 10.second)
 Future.firstCompletedOf(Seq(futureEmpList, timeoutFuture)).map {
   case data: List[Employee] => Ok(html.list(data))
   case t: String =>
      Logger.error("Problem found in employee list process")
      InternalServerError(t)
   }
}

 After:

/**
 * Display the paginated list of employees.
 *
 * @param page Current page number (starts from 0)
 * @param orderBy Column to be sorted
 * @param filter Filter applied on employee names
 */
 def list(page: Int, orderBy: Int, filter: String) = Action.async { implicit request =>
   val futurePage: Future[Page[Employee]] = TimeoutFuture(Employee.list(page = page, orderBy = orderBy, filter = ("%" + filter + "%")))
   futurePage.map(page => Ok(html.list(page, orderBy, filter))).recover {
     case t: TimeoutException =>
       Logger.error("Problem found in employee list process")
       InternalServerError(t.getMessage)
   }
 }

Cleaned up the Futures to use more idiomatic error handling. Implemented by James Ward

object TimeoutFuture {

    def apply[A](block: => A)(implicit timeout: FiniteDuration): Future[A] = {

      val promise = scala.concurrent.promise[A]()

      // if the promise doesn't have a value yet then this completes the future with a failure
      Promise.timeout(Nil, timeout).map(_ => promise.tryFailure(new TimeoutException("This operation timed out")))

      // this tries to complete the future with the value from block
      Future(promise.success(block))

      promise.future
    }

}

Replaced the embedded JS & CSS libraries with WebJars. Implemented by James Ward

Do the following steps to implement WebJars functionality with Play Framework:

  1. Add following entry into build.sbt file.
"org.webjars" %% "webjars-play" % "2.2.2-1",
"org.webjars" % "bootstrap" % "3.1.0",
"org.webjars" % "bootswatch-yeti" % "3.1.1",
"org.webjars" % "html5shiv" % "3.7.0",
"org.webjars" % "respond" % "1.4.2"

2. Add following entry into  conf/routes file.

# Map the webjar static assets to the /webjars URL
GET     /webjars/*file                    controllers.WebJarAssets.at(file)

3. Add following entry into app/views/main.scala.html file.

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" media="screen" href="@routes.WebJarAssets.at(WebJarAssets.locate("bootswatch-yeti", "bootstrap.min.css"))">

<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
  <script src="@routes.WebJarAssets.at(WebJarAssets.locate("html5shiv.js"))"></script>
  <script src="@routes.WebJarAssets.at(WebJarAssets.locate("respond.min.js"))"></script>
<![endif]-->

<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="@routes.WebJarAssets.at(WebJarAssets.locate("jquery.min.js"))"></script>
        
<!-- Latest compiled and minified JavaScript -->
<script src="@routes.WebJarAssets.at(WebJarAssets.locate("bootswatch-yeti", "bootstrap.min.js"))"></script>

Special thanks to James Ward for his contribution and valuable time to improve the code quality and implemented the WebJars functionality into this application.


Check the application and the code base on bellow links:

The live application is currently hosted at : http://employee-self-service.herokuapp.com/
The Github code for the project is at : https://github.com/knoldus/Employee-Self-Service

This is the fourth edition of this application. If you have any changes then feel free to send in pull requests and we would do the merges 🙂 Stay tuned.

Previous blogs related to this application:

  1. Employee-Self-Service – (Part-1)
  2. Employee-Self-Service – (Part-2)
  3. Employee-Self-Service – (Part-3)
Advertisements

About Anand Kumar Singh

Hi, I’m Anand Singh and I specialize in Computer Software. I’m passionate about what I do, and I love to help people. Nothing is more fulfilling than being part of a team with similar interests, and an organization that values its employees. I'm Lead Software Engineer at Sapient. Having 5+ years of experience in Java, Grails/Groovy and Scala language. Anand also knows about Javascript, jQuery, CSS and other UI design technologies. Certified developer in: * Principles of Reactive Programming – Coursera * Functional Programming Principles in Scala – Coursera * Functional Program Design in Scala – Coursera * Introduction to Big Data with Apache Spark – edX * Oracle Certified Associate (OCA) * Sun Certified Java Programmer (SCJP) I'm passionate to work with upcoming technologies and bleeding platforms providing the new way to programming & development. He is actively involved in development & maintenance of several software projects. He has been enjoying Scala for more than a year now. Anand completed his MCA from IGNOU. As a Developer, I'm continuing my journey by actively collaborating with other Developers in my field. I'm actively contributing on the TechMonad & Playing Scala platform. I likes to spend his free time in Cooking, watching Discovery for new Technology and visiting new places with friends.
This entry was posted in Bootstrap, CSS, Future, JavaScript, jQuery, Non-Blocking, Play Framework, Reactive, Scala, Web and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s