Not solved import controllers.Application in Scala´s Play framework - scala

I´m developing a Play Framework project in Scala, but I not able to get correctly the import controllers.Application reference.
The snippet bellow focus on the onStart method at my Global.scala file, that is located inside the app directory. What is going under the hood? Why can´t I get this easy import done correctly?
import play.api._
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.concurrent._
import play.api.Play.current
import scala.concurrent.duration._
import scala.util.Random
import model.StatusUpdate
object Global extends GlobalSettings {
override def onStart(app: Application) {
import controllers.{Application => App}
// Definition of the onStart method.
}
My Application.scala file that is located in the controllers directory is defined as bellow:
package controllers
import play.api._
import play.api.libs.iteratee._
import play.api.libs.json._
import play.api.mvc._
object Application extends Controller {
//Definition of methods
}

Related

Ammonite Ivy Import Fails For Joda Time

I have the following in my predef.sc file and when I load Ammonite via my terminal, it fails for one library, the joda-time:
import $ivy.`org.typelevel::cats-core:2.1.1`, cats._, cats.implicits._
import $ivy.`org.scalatest::scalatest:3.0.8`,org.scalatest._
import $ivy.`org.scalacheck::scalacheck:1.14.0`
import $ivy.`io.monix::monix:3.1.0`
import $ivy.`dev.zio::zio:1.0.0-RC18`
import $ivy.`com.github.nscala-time::nscala-time:2.26.0`
import $ivy.`org.typelevel::cats-effect:2.1.2`
import $ivy.`com.github.chocpanda::scalacheck-magnolia:0.3.1`
import $ivy.`io.chrisdavenport::cats-scalacheck:0.2.0`
import $ivy.`net.ruippeixotog::scala-scraper:2.2.0`
import $ivy.`com.softwaremill.sttp.client3::core:3.1.9`
import $ivy.`joda-time::joda-time:2.10.14`
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.Future
import scala.util.{Failure, Success}
import scala.concurrent.Await
import monix.eval.Task
import org.scalacheck.{Arbitrary, Gen}
import com.github.nscala_time.time.Imports.DateTime
import com.github.nscala_time.time.Imports.DateTimeFormat
import org.scalacheck.magnolia._
import net.ruippeixotog.scalascraper.browser.JsoupBrowser
import net.ruippeixotog.scalascraper.dsl.DSL._
import net.ruippeixotog.scalascraper.dsl.DSL.Extract._
import net.ruippeixotog.scalascraper.dsl.DSL.Parse._
import sttp.client3._
import monix.reactive.Observable
import monix.execution.Ack.Continue
import monix.execution.{Ack, Scheduler}
import monix.reactive.observers.Subscriber
import monix.execution.Scheduler.Implicits.global
import org.joda.time.{DateTime, Days, Interval, Weeks}
import scala.math.Integral.Implicits._
The actual error message:
joesan#joesan-S-14-v5:~$ amm
Loading...
Failed to resolve ivy dependencies:Error downloading joda-time:joda-time_2.13:2.10.14
not found: /home/joesan/.ivy2/local/joda-time/joda-time_2.13/2.10.14/ivys/ivy.xml
not found: https://repo1.maven.org/maven2/joda-time/joda-time_2.13/2.10.14/joda-time_2.13-2.10.14.pom
I got it resolved by adding a dependency to the nscala-time as below:
import $ivy.`com.github.nscala-time::nscala-time:2.30.0`
import com.github.nscala_time.time.Imports._
Not sure why this was able to resolve while the joda-time not!

Scala Play Framework not found: type AbstractController problem

I have a TaskList App on Scala Play Framework and I am creating the first controller like this
package controllers
import javax.inject._
import play.api.mvc._
import play.api.i18n._
#Singelton
class TaskList1 #Inject{}(cc: ControllerComponents) extends AbstractController(cc){
def taskList = Action {
Ok("Task List")
}
}
But I have this problem when I launch my application
enter image description here
The error
You have few typos here.
It should be:
#Singleton
class TaskList1 #Inject()(cc: ControllerComponents) extends AbstractController(cc) {
// ...

Play get instance of DatabaseConfigProvider

I'm use Slick as a Play module. In this way, DatabaseConfigProvider will be injected to Controller.
In a special stage, I want get DatabaseConfigProvider out of Controller.
How can I get DatabaseConfigProvider instance?
You can inject it like this:
class MyClass #Inject() (protected val dbConfigProvider: DatabaseConfigProvider)
extends HasDatabaseConfigProvider[MyPostgresDriver]
With these imports:
import javax.inject.Inject
import database.MyPostgresDriver
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import database.MyPostgresDriver.api._

Failed assertion with automated initialisation of database in Cassandra

Completely new to Cassandra. Tried to initialize a database in Cassandra using phantom-dsl. I received this error message.
*** RUN ABORTED ***
java.lang.AssertionError: assertion failed: no symbol could be loaded from class com.datastax.driver.core.Cluster in package core with name Cluster and classloader sun.misc.Launcher$AppClassLoader#279f2327
at scala.reflect.runtime.JavaMirrors$JavaMirror.scala$reflect$runtime$JavaMirrors$JavaMirror$$classToScala1(JavaMirrors.scala:1021)
at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$classToScala$1.apply(JavaMirrors.scala:980)
at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$classToScala$1.apply(JavaMirrors.scala:980)
at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$toScala$1.apply(JavaMirrors.scala:97)
at scala.reflect.runtime.TwoWayCaches$TwoWayCache$$anonfun$toScala$1.apply(TwoWayCaches.scala:39)
at scala.reflect.runtime.Gil$class.gilSynchronized(Gil.scala:19)
at scala.reflect.runtime.JavaUniverse.gilSynchronized(JavaUniverse.scala:16)
at scala.reflect.runtime.TwoWayCaches$TwoWayCache.toScala(TwoWayCaches.scala:34)
at scala.reflect.runtime.JavaMirrors$JavaMirror.toScala(JavaMirrors.scala:95)
at scala.reflect.runtime.JavaMirrors$JavaMirror.classToScala(JavaMirrors.scala:980)
I am not really sure whether it is an issue with the Connector in phantom-dsl or the ClusterBuilder in datastax-driver.
Connector.scala
package com.neruti.db
import com.neruti.db.models._
import com.websudos.phantom.database.Database
import com.websudos.phantom.connectors.ContactPoints
import com.websudos.phantom.dsl.KeySpaceDef
object Connector {
val host= Seq("localhost")
val port = 9160
val keySpace: String = "nrt_entities"
// val inet = InetAddress.getByName
lazy val connector = ContactPoints(host,port).withClusterBuilder(
_.withCredentials("cassandra", "cassandra")
).keySpace(keySpace)
}
CassandraSpec.scala
package com.neruti.db
import com.neruti.User
import com.neruti.db.models._
import com.neruti.db.databases._
import com.neruti.db.services._
import com.neruti.db.Connector._
import java.util.UUID
import com.datastax.driver.core.ResultSet
import org.scalatest._
import org.scalatest.{BeforeAndAfterAll,FlatSpec,Matchers,ShouldMatchers}
import org.scalatest.concurrent.ScalaFutures
import org.scalamock.scalatest.MockFactory
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
abstract class BaseCassandraSpec extends FlatSpec
with BeforeAndAfterAll
with Inspectors
with Matchers
with OptionValues
with ScalaFutures
class CassandraTest extends BaseCassandraSpec
with ProductionDatabase
with UserService
with Connector.connector.Connector{
val user = User(
Some("foobar"),
Some("foo#foobar.com"),
Some(UUID.randomUUID()),
)
override protected def beforeAll(): Unit = {
Await.result(database.userModel.create(user),10.seconds)
}
}
Looks there may be multiple issues you are looking at:
The latest version of phantom is 2.1.3, I'd strongly recommend using that, especially if you are just starting out. The migration guide is here in case you need it.
The entire reflection mechanism has been replaced in the latest version, so that error should magically go away. With respect to testing and generating objects, I would also look to include com.outworkers.util.testing, which is freely available on Maven Central
libraryDependencies ++= Seq(
//..,
"com.outworkers" %% "phantom-dsl" % "2.1.3",
"com.outworkers" %% "util-testing" % "0.30.1" % Test
)
This will offer you automated case class generation:
import com.outworkers.util.testing._
val sample = gen[User]

Where is GuiceApplicationBuilder?

I am currently trying to set up a Play2 application with dependency injection, with information from the official docs. However, my IDE cannot find GuiceApplicationBuilder. So which additional entry for libraryDependencies do I have to specify to get this builder?
EDIT: I made a screenshot of what happens when I try to import the things stated by #anquegi
EDIT 2: The problem was very simple: I used the wrong version of Play - 2.3.8 did in fact not have these, I had to use 2.4.0-RC2 to get it to work.
At the dependencies in the play docs /ScalaDependencyInjection you must import this
import play.api.ApplicationLoader
import play.api.Configuration
import play.api.inject._
import play.api.inject.guice._
This is the compile code in the section Advanced: Extending the GuiceApplicationLoader, so you need to use this extending in your class:
import play.api.ApplicationLoader
import play.api.Configuration
import play.api.inject._
import play.api.inject.guice._
class CustomApplicationLoader extends GuiceApplicationLoader() {
override def builder(context: ApplicationLoader.Context): GuiceApplicationBuilder = {
val extra = Configuration("a" -> 1)
initialBuilder
.in(context.environment)
.loadConfig(extra ++ context.initialConfiguration)
.overrides(overrides(context): _*)
}
}