Using Forms after migrating from playframework 2.0 to 2.1 RC2 (java) - migration

I have updated my controllers with to use play.data.Form.form() method instead of Controller.form(). When I try to run my application I get errors like:
error: method render in class create_user cannot be applied to given types;
return ok(create_user.render("", Form.form(CreateUserInfo.class), creator.get()));
required: String, play.api.data.Form, User
found: String, play.data.Form, User
It looks like the my templates expect to get play.api.data.Form instead of play.data.Form. Is there suppost to be any implicit conversion or should I update my templates to use play.data.Form?
If I'm using play.data.Form in my templates I am missing out on some of the features of play.api.data.Form, like the ability to request parameters through the apply method ( ex: createUserForm("username") )

The solution was provided by Guillaume Bort at the playframework google group.
I forgot to add javaCore as a dependency for my application after updating Build.scala. You have to explicitly add javaCore as a dependency in 2.1.
val appDependencies = Seq(
javaCore
)
And remember to start using play.Project instead of PlayProject:
val main = play.Project(appName, appVersion, appDependencies).settings(
// Add your own project settings here
)

Related

How can I create a simple scala.js cross project with client, server and api modules?

I would like to create a very simple Scala.js application with three modules under a project root like this:
project
-server
-client
-api
This is a cross project because I would like the source in the api module to be compiled by both Scala and Scala.js. I understand that this might nescessitate the need for two api modules (which share the same source code), jvm type and js type.
Source in the server module should only be compiled by Scala and source in the client module should only be compiled by Scala.js. The server module needs to depend on the api module (jvm type) whereas the client module needs to depend on the api module (js type).
Could someone please post the most basic build.sbt that shows how this can be achieved?
EDIT:
It looks like this is the way to do it:
lazy val commonSettings = Seq(
scalaVersion := "2.12.1"
)
lazy val projectRoot = project.in(file(".")).
aggregate(client, server).
settings(
name := "projectRoot"
)
lazy val server = project.in(file("server")).
settings(
commonSettings
).
dependsOn(apiJvm)
lazy val client = project.in(file("client")).
settings(
commonSettings
).
enablePlugins(ScalaJSPlugin).
dependsOn(apiJs)
lazy val api = crossProject.in(file(".")).
settings(
commonSettings
).
jvmSettings(
// Add JVM-specific settings here
).
jsSettings(
// Add JS-specific settings here
)
lazy val apiJvm = api.jvm.in(file("apiJVM"))
lazy val apiJs = api.js.in(file("apiJS"))
The only problem with this is the shared source goes into a folder called shared with a different module name in IntelliJ. It's a shame it isn't in an api folder with a module name of api. Presumably I shouldn't put any code in the apiJS and apiJVM modules that get created? They are only there to be used as dependencies?

How can I read version of project in dotnet core (formerly asp.net mvc6)

how can I read assembly version information for my project, the value which in this case comes from the from project.json, and read that in my ASP.net core Controller and pass it to the view?
You can use Razor to get this right from the View and bypass the Controller.
<b>Version</b> #(
Assembly.GetAssembly(typeof(HomeController))
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion
)
To get your application version, as it exists in project.json, you would write:
string appVersion = Assembly.
GetEntryAssembly().
GetCustomAttribute<AssemblyInformationalVersionAttribute>().
InformationalVersion;
Additionally, if you want to know which version of .net core your app is running on you can do this:
....
string dotNetRuntimeVersion = typeof(RuntimeEnvironment)
.GetTypeInfo()
.Assembly
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion;
You may need to add these units to your using for the above runtime version snippet:
using Microsoft.DotNet.InternalAbstractions;
using System.Reflection;
You can get the AssemblyInformationalVersionAttribute from your project's assembly and then pass it to the view.
Here's how to get that attribute: https://github.com/aspnet/dnx/blob/dev/src/Microsoft.Dnx.Host/RuntimeEnvironment.cs#L35-L44
In an early beta version you could add constructor to your controller with parameter IApplicationEnvironment. this param have a property with Version name
public HomeController(IApplicationEnvironment appEnvironment) {
this.appEnvironment = appEnvironment;
}
(No longer works in ASP.net core 1.0)

Get pluginId stored in the IPreferenceNode in Eclipse

I am developing a plugin, in my plugin I want to get another plugin ID. I use the following code:
PreferenceManager pm = PlatformUI.getWorkbench( ).getPreferenceManager();
List<IPreferenceNode> list = pm.getElements(PreferenceManager.PRE_ORDER);
String pluginid;
// restoreDefValues("org.eclipse.ant.ui");
for(IPreferenceNode node : list){
the code to find the node related to the plugin;
}
When I debug the program, I can clearly see that in variable node(IPreferenceNode), it has the value of the pluginId. However, I check the document of IPreferenceNode, it seems that the neither IPreferenceNode nor the class PreferenceNode, provide a method to return the value of pluginId. I tried node.toString() as well, couldn't get the pluginId. So what should I do? Is there any other ways to get a plugin ID from another plugin?
Preference nodes created using the org.eclipse.ui.preferencePages extension point will actually be instances of org.eclipse.ui.internal.dialogs.WorkbenchPreferenceNode. The super class of this (WorkbenchPreferenceExtensionNode) contains the plugin id.
These classes are internal so you should not try to use them directly. However they implement org.eclipse.ui.IPluginContribution which can be used and has a getPluginId() method.
So something like:
if (node instanceof IPluginContribution) {
pluginId = ((IPluginContribution)node).getPluginId();
}
should work.

Play 2.1 for Http Server

I am new to play framework. My requirement is very simple. I want to create rest api server in scala using play framework. I could use play2-mini, but it seems it is outdated.
I want play 2.1 to be used in my project. Instead of setting play framework as dependency, I want only core module. So I have few questions -
What is core module of Play ? what is module name ?
Is it sufficient to use core module for creating asynchronous http server ?
This link says I can use core module instead of play-mini. If it's true, where can I get more info about it.
You can just simply setup a route and then point it to a controller that parses the data you send. Here is an example of json parsing and serving back a response with play.
http://www.playframework.com/documentation/2.1.1/ScalaJsonRequests
package controllers
import play.api._
import play.api.mvc._
import play.api.libs.json._
// you need this import to have combinators
import play.api.libs.functional.syntax._
object Application extends Controller {
implicit val rds = (
(__ \ 'name).read[String] and
(__ \ 'age).read[Long]
) tupled
def sayHello = Action { request =>
request.body.asJson.map { json =>
json.validate[(String, Long)].map{
case (name, age) => Ok("Hello " + name + ", you're "+age)
}.recoverTotal{
e => BadRequest("Detected error:"+ JsError.toFlatJson(e))
}
}.getOrElse {
BadRequest("Expecting Json data")
}
}
}
or even simpler...
def sayHello = Action(parse.json) { request =>
request.body.validate[(String, Long)].map{
case (name, age) => Ok("Hello " + name + ", you're "+age)
}.recoverTotal{
e => BadRequest("Detected error:"+ JsError.toFlatJson(e))
}
}
Play Framework is a highly modular project. Internally it consists of around 20 subprojects. Some of them you can include in your project as a library dependency if you need them, for example anorm or jdbc. Other projects (i.e. PlayExceptions, RoutesCompiler, TemplatesCompiler etc.) are essential for any Play application, so you don't need to declare dependency on them. These projects could be called a 'core' of Play Framework.
In other words, if you need a Play application with minimum dependencies, just don't declare dependencies you don't need.
Play sources: https://github.com/playframework/Play20
I don't think there is much problem here. For example, this is my build.sbt used for a very small project, that uses json, in which I wanted to use Play libraries, but not necessarily create a full Play app:
name := "my-small-project"
version := "0.0.1-SNAPSHOT"
resolvers ++= Seq(
"TypeSafe Repo" at "http://repo.typesafe.com/typesafe/releases",
"Sonatype Repo" at "http://oss.sonatype.org/content/repositories/releases"
)
libraryDependencies ++= Seq(
"org.specs2" %% "specs2" % "2.3.7" % "test",
"commons-codec" % "commons-codec" % "1.8",
"com.typesafe.play" % "play_2.10" % "2.2.1",
"com.typesafe.play" % "play-json_2.10" % "2.2.1"
)
And You could still remove some dependencies here, especially if You don't need Base64 encoding. Here I consider play_2.10 as "the core" you're interested in. You should get yourself familiar with sbt though, but it's not that hard.
Also remember, that the difference between "a Play application" and "an application that uses Play libraries" is quite fuzzy, especially if you use sbt. And this is the beauty of it, this shows how Play creators thoughtfully tried not to invent a wheel once again by creating custom system requirements for their project to build. You can enter a Play app dir and type "sbt compile" for example instead of firing a Play console and it should work just fine.

Gradle / Groovy properties

I would like to control 'global' config in Gradle build scripts using external property files on each build machine (dev, ci, uat,...) and specify the filename with a command line argument.
e.g. gradle -DbuildProperties=/example/config/build.properties
I specifically don't want to use gradle.properties as we have existing projects that already use this approach and (for example) we want to be able to amend database urls and jdbc drivers without having to change every project.
So far have tried:-
Properties props = new Properties()
props.load(new FileInputStream("$filename"))
project.setProperty('props', props)
which works but has a deprecated warning, but I can't figure out how to avoid this.
Have also tried using groovy style config files with ConfigSlurper:-
environments {
dev {
db.security {
driver=net.sourceforge.jtds.jdbc.Driver
url=jdbc:someserver://somehost:1234/some_db
username=userId
password=secret
}
}
}
but the colons and forward slashes are causing exceptions and we don't want to have to mess up config with escape characters.
There must be a non-deprecated way to do this - can anyone suggest the 'right' way to do it?
Thanks
You can get rid of the deprecated warning quite easily. The message you got probably looks something like this:
Creating properties on demand (a.k.a. dynamic properties) has been deprecated and is scheduled to be removed in Gradle 2.0. Please read http://gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html for information on the replacement for dynamic properties.
Deprecated dynamic property: "props" on "root project 'private'", value: "true".
It can be fixed by replacing:
project.setProperty('props', props)
with
project.ext.props = props
Just to supplement the response given by #Steinar:
it's still possible to use next syntax:
project.ext.set('prop_name', prop_value)
in case you have several properties from file:
props.each({ project.ext.set(it.key, it.value)} )