Showing posts with label Playframework. Show all posts
Showing posts with label Playframework. Show all posts

Sunday, October 8, 2017

Wednesday, October 28, 2015

Generate CSV in Playframwork

Example of generate CSV by modify output header.

  def exportCSV = Action {
    val data = "data 1, data 2"

      Ok(data).withHeaders(
          CONTENT_TYPE -> "text/csv",
          CONTENT_DISPOSITION -> "attachment; filename=foo.csv"
      )
  }

This method only suitable if you have small amount of data. If not, I would recommend using streaming.

Sunday, November 2, 2014

MongoDB / Salat plugin for Play 2.3


Here is complete installation steps on Salat plugin for Play 2.3:

  1. You modify your project/Build.scala:
    import sbt._
    import Keys._
    import play.Play.autoImport._
    import PlayKeys._
    import play.twirl.sbt.Import.TwirlKeys
    
    object ApplicationBuild extends Build {
      val appName = "Playframework"
      val appVersion = "2.3.6"
      
      val appDependencies = Seq(
          "se.radley" %% "play-plugins-salat" % "1.5.0"
      )
    
      val main = Project(appName, file(".")).enablePlugins(play.PlayScala).settings(
        version := appVersion,
        libraryDependencies ++= appDependencies,
        routesImport += "se.radley.plugin.salat.Binders._",
        TwirlKeys.templateImports += "org.bson.types.ObjectId"
      )
    }
  2. Creating or appending conf/play.plugins
    500:se.radley.plugin.salat.SalatPlugin
  3. Continue edit conf/application.conf
    dbplugin = disabled
    evolutionplugin = disabled
    ehcacheplugin = disabled
    
    mongodb.default.db = "mydb"
    
    # Optional values
    #mongodb.default.host = "127.0.0.1"
    #mongodb.default.port = 27017
    #mongodb.default.user = "leon"
    #mongodb.default.password = "123456"
    
    # MongoURI
    # ~~~~~
    # a MongoURI can also be used http://www.mongodb.org/display/DOCS/Connections
    # mongodb.default.uri = "mongodb://127.0.0.1:27017,mongodb.org:1337/salat-test"
    
    # WriteConcern
    # ~~~~~
    # Can be any of the following
    #
    # fsyncsafe - Exceptions are raised for network issues and server errors; Write operations wait for the server to flush data to disk.
    # replicassafe - Exceptions are raised for network issues and server errors; waits for at least 2 servers for the write operation.
    # safe - Exceptions are raised for network issues and server errors; waits on a server for the write operation.
    # normal - Exceptions are raised for network issues but not server errors.
    
    #mongodb.default.writeconcern = "safe"
    
    # Replica sets
    # ~~~~~
    # http://www.mongodb.org/display/DOCS/Why+Replica+Sets
    #
    # To user a replicaset instead of a single host, omit optional values and use the configuration below instead.
    # Since replica sets use public key authentication, user and password won't work together with the replicaset option.
    
    #mongodb.default.replicaset {
    #    host1.host = "10.0.0.1"
    #
    #    host2.host = "10.0.0.2"
    #    host2.port = 27018
    #}
    
    # Mongo Options
    # ~~~~~
    # http://api.mongodb.org/java/2.8.0/com/mongodb/MongoOptions.html
    #
    # For passing custom options to the MongoConnection add the properties under "options". Add just the ones which are different from defaults.
    
    #mongodb.default.options {
    #    connectionsPerHost = 100
    #    threadsAllowedToBlockForConnectionMultiplier = 1000
    #    connectTimeout = 60000
    #}
    
  4. Create app/models/mongoContext.scala
    package models
    
    import com.novus.salat.dao._
    import com.novus.salat.annotations._
    import com.mongodb.casbah.Imports._
    import com.novus.salat.{TypeHintFrequency, StringTypeHintStrategy, Context}
    import play.api.Play
    import play.api.Play.current
    
    package object mongoContext {
      implicit val context = {
        val context = new Context {
          val name = "global"
          override val typeHintStrategy = StringTypeHintStrategy(when = TypeHintFrequency.WhenNecessary, typeHint = "_t")
        }
        context.registerGlobalKeyOverride(remapThis = "id", toThisInstead = "_id")
        context.registerClassLoader(Play.classloader)
        context
      }
    }
  5. Remove built.sbt as we are using Build.scala. This to avoid below runtime error.
    java.lang.RuntimeException: Overlapping output directories
  6. Start your play application using "activator run".

References:
https://github.com/leon/play-salat